List of usage examples for org.bouncycastle.crypto.paddings PaddedBufferedBlockCipher PaddedBufferedBlockCipher
public PaddedBufferedBlockCipher(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. j a va 2s . 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 {// w w w . j av 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.spout.api.security.SecurityHandler.java
License:Open Source License
public PaddedBufferedBlockCipher addSymmetricPadding(BlockCipher rawCipher, String padding) { if (padding.equals("PKCS7")) { return new PaddedBufferedBlockCipher(rawCipher); }//from w w w . ja v a 2s. com return null; }
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);//from w ww . java2 s . c om 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 {/*ww w . j a v 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()); } }
From source file:org.universAAL.ri.gateway.communication.cipher.Blowfish.java
License:Apache License
public Blowfish(final String encryptionKey) { cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new BlowfishEngine())); key = new KeyParameter(encryptionKey.getBytes()); }
From source file:org.votingsystem.signature.util.Encryptor.java
License:Open Source License
public static String encryptAES(String messageToEncrypt, AESParams aesParams) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException, InvalidCipherTextException { PaddedBufferedBlockCipher pbbc = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())); KeyParameter keyParam = new KeyParameter(aesParams.getKey().getEncoded()); ParametersWithIV params = new ParametersWithIV(keyParam, aesParams.getIV().getIV()); pbbc.init(true, params); //to decrypt put param to false byte[] input = messageToEncrypt.getBytes("UTF-8"); byte[] output = new byte[pbbc.getOutputSize(input.length)]; int bytesWrittenOut = pbbc.processBytes(input, 0, input.length, output, 0); pbbc.doFinal(output, bytesWrittenOut); return new String(org.bouncycastle.util.encoders.Base64.encode(output)); }
From source file:org.votingsystem.signature.util.Encryptor.java
License:Open Source License
public static String decryptAES(String messageToDecrypt, AESParams aesParams) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException, InvalidCipherTextException { PaddedBufferedBlockCipher pbbc = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())); KeyParameter keyParam = new KeyParameter(aesParams.getKey().getEncoded()); CipherParameters params = new ParametersWithIV(keyParam, aesParams.getIV().getIV()); pbbc.init(false, params); //to encrypt put param to true byte[] input = org.bouncycastle.util.encoders.Base64.decode(messageToDecrypt.getBytes("UTF-8")); byte[] output = new byte[pbbc.getOutputSize(input.length)]; int bytesWrittenOut = pbbc.processBytes(input, 0, input.length, output, 0); pbbc.doFinal(output, bytesWrittenOut); int i = output.length - 1; //remove padding while (i >= 0 && output[i] == 0) { --i;/*from www . j av a 2 s .c o m*/ } return new String(Arrays.copyOf(output, i + 1), "UTF-8"); }
From source file:org.xmind.core.internal.security.BouncyCastleSecurityProvider.java
License:Open Source License
private BufferedBlockCipher createCipher(boolean encrypt, IEncryptionData encData, String password) throws CoreException { checkEncryptionData(encData);/*from w w w . jav a 2s.co m*/ // Create a parameter generator PKCS12ParametersGenerator paramGen = new PKCS12ParametersGenerator(new MD5Digest()); // Get the password bytes byte[] pwBytes = password == null ? new byte[0] : PBEParametersGenerator.PKCS12PasswordToBytes(password.toCharArray()); // Initialize the parameter generator with password bytes, // salt and iteration counts paramGen.init(pwBytes, getSalt(encData), getIterationCount(encData)); // Generate a parameter CipherParameters param = paramGen.generateDerivedParameters(128); // Create a block cipher BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())); // Initialize the block cipher cipher.init(encrypt, param); return cipher; }
From source file:org.xwiki.crypto.cipher.internal.symmetric.BcPaddedSymmetricCipher.java
License:Open Source License
/** * Generic Bouncy Castle based block cipher with padding. * @param cipher the block cipher to encapsulate. * @param forEncryption true if the block cipher is setup for encryption. * @param parameters parameters to initialize the cipher. *//*ww w . ja v a2 s. c om*/ public BcPaddedSymmetricCipher(BlockCipher cipher, boolean forEncryption, CipherParameters parameters) { super(new PaddedBufferedBlockCipher(cipher), forEncryption, parameters); this.paddingName = (getOutputBlockSize() <= 8) ? PKCS5_PADDING : PKCS7_PADDING; }