List of usage examples for org.bouncycastle.crypto.paddings PaddedBufferedBlockCipher reset
public void reset()
From source file:org.albertschmitt.crypto.AESService.java
License:Open Source License
/** * Return a PaddedBufferedBlockCipher for encryption or decryption. * * @param iv The initialization vector./*from w ww.j a v a 2 s . co m*/ * @param forEncryption True to encrypt, false to decrypt. * @return PaddedBufferedBlockCipher configured to encrypt or decrypt. */ private PaddedBufferedBlockCipher getCipher(byte[] iv, Boolean forEncryption) { ParametersWithIV ivKeyParam = new ParametersWithIV(aes_key, iv); BlockCipherPadding padding = new PKCS7Padding(); PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), padding); cipher.reset(); cipher.init(forEncryption, ivKeyParam); return cipher; }
From source file:org.xwiki.crypto.passwd.internal.AbstractPasswordCiphertext.java
License:Open Source License
/** * {@inheritDoc}//from w ww.j a v a2 s . co m * * @see org.xwiki.crypto.passwd.PasswordCiphertext#init(byte[], java.lang.String, org.xwiki.crypto.passwd.KeyDerivationFunction) */ public synchronized void init(byte[] message, String password, KeyDerivationFunction initializedKeyFunction) throws GeneralSecurityException { this.keyFunction = initializedKeyFunction; PaddedBufferedBlockCipher theCipher = this.getCipher(); theCipher.reset(); theCipher.init(true, this.makeKey(password)); try { this.ciphertext = new byte[theCipher.getOutputSize(message.length)]; int length = theCipher.processBytes(message, 0, message.length, this.ciphertext, 0); theCipher.doFinal(this.ciphertext, length); } catch (InvalidCipherTextException e) { // I don't think this should ever happen for encrypting. throw new GeneralSecurityException("Failed to encrypt text", e); } }
From source file:org.xwiki.crypto.passwd.internal.AbstractPasswordCiphertext.java
License:Open Source License
/** * {@inheritDoc}/*from w ww .ja v a 2s.co m*/ * * @see org.xwiki.crypto.passwd.PasswordCiphertext#decrypt(java.lang.String) */ public synchronized byte[] decrypt(String password) throws GeneralSecurityException { PaddedBufferedBlockCipher theCipher = this.getCipher(); theCipher.reset(); theCipher.init(false, this.makeKey(password)); try { final byte[] out = new byte[theCipher.getOutputSize(ciphertext.length)]; int length = theCipher.processBytes(ciphertext, 0, ciphertext.length, out, 0); int remaining = theCipher.doFinal(out, length); // length+remaining is the actual length of the output. getOutputSize is close but still leaves a few // nulls at the top of the array. final byte[] unpadded = new byte[length + remaining]; System.arraycopy(out, 0, unpadded, 0, unpadded.length); return unpadded; } catch (InvalidCipherTextException e) { // We are going to assume here that the password was wrong. return null; } }