use of org.spongycastle.crypto.modes.CBCBlockCipher in project rskj by rsksmart.
the class KeyCrypterAes method encrypt.
/**
* Password based encryption using AES - CBC 256 bits.
*/
@Override
public EncryptedData encrypt(byte[] plainBytes, KeyParameter key) {
checkNotNull(plainBytes);
checkNotNull(key);
try {
// Generate iv - each encryption call has a different iv.
byte[] iv = new byte[BLOCK_LENGTH];
secureRandom.nextBytes(iv);
ParametersWithIV keyWithIv = new ParametersWithIV(key, iv);
// Encrypt using AES.
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
cipher.init(true, keyWithIv);
byte[] encryptedBytes = new byte[cipher.getOutputSize(plainBytes.length)];
final int length1 = cipher.processBytes(plainBytes, 0, plainBytes.length, encryptedBytes, 0);
final int length2 = cipher.doFinal(encryptedBytes, length1);
return new EncryptedData(iv, Arrays.copyOf(encryptedBytes, length1 + length2));
} catch (Exception e) {
throw new KeyCrypterException("Could not encrypt bytes.", e);
}
}
use of org.spongycastle.crypto.modes.CBCBlockCipher in project bitcoin-wallet by bitcoin-wallet.
the class Crypto method encryptRaw.
/**
* Password based encryption using AES - CBC 256 bits.
*
* @param plainBytes
* The bytes to encrypt
* @param password
* The password to use for encryption
* @return SALT_LENGTH bytes of salt followed by the encrypted bytes.
* @throws IOException
*/
private static byte[] encryptRaw(final byte[] plainTextAsBytes, final char[] password) throws IOException {
try {
// Generate salt - each encryption call has a different salt.
final byte[] salt = new byte[SALT_LENGTH];
secureRandom.nextBytes(salt);
final ParametersWithIV key = (ParametersWithIV) getAESPasswordKey(password, salt);
// The following code uses an AES cipher to encrypt the message.
final BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
cipher.init(true, key);
final byte[] encryptedBytes = new byte[cipher.getOutputSize(plainTextAsBytes.length)];
final int processLen = cipher.processBytes(plainTextAsBytes, 0, plainTextAsBytes.length, encryptedBytes, 0);
final int doFinalLen = cipher.doFinal(encryptedBytes, processLen);
// The result bytes are the SALT_LENGTH bytes followed by the encrypted bytes.
return concat(salt, Arrays.copyOf(encryptedBytes, processLen + doFinalLen));
} catch (final InvalidCipherTextException | DataLengthException x) {
throw new IOException("Could not encrypt bytes", x);
}
}
use of org.spongycastle.crypto.modes.CBCBlockCipher in project jmulticard by ctt-gob-es.
the class AmAESCrypto method initCiphers.
private void initCiphers(final byte[] key, final byte[] iv) {
// get the keyBytes
this.keyBytes = new byte[key.length];
System.arraycopy(key, 0, this.keyBytes, 0, key.length);
this.keyP = new KeyParameter(this.keyBytes);
// get the IV
this.IV = new byte[BLOCK_SIZE];
System.arraycopy(iv, 0, this.IV, 0, this.IV.length);
// create the ciphers
// AES block cipher in CBC mode with ISO7816d4 padding
this.encryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new ISO7816d4Padding());
this.decryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new ISO7816d4Padding());
// create the IV parameter
final ParametersWithIV parameterIV = new ParametersWithIV(this.keyP, this.IV);
this.encryptCipher.init(true, parameterIV);
this.decryptCipher.init(false, parameterIV);
}
use of org.spongycastle.crypto.modes.CBCBlockCipher in project SightRemote by TebbeUbben.
the class Cryptograph method produceCCMTag.
public static byte[] produceCCMTag(byte[] nonce, byte[] payload, byte[] header, byte[] key) {
TwofishEngine engine = new TwofishEngine();
engine.init(true, new KeyParameter(key));
byte[] initializationVector = new byte[engine.getBlockSize()];
engine.processBlock(produceIV(nonce, (short) payload.length), 0, initializationVector, 0);
CBCBlockCipher cbc = new CBCBlockCipher(new TwofishEngine());
cbc.init(true, new ParametersWithIV(new KeyParameter(key), initializationVector));
byte[] processedHeader = blockCipherZeroPad(processHeader(header));
byte[] processedPayload = blockCipherZeroPad(payload);
byte[] combine = combine(processedHeader, blockCipherZeroPad(processedPayload));
byte[] result = new byte[combine.length];
for (int i = 0; i < combine.length / 16; i++) cbc.processBlock(combine, i * 16, result, i * 16);
byte[] result2 = new byte[8];
System.arraycopy(result, result.length - 16, result2, 0, 8);
byte[] ctr = new byte[engine.getBlockSize()];
engine.processBlock(produceCTRBlock(nonce, (short) 0), 0, ctr, 0);
return byteArrayXOR(result2, ctr);
}
use of org.spongycastle.crypto.modes.CBCBlockCipher in project bitcoin-wallet by bitcoin-wallet.
the class Crypto method decryptRaw.
/**
* Decrypt bytes previously encrypted with this class.
*
* @param bytesToDecode
* The bytes to decrypt
* @param passwordbThe
* password to use for decryption
* @return The decrypted bytes
* @throws IOException
*/
private static byte[] decryptRaw(final byte[] bytesToDecode, final char[] password) throws IOException {
try {
// separate the salt and bytes to decrypt
final byte[] salt = new byte[SALT_LENGTH];
System.arraycopy(bytesToDecode, 0, salt, 0, SALT_LENGTH);
final byte[] cipherBytes = new byte[bytesToDecode.length - SALT_LENGTH];
System.arraycopy(bytesToDecode, SALT_LENGTH, cipherBytes, 0, bytesToDecode.length - SALT_LENGTH);
final ParametersWithIV key = (ParametersWithIV) getAESPasswordKey(password, salt);
// decrypt the message
final BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
cipher.init(false, key);
final byte[] decryptedBytes = new byte[cipher.getOutputSize(cipherBytes.length)];
final int processLen = cipher.processBytes(cipherBytes, 0, cipherBytes.length, decryptedBytes, 0);
final int doFinalLen = cipher.doFinal(decryptedBytes, processLen);
return Arrays.copyOf(decryptedBytes, processLen + doFinalLen);
} catch (final InvalidCipherTextException | DataLengthException x) {
throw new IOException("Could not decrypt bytes", x);
}
}
Aggregations