public InputStream decrypt(InputStream in) throws Exception { // Read the header of the encrypted file. byte[] pbeIV = readBytes(PBE_IV_LENGTH, in); byte[] encryptionIV = readBytes(ENCRYPTION_IV_LENGTH, in); int keyLength = in.read(); byte[] encryptedKeyBytes = readBytes(keyLength, in); // Decrypt the key bytes byte[] decryptedKeyBytes = transformWithPassword(encryptedKeyBytes, pbeIV, password, Cipher.DECRYPT_MODE); // Create the key from the key bytes Key key = new SecretKeySpec(decryptedKeyBytes, ENCRYPTION_KEY_ALGORITHM); // If we haven't yet generated a key, just use this one if (this.key == null) { this.pbeIV = pbeIV; this.encryptionIV = encryptionIV; this.encryptedKeyBytes = encryptedKeyBytes; this.key = key; } // now create the decrypt cipher Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(encryptionIV)); return new CipherInputStream(in, cipher); }
key = generateKey(getKeysize()); pbeIV = randomBytes(PBE_IV_LENGTH); encryptionIV = randomBytes(ENCRYPTION_IV_LENGTH); encryptedKeyBytes = transformWithPassword(key.getEncoded(), pbeIV, password, Cipher.ENCRYPT_MODE);
key = generateKey(getKeysize()); pbeIV = randomBytes(PBE_IV_LENGTH); encryptionIV = randomBytes(ENCRYPTION_IV_LENGTH); encryptedKeyBytes = transformWithPassword(key.getEncoded(), pbeIV, password, Cipher.ENCRYPT_MODE);
public InputStream decrypt(InputStream in) throws Exception { // Read the header of the encrypted file. byte[] pbeIV = readBytes(PBE_IV_LENGTH, in); byte[] encryptionIV = readBytes(ENCRYPTION_IV_LENGTH, in); int keyLength = in.read(); byte[] encryptedKeyBytes = readBytes(keyLength, in); // Decrypt the key bytes byte[] decryptedKeyBytes = transformWithPassword(encryptedKeyBytes, pbeIV, password, Cipher.DECRYPT_MODE); // Create the key from the key bytes Key key = new SecretKeySpec(decryptedKeyBytes, ENCRYPTION_KEY_ALGORITHM); // If we haven't yet generated a key, just use this one if (this.key == null) { this.pbeIV = pbeIV; this.encryptionIV = encryptionIV; this.encryptedKeyBytes = encryptedKeyBytes; this.key = key; } // now create the decrypt cipher Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(encryptionIV)); return new CipherInputStream(in, cipher); }