List of usage examples for org.bouncycastle.crypto.modes CBCBlockCipher CBCBlockCipher
public CBCBlockCipher(BlockCipher cipher)
From source file:org.sperle.keepass.crypto.bc.AESCipher.java
License:Open Source License
public byte[] encrypt(byte[] key, byte[] plainText, byte[] iv, int rounds, boolean padding, ProgressMonitor pm) throws KeePassCryptoException { try {/*from w w w . ja v a 2 s .c om*/ BufferedBlockCipher cipher = null; if (padding) { cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())); } else { cipher = new BufferedBlockCipher(new AESEngine()); } if (iv != null) cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv)); else cipher.init(true, new KeyParameter(key)); if (pm != null) { if (rounds == 1) pm.nextStep(plainText.length / cipher.getBlockSize(), "pm_encrypt"); // count length (database) else if (rounds > 1) pm.nextStep(rounds, "pm_encrypt"); // count rounds (master password) } byte[] cipherText = null; if (padding) { cipherText = new byte[cipher.getOutputSize(plainText.length)]; } else { cipherText = new byte[plainText.length]; } int outLength = cipher.processBytes(plainText, 0, plainText.length, cipherText, 0, rounds == 1 ? pm : null); if (outLength == -1) return null; // user canceled if (rounds > 1) { if (pm != null) pm.tick(); for (int i = 1; i < rounds; i++) { outLength = cipher.processBytes(cipherText, 0, cipherText.length, cipherText, 0, null); if (pm != null) { if (pm.isCanceled()) return null; pm.tick(); } } } if (padding) cipher.doFinal(cipherText, outLength); return cipherText; } catch (Exception e) { throw new KeePassCryptoException("Exception during AES encryption: " + e.getMessage()); } }
From source file:org.sperle.keepass.crypto.bc.AESCipher.java
License:Open Source License
public byte[] decrypt(byte[] key, byte[] cipherText, byte[] iv, ProgressMonitor pm) throws KeePassCryptoException { try {/*from www. j a v a2 s. c o m*/ BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())); if (iv != null) cipher.init(false, new ParametersWithIV(new KeyParameter(key), iv)); else cipher.init(false, new KeyParameter(key)); if (pm != null) pm.nextStep(cipherText.length / cipher.getBlockSize(), "pm_decrypt"); byte[] plainText = new byte[cipher.getOutputSize(cipherText.length)]; int outLength = cipher.processBytes(cipherText, 0, cipherText.length, plainText, 0, pm); if (outLength == -1) return null; // user canceled outLength += cipher.doFinal(plainText, outLength); return (outLength < plainText.length) ? ByteArrays.cut(plainText, outLength) : plainText; } catch (Exception e) { throw new KeePassCryptoException("Exception during AES decryption: " + e.getMessage()); } }
From source file:org.springframework.security.crypto.encrypt.BouncyCastleAesCbcBytesEncryptor.java
License:Apache License
@Override public byte[] encrypt(byte[] bytes) { byte[] iv = this.ivGenerator.generateKey(); PaddedBufferedBlockCipher blockCipher = new PaddedBufferedBlockCipher( new CBCBlockCipher(new AESFastEngine()), new PKCS7Padding()); blockCipher.init(true, new ParametersWithIV(secretKey, iv)); byte[] encrypted = process(blockCipher, bytes); return iv != null ? concatenate(iv, encrypted) : encrypted; }
From source file:org.springframework.security.crypto.encrypt.BouncyCastleAesCbcBytesEncryptor.java
License:Apache License
@Override public byte[] decrypt(byte[] encryptedBytes) { byte[] iv = subArray(encryptedBytes, 0, this.ivGenerator.getKeyLength()); encryptedBytes = subArray(encryptedBytes, this.ivGenerator.getKeyLength(), encryptedBytes.length); PaddedBufferedBlockCipher blockCipher = new PaddedBufferedBlockCipher( new CBCBlockCipher(new AESFastEngine()), new PKCS7Padding()); blockCipher.init(false, new ParametersWithIV(secretKey, iv)); return process(blockCipher, encryptedBytes); }
From source file:org.tramaci.onionmail.Stdio.java
License:Open Source License
public static byte[] AESEnc2m(byte[][] key, byte[][] iv, byte[] data) throws Exception { byte[][] blo = Stdio.DivBlock(data, 16, false); int cx = blo.length; int kc = key.length; for (int kx = 0; kx < kc; kx++) { CBCBlockCipher aes = new CBCBlockCipher(new AESEngine()); CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key[kx]), iv[kx]); aes.init(true, ivAndKey);//from ww w .jav a 2s . c o m for (int ax = 0; ax < cx; ax++) aes.processBlock(blo[ax], 0, blo[ax], 0); } data = Stdio.MulBlock(blo, 16); blo = null; return data; }
From source file:org.tramaci.onionmail.Stdio.java
License:Open Source License
public static byte[] AESDec2m(byte[][] key, byte[][] iv, byte[] data) throws Exception { byte[][] blo = Stdio.DivBlock(data, 16, false); int cx = blo.length; int kc = key.length - 1; for (int kx = kc; kx > -1; kx--) { CBCBlockCipher aes = new CBCBlockCipher(new AESEngine()); CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key[kx]), iv[kx]); aes.init(false, ivAndKey);/*from ww w. jav a 2 s.c o m*/ for (int ax = 0; ax < cx; ax++) aes.processBlock(blo[ax], 0, blo[ax], 0); } data = Stdio.MulBlock(blo, 16); blo = null; return data; }
From source file:org.tramaci.onionmail.Stdio.java
License:Open Source License
public static byte[] AESEnc2(byte[] key, byte[] iv, byte[] data) throws Exception { byte[][] blo = Stdio.DivBlock(data, 16, false); CBCBlockCipher aes = new CBCBlockCipher(new AESEngine()); CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv); aes.init(true, ivAndKey);/*from www . j ava 2 s . c o m*/ int cx = blo.length; for (int ax = 0; ax < cx; ax++) aes.processBlock(blo[ax], 0, blo[ax], 0); data = Stdio.MulBlock(blo, 16); blo = null; return data; }
From source file:org.tramaci.onionmail.Stdio.java
License:Open Source License
public static byte[] AESDec2(byte[] key, byte[] iv, byte[] data) throws Exception { byte[][] blo = Stdio.DivBlock(data, 16, false); CBCBlockCipher aes = new CBCBlockCipher(new AESEngine()); CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv); aes.init(false, ivAndKey);//www. j ava 2 s. c o m int cx = blo.length; for (int ax = 0; ax < cx; ax++) aes.processBlock(blo[ax], 0, blo[ax], 0); data = Stdio.MulBlock(blo, 16); blo = null; return data; }
From source file:org.tramaci.onionmail.Stdio.java
License:Open Source License
public static byte[] AES2Enc(byte[] key, byte[] iv, byte[] data) throws Exception { PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())); CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv); aes.init(true, ivAndKey);// www . j a v a 2 s. co m return AES2cipher(aes, data); }
From source file:org.tramaci.onionmail.Stdio.java
License:Open Source License
public static byte[] AES2Dec(byte[] key, byte[] iv, byte[] data) throws Exception { try {/*from w w w . jav a2s. co m*/ PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())); CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv); aes.init(false, ivAndKey); return AES2cipher(aes, data); } catch (Exception E) { throw new Exception("!Invalid KEY for data" + E.getMessage()); } }