Search in sources :

Example 16 with BufferedBlockCipher

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;
}
Also used : BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher)

Example 17 with BufferedBlockCipher

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;
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) AESEngine(org.bouncycastle.crypto.engines.AESEngine) PKCS7Padding(org.bouncycastle.crypto.paddings.PKCS7Padding) BlockCipherPadding(org.bouncycastle.crypto.paddings.BlockCipherPadding) BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher)

Example 18 with BufferedBlockCipher

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);
}
Also used : BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher)

Example 19 with BufferedBlockCipher

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;
}
Also used : ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) AESEngine(org.bouncycastle.crypto.engines.AESEngine) PKCS7Padding(org.bouncycastle.crypto.paddings.PKCS7Padding) BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher)

Example 20 with BufferedBlockCipher

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);
}
Also used : AESEngine(org.bouncycastle.crypto.engines.AESEngine) OFBBlockCipher(org.bouncycastle.crypto.modes.OFBBlockCipher) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) PKCS5S2ParametersGenerator(org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher) BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher) BlockCipher(org.bouncycastle.crypto.BlockCipher) OFBBlockCipher(org.bouncycastle.crypto.modes.OFBBlockCipher) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) SecureRandom(java.security.SecureRandom) CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher) PBEParametersGenerator(org.bouncycastle.crypto.PBEParametersGenerator)

Aggregations

BufferedBlockCipher (org.bouncycastle.crypto.BufferedBlockCipher)24 PaddedBufferedBlockCipher (org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher)19 CBCBlockCipher (org.bouncycastle.crypto.modes.CBCBlockCipher)15 AESEngine (org.bouncycastle.crypto.engines.AESEngine)14 ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)14 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)10 CipherParameters (org.bouncycastle.crypto.CipherParameters)7 SICBlockCipher (org.bouncycastle.crypto.modes.SICBlockCipher)6 InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)5 IOException (java.io.IOException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 SecureRandom (java.security.SecureRandom)4 DataLengthException (org.bouncycastle.crypto.DataLengthException)4 PBEParametersGenerator (org.bouncycastle.crypto.PBEParametersGenerator)4 ECDHBasicAgreement (org.bouncycastle.crypto.agreement.ECDHBasicAgreement)4 SHA256Digest (org.bouncycastle.crypto.digests.SHA256Digest)4 PKCS5S2ParametersGenerator (org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator)4 HMac (org.bouncycastle.crypto.macs.HMac)4 OFBBlockCipher (org.bouncycastle.crypto.modes.OFBBlockCipher)4 AESFastEngine (org.bouncycastle.crypto.engines.AESFastEngine)3