use of org.bouncycastle.crypto.modes.CBCBlockCipher in project faf-java-server by FAForever.
the class UniqueIdService method aesDecrypt.
private byte[] aesDecrypt(byte[] initVector, byte[] aesEncryptedJson, byte[] aesKey) throws InvalidCipherTextException {
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
cipher.init(false, new ParametersWithIV(new KeyParameter(aesKey), initVector));
int plaintextSize = cipher.processBytes(aesEncryptedJson, 0, aesEncryptedJson.length, aesEncryptedJson, 0);
plaintextSize += cipher.doFinal(aesEncryptedJson, plaintextSize);
return Arrays.copyOf(aesEncryptedJson, plaintextSize);
}
use of org.bouncycastle.crypto.modes.CBCBlockCipher in project samourai-wallet-android by Samourai-Wallet.
the class AESUtil method encryptWithSetMode.
public static String encryptWithSetMode(String cleartext, CharSequenceX password, int iterations, int mode, @Nullable BlockCipherPadding padding) throws DecryptionException, UnsupportedEncodingException {
final int AESBlockSize = 4;
if (password == null) {
throw new DecryptionException("Password null");
}
// Use secure random to generate a 16 byte iv
SecureRandom random = new SecureRandom();
byte[] iv = new byte[AESBlockSize * 4];
random.nextBytes(iv);
byte[] clearbytes = cleartext.getBytes("UTF-8");
PBEParametersGenerator generator = new PKCS5S2ParametersGenerator();
generator.init(PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(password.toString().toCharArray()), iv, iterations);
KeyParameter keyParam = (KeyParameter) generator.generateDerivedParameters(256);
CipherParameters params = new ParametersWithIV(keyParam, iv);
BlockCipher cipherMode;
if (mode == MODE_CBC) {
cipherMode = new CBCBlockCipher(new AESEngine());
} else {
// mode == MODE_OFB
cipherMode = new OFBBlockCipher(new AESEngine(), 128);
}
BufferedBlockCipher cipher;
if (padding != null) {
cipher = new PaddedBufferedBlockCipher(cipherMode, padding);
} else {
cipher = new BufferedBlockCipher(cipherMode);
}
cipher.reset();
cipher.init(true, params);
byte[] outBuf = cipherData(cipher, clearbytes);
// Append to IV to the output
int len1 = iv.length;
int len2 = outBuf.length;
byte[] ivAppended = new byte[len1 + len2];
System.arraycopy(iv, 0, ivAppended, 0, len1);
System.arraycopy(outBuf, 0, ivAppended, len1, len2);
// String ret = Base64.encodeBase64String(ivAppended);
byte[] raw = Base64.encodeBase64(ivAppended);
return new String(raw);
}
use of org.bouncycastle.crypto.modes.CBCBlockCipher in project spring-security by spring-projects.
the class BouncyCastleAesCbcBytesEncryptor method encrypt.
@Override
@SuppressWarnings("deprecation")
public byte[] encrypt(byte[] bytes) {
byte[] iv = this.ivGenerator.generateKey();
PaddedBufferedBlockCipher blockCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new org.bouncycastle.crypto.engines.AESFastEngine()), new PKCS7Padding());
blockCipher.init(true, new ParametersWithIV(this.secretKey, iv));
byte[] encrypted = process(blockCipher, bytes);
return (iv != null) ? EncodingUtils.concatenate(iv, encrypted) : encrypted;
}
use of org.bouncycastle.crypto.modes.CBCBlockCipher in project gocd by gocd.
the class DESEncrypter method encrypt.
private static String encrypt(byte[] key, String plainText) throws CryptoException {
try {
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESEngine()));
KeyParameter keyParameter = new KeyParameter(key);
cipher.init(true, keyParameter);
byte[] plainTextBytes = plainText.getBytes();
byte[] cipherTextBytes = new byte[cipher.getOutputSize(plainTextBytes.length)];
int outputLength = cipher.processBytes(plainTextBytes, 0, plainTextBytes.length, cipherTextBytes, 0);
cipher.doFinal(cipherTextBytes, outputLength);
return ENCODER.encodeToString(cipherTextBytes).trim();
} catch (Exception e) {
throw new CryptoException(e);
}
}
Aggregations