/** * Initialize a new cipher object with the given cipher properties and no padding * If the given algorithm is not implemented in the JCE, it will try to load it from the bouncy castle * provider. * * @param key the secret key * @param cipherAlgorithm the cipher algorithm * @param chain the chaining mode * @param vec the initialization vector (IV), can be null * @param cipherMode Cipher.DECRYPT_MODE or Cipher.ENCRYPT_MODE * @return the requested cipher * @throws GeneralSecurityException * @throws EncryptedDocumentException if the initialization failed or if an algorithm was specified, * which depends on a missing bouncy castle provider */ public static Cipher getCipher(SecretKey key, CipherAlgorithm cipherAlgorithm, ChainingMode chain, byte[] vec, int cipherMode) { return getCipher(key, cipherAlgorithm, chain, vec, cipherMode, null); }
@Override protected InputStream decorateInputStream(FileInputStream fis) { Cipher ciDec = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.DECRYPT_MODE, "PKCS5Padding"); return new CipherInputStream(fis, ciDec); } }
/** * Returns the input stream for reading the previously written encrypted data * * @return the inputstream * @throws IOException if the reading of the underlying file fails */ public InputStream getInputStream() throws IOException { Cipher ciDec = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.DECRYPT_MODE, PADDING); return new CipherInputStream(new FileInputStream(tempFile), ciDec); }
/** * Returns the output stream for writing the data.<p> * Make sure to close it, otherwise the last cipher block is not written completely. * * @return the outputstream * @throws IOException if the writing to the underlying file fails */ public OutputStream getOutputStream() throws IOException { Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.ENCRYPT_MODE, PADDING); return new CipherOutputStream(new FileOutputStream(tempFile), ciEnc); }
private static AesZipFileZipEntrySource fileToSource(File tmpFile, byte keyBytes[], byte ivBytes[]) throws IOException { SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, CipherAlgorithm.aes128.jceId); Cipher ciDec = CryptoFunctions.getCipher(skeySpec, CipherAlgorithm.aes128, ChainingMode.cbc, ivBytes, Cipher.DECRYPT_MODE, PADDING); return new AesZipFileZipEntrySource(tmpFile, ciDec); }
@Override protected OutputStream decorateOutputStream(FileOutputStream fos) { init(); Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.ENCRYPT_MODE, "PKCS5Padding"); return new CipherOutputStream(fos, ciEnc); }
public byte[] sign() throws IOException, GeneralSecurityException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); bos.write(getHashMagic()); bos.write(md.digest()); final Cipher cipher = CryptoFunctions.getCipher(key, CipherAlgorithm.rsa , ChainingMode.ecb, null, Cipher.ENCRYPT_MODE, "PKCS1Padding"); return cipher.doFinal(bos.toByteArray()); }
private Cipher getCipher(SecretKey key) { EncryptionHeader em = getEncryptionInfo().getHeader(); ChainingMode cm = em.getChainingMode(); assert(cm == ChainingMode.ecb); return CryptoFunctions.getCipher(key, em.getCipherAlgorithm(), cm, null, Cipher.DECRYPT_MODE); }
private Cipher getCipher(SecretKey key, String padding) { EncryptionVerifier ver = getEncryptionInfo().getVerifier(); return CryptoFunctions.getCipher(key, ver.getCipherAlgorithm(), ver.getChainingMode(), null, Cipher.ENCRYPT_MODE, padding); }
protected static Cipher initCipherForBlock(Cipher cipher, int block, EncryptionInfo encryptionInfo, SecretKey skey, int encryptMode) throws GeneralSecurityException { EncryptionVerifier ver = encryptionInfo.getVerifier(); HashAlgorithm hashAlgo = ver.getHashAlgorithm(); byte blockKey[] = new byte[4]; LittleEndian.putUInt(blockKey, 0, block); byte encKey[] = CryptoFunctions.generateKey(skey.getEncoded(), hashAlgo, blockKey, 16); SecretKey key = new SecretKeySpec(encKey, skey.getAlgorithm()); if (cipher == null) { EncryptionHeader em = encryptionInfo.getHeader(); cipher = CryptoFunctions.getCipher(key, em.getCipherAlgorithm(), null, null, encryptMode); } else { cipher.init(encryptMode, key); } return cipher; }
protected static Cipher initCipherForBlock(Cipher cipher, int block, EncryptionInfo encryptionInfo, SecretKey skey, int encryptMode) throws GeneralSecurityException { EncryptionVerifier ver = encryptionInfo.getVerifier(); HashAlgorithm hashAlgo = ver.getHashAlgorithm(); byte blockKey[] = new byte[4]; LittleEndian.putUInt(blockKey, 0, block); MessageDigest hashAlg = CryptoFunctions.getMessageDigest(hashAlgo); hashAlg.update(skey.getEncoded()); byte encKey[] = hashAlg.digest(blockKey); EncryptionHeader header = encryptionInfo.getHeader(); int keyBits = header.getKeySize(); encKey = CryptoFunctions.getBlock0(encKey, keyBits / 8); if (keyBits == 40) { encKey = CryptoFunctions.getBlock0(encKey, 16); } SecretKey key = new SecretKeySpec(encKey, skey.getAlgorithm()); if (cipher == null) { cipher = CryptoFunctions.getCipher(key, header.getCipherAlgorithm(), null, null, encryptMode); } else { cipher.init(encryptMode, key); } return cipher; }
protected static Cipher initCipherForBlock(Cipher existing, int block, boolean lastChunk, EncryptionInfo encryptionInfo, SecretKey skey, int encryptionMode) throws GeneralSecurityException { EncryptionHeader header = encryptionInfo.getHeader(); String padding = (lastChunk ? "PKCS5Padding" : "NoPadding"); if (existing == null || !existing.getAlgorithm().endsWith(padding)) { existing = getCipher(skey, header.getCipherAlgorithm(), header.getChainingMode(), header.getKeySalt(), encryptionMode, padding); } byte[] blockKey = new byte[4]; LittleEndian.putInt(blockKey, 0, block); byte[] iv = generateIv(header.getHashAlgorithm(), header.getKeySalt(), blockKey, header.getBlockSize()); AlgorithmParameterSpec aps; if (header.getCipherAlgorithm() == CipherAlgorithm.rc2) { aps = new RC2ParameterSpec(skey.getEncoded().length*8, iv); } else { aps = new IvParameterSpec(iv); } existing.init(encryptionMode, skey, aps); return existing; }
Cipher cipher = CryptoFunctions.getCipher(getSecretKey(), header.getCipherAlgorithm(), header.getChainingMode(), iv, Cipher.ENCRYPT_MODE); byte encryptedHmacValue[] = cipher.doFinal(hmacValueFilled);
cipher = getCipher(secretKey, cipherAlgo, header.getChainingMode(), vec, Cipher.DECRYPT_MODE); byte hmacKey[] = cipher.doFinal(header.getEncryptedHmacKey()); hmacKey = getBlock0(hmacKey, hashAlgo.hashSize); cipher = getCipher(secretKey, cipherAlgo, header.getChainingMode(), vec, Cipher.DECRYPT_MODE); byte hmacValue[] = cipher.doFinal(header.getEncryptedHmacValue()); hmacValue = getBlock0(hmacValue, hashAlgo.hashSize);
/* package */ static byte[] hashInput(AgileEncryptionVerifier ver, byte pwHash[], byte blockKey[], byte inputKey[], int cipherMode) { CipherAlgorithm cipherAlgo = ver.getCipherAlgorithm(); ChainingMode chainMode = ver.getChainingMode(); int keySize = ver.getKeySize()/8; int blockSize = ver.getBlockSize(); HashAlgorithm hashAlgo = ver.getHashAlgorithm(); byte intermedKey[] = generateKey(pwHash, hashAlgo, blockKey, keySize); SecretKey skey = new SecretKeySpec(intermedKey, cipherAlgo.jceId); byte[] iv = generateIv(hashAlgo, ver.getSalt(), null, blockSize); Cipher cipher = getCipher(skey, cipherAlgo, chainMode, iv, cipherMode); byte[] hashFinal; try { inputKey = getBlock0(inputKey, getNextBlockSize(inputKey.length, blockSize)); hashFinal = cipher.doFinal(inputKey); return hashFinal; } catch (GeneralSecurityException e) { throw new EncryptedDocumentException(e); } }
private static void copyToFile(InputStream is, File tmpFile, byte keyBytes[], byte ivBytes[]) throws IOException { SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, CipherAlgorithm.aes128.jceId); Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, CipherAlgorithm.aes128, ChainingMode.cbc, ivBytes, Cipher.ENCRYPT_MODE, PADDING);
Cipher cipher = getCipher(secretKey, header.getCipherAlgorithm(), header.getChainingMode(), vec, Cipher.ENCRYPT_MODE); byte hmacKey[] = getBlock0(this.integritySalt, getNextBlockSize(this.integritySalt.length, blockSize)); byte encryptedHmacKey[] = cipher.doFinal(hmacKey);
Cipher cipher = getCipher(secretKey, cipherAlgo, header.getChainingMode(), vec, Cipher.DECRYPT_MODE); byte hmacKey[] = cipher.doFinal(header.getEncryptedHmacKey()); hmacKey = getBlock0(hmacKey, header.getHashAlgorithm().hashSize); cipher = getCipher(secretKey, cipherAlgo, ver.getChainingMode(), vec, Cipher.DECRYPT_MODE); byte hmacValue[] = cipher.doFinal(header.getEncryptedHmacValue()); hmacValue = getBlock0(hmacValue, header.getHashAlgorithm().hashSize);
/** * Returns the output stream for writing the data.<p> * Make sure to close it, otherwise the last cipher block is not written completely. * * @return the outputstream * @throws IOException if the writing to the underlying file fails */ public OutputStream getOutputStream() throws IOException { Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.ENCRYPT_MODE, PADDING); return new CipherOutputStream(new FileOutputStream(tempFile), ciEnc); }
@Override protected OutputStream decorateOutputStream(FileOutputStream fos) { init(); Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.ENCRYPT_MODE, "PKCS5Padding"); return new CipherOutputStream(fos, ciEnc); }