use of org.bouncycastle.crypto.BufferedBlockCipher in project nem2-sdk-java by nemtech.
the class Ed25519BlockCipher method encrypt.
@Override
public byte[] encrypt(final byte[] input) {
// Setup salt.
final byte[] salt = new byte[this.keyLength];
this.random.nextBytes(salt);
// Derive shared key.
final byte[] sharedKey = this.getSharedKey(this.senderKeyPair.getPrivateKey(), this.recipientKeyPair.getPublicKey(), salt);
// Setup IV.
final byte[] ivData = new byte[16];
this.random.nextBytes(ivData);
// Setup block cipher.
final BufferedBlockCipher cipher = this.setupBlockCipher(sharedKey, ivData, true);
// Encode.
final byte[] buf = this.transform(cipher, input);
if (null == buf) {
return null;
}
final byte[] result = new byte[salt.length + ivData.length + buf.length];
System.arraycopy(salt, 0, result, 0, salt.length);
System.arraycopy(ivData, 0, result, salt.length, ivData.length);
System.arraycopy(buf, 0, result, salt.length + ivData.length, buf.length);
return result;
}
use of org.bouncycastle.crypto.BufferedBlockCipher in project nem2-sdk-java by nemtech.
the class Ed25519BlockCipher method setupBlockCipher.
private BufferedBlockCipher setupBlockCipher(final byte[] sharedKey, final byte[] ivData, final boolean forEncryption) {
// Setup cipher parameters with key and IV.
final KeyParameter keyParam = new KeyParameter(sharedKey);
final CipherParameters params = new ParametersWithIV(keyParam, ivData);
// Setup AES cipher in CBC mode with PKCS7 padding.
final BlockCipherPadding padding = new PKCS7Padding();
final BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), padding);
cipher.reset();
cipher.init(forEncryption, params);
return cipher;
}
use of org.bouncycastle.crypto.BufferedBlockCipher in project nem2-sdk-java by nemtech.
the class Ed25519BlockCipher method decrypt.
@Override
public byte[] decrypt(final byte[] input) {
if (input.length < 64) {
return null;
}
final byte[] salt = Arrays.copyOfRange(input, 0, this.keyLength);
final byte[] ivData = Arrays.copyOfRange(input, this.keyLength, 48);
final byte[] encData = Arrays.copyOfRange(input, 48, input.length);
// Derive shared key.
final byte[] sharedKey = this.getSharedKey(this.recipientKeyPair.getPrivateKey(), this.senderKeyPair.getPublicKey(), salt);
// Setup block cipher.
final BufferedBlockCipher cipher = this.setupBlockCipher(sharedKey, ivData, false);
// Decode.
return this.transform(cipher, encData);
}
use of org.bouncycastle.crypto.BufferedBlockCipher in project photon-model by vmware.
the class EncryptorService method getCipher.
/*
* Cipher settings
*/
private BufferedBlockCipher getCipher(boolean forEncryption) {
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
cipher.init(forEncryption, new ParametersWithIV(new KeyParameter(this.keyBytes, IV_LENGTH, this.keyBytes.length - IV_LENGTH), this.keyBytes, 0, IV_LENGTH));
return cipher;
}
use of org.bouncycastle.crypto.BufferedBlockCipher 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);
}
Aggregations