Search in sources :

Example 1 with BlockCipherPadding

use of org.bouncycastle.crypto.paddings.BlockCipherPadding in project sentinel-android by Samourai-Wallet.

the class AESUtil method decrypt.

// AES 256 PBKDF2 CBC iso10126 decryption
// 16 byte IV must be prepended to ciphertext - Compatible with crypto-js
public static String decrypt(String ciphertext, CharSequenceX password, int iterations) {
    final int AESBlockSize = 4;
    byte[] cipherdata = Base64.decodeBase64(ciphertext.getBytes());
    // Seperate the IV and cipher data
    byte[] iv = copyOfRange(cipherdata, 0, AESBlockSize * 4);
    byte[] input = copyOfRange(cipherdata, AESBlockSize * 4, cipherdata.length);
    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);
    // setup AES cipher in CBC mode with PKCS7 padding
    BlockCipherPadding padding = new ISO10126d2Padding();
    BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), padding);
    cipher.reset();
    cipher.init(false, params);
    // create a temporary buffer to decode into (includes padding)
    byte[] buf = new byte[cipher.getOutputSize(input.length)];
    int len = cipher.processBytes(input, 0, input.length, buf, 0);
    try {
        len += cipher.doFinal(buf, len);
    } catch (InvalidCipherTextException icte) {
        icte.printStackTrace();
        return null;
    }
    // remove padding
    byte[] out = new byte[len];
    System.arraycopy(buf, 0, out, 0, len);
    // return string representation of decoded bytes
    String ret = null;
    try {
        ret = new String(out, "UTF-8");
    } catch (UnsupportedEncodingException uee) {
        uee.printStackTrace();
        return null;
    }
    return ret;
}
Also used : PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) AESEngine(org.bouncycastle.crypto.engines.AESEngine) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) PKCS5S2ParametersGenerator(org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ISO10126d2Padding(org.bouncycastle.crypto.paddings.ISO10126d2Padding) CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) BlockCipherPadding(org.bouncycastle.crypto.paddings.BlockCipherPadding) BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher) PBEParametersGenerator(org.bouncycastle.crypto.PBEParametersGenerator)

Example 2 with BlockCipherPadding

use of org.bouncycastle.crypto.paddings.BlockCipherPadding in project sentinel-android by Samourai-Wallet.

the class AESUtil method encrypt.

public static String encrypt(String cleartext, CharSequenceX password, int iterations) {
    final int AESBlockSize = 4;
    if (password == null) {
        return 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 = null;
    try {
        clearbytes = cleartext.getBytes("UTF-8");
    } catch (UnsupportedEncodingException uee) {
        uee.printStackTrace();
        return null;
    }
    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);
    // setup AES cipher in CBC mode with PKCS7 padding
    BlockCipherPadding padding = new ISO10126d2Padding();
    BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), padding);
    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);
    byte[] raw = Base64.encodeBase64(ivAppended);
    String ret = new String(raw);
    return ret;
}
Also used : PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) AESEngine(org.bouncycastle.crypto.engines.AESEngine) PKCS5S2ParametersGenerator(org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) SecureRandom(java.security.SecureRandom) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ISO10126d2Padding(org.bouncycastle.crypto.paddings.ISO10126d2Padding) CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) BlockCipherPadding(org.bouncycastle.crypto.paddings.BlockCipherPadding) BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher) PBEParametersGenerator(org.bouncycastle.crypto.PBEParametersGenerator)

Example 3 with BlockCipherPadding

use of org.bouncycastle.crypto.paddings.BlockCipherPadding 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 4 with BlockCipherPadding

use of org.bouncycastle.crypto.paddings.BlockCipherPadding in project vsDiaryWriter by shilongdai.

the class BlockCiphers method getEncryptor.

public static BlockCipherEncryptor getEncryptor(String cipher, String mode, String padding) {
    StringBuilder sb = new StringBuilder();
    sb.append(cipher).append("/").append(mode).append("/").append(padding);
    String type = sb.toString();
    BlockCipherEncryptor bc = cache.get(type);
    if (bc == null) {
        BlockCipher engine = getBlockCipherEngine(cipher);
        BlockCipher modeEngine = wrapBlockCipherMode(engine, mode);
        BlockCipherPadding pad = getBlockCipherPadding(padding);
        BCBlockCipherBuilder builder = new BCBlockCipherBuilder();
        builder.setBlockSize(engine.getBlockSize()).setKeySize(getKeySize(cipher)).setCipher(modeEngine).setPadding(pad);
        bc = builder.build();
        cache.put(type, bc);
    }
    return bc;
}
Also used : CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher) CFBBlockCipher(org.bouncycastle.crypto.modes.CFBBlockCipher) BlockCipher(org.bouncycastle.crypto.BlockCipher) OFBBlockCipher(org.bouncycastle.crypto.modes.OFBBlockCipher) SICBlockCipher(org.bouncycastle.crypto.modes.SICBlockCipher) BlockCipherPadding(org.bouncycastle.crypto.paddings.BlockCipherPadding) BCBlockCipherBuilder(net.viperfish.journal.secureAlgs.BCBlockCipherEncryptor.BCBlockCipherBuilder)

Aggregations

CBCBlockCipher (org.bouncycastle.crypto.modes.CBCBlockCipher)4 BlockCipherPadding (org.bouncycastle.crypto.paddings.BlockCipherPadding)4 BufferedBlockCipher (org.bouncycastle.crypto.BufferedBlockCipher)3 CipherParameters (org.bouncycastle.crypto.CipherParameters)3 AESEngine (org.bouncycastle.crypto.engines.AESEngine)3 PaddedBufferedBlockCipher (org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher)3 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)3 ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 PBEParametersGenerator (org.bouncycastle.crypto.PBEParametersGenerator)2 PKCS5S2ParametersGenerator (org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator)2 ISO10126d2Padding (org.bouncycastle.crypto.paddings.ISO10126d2Padding)2 SecureRandom (java.security.SecureRandom)1 BCBlockCipherBuilder (net.viperfish.journal.secureAlgs.BCBlockCipherEncryptor.BCBlockCipherBuilder)1 BlockCipher (org.bouncycastle.crypto.BlockCipher)1 InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)1 CFBBlockCipher (org.bouncycastle.crypto.modes.CFBBlockCipher)1 OFBBlockCipher (org.bouncycastle.crypto.modes.OFBBlockCipher)1 SICBlockCipher (org.bouncycastle.crypto.modes.SICBlockCipher)1 PKCS7Padding (org.bouncycastle.crypto.paddings.PKCS7Padding)1