Search in sources :

Example 11 with PKCS5S2ParametersGenerator

use of org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator 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)

Example 12 with PKCS5S2ParametersGenerator

use of org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator in project web3sdk by FISCO-BCOS.

the class Wallet method generateAes128CtrDerivedKey.

private static byte[] generateAes128CtrDerivedKey(byte[] password, byte[] salt, int c, String prf) throws CipherException {
    if (!prf.equals("hmac-sha256")) {
        throw new CipherException("Unsupported prf:" + prf);
    }
    // Java 8 supports this, but you have to convert the password to a character array, see
    // http://stackoverflow.com/a/27928435/3211687
    PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA256Digest());
    gen.init(password, salt, c);
    return ((KeyParameter) gen.generateDerivedParameters(256)).getKey();
}
Also used : PKCS5S2ParametersGenerator(org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator) SHA256Digest(org.bouncycastle.crypto.digests.SHA256Digest) KeyParameter(org.bouncycastle.crypto.params.KeyParameter)

Example 13 with PKCS5S2ParametersGenerator

use of org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator in project cloudstack by apache.

the class PBKDF2UserAuthenticator method encode.

public String encode(String password, byte[] salt, int rounds) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeySpecException {
    PKCS5S2ParametersGenerator generator = new PKCS5S2ParametersGenerator();
    generator.init(PBEParametersGenerator.PKCS5PasswordToBytes(password.toCharArray()), salt, rounds);
    return format("%s:%s:%d", encode(salt), encode(((KeyParameter) generator.generateDerivedParameters(s_keylen)).getKey()), rounds);
}
Also used : PKCS5S2ParametersGenerator(org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator) KeyParameter(org.bouncycastle.crypto.params.KeyParameter)

Aggregations

PKCS5S2ParametersGenerator (org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator)13 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)10 PBEParametersGenerator (org.bouncycastle.crypto.PBEParametersGenerator)7 CipherParameters (org.bouncycastle.crypto.CipherParameters)5 BufferedBlockCipher (org.bouncycastle.crypto.BufferedBlockCipher)4 SHA256Digest (org.bouncycastle.crypto.digests.SHA256Digest)4 AESEngine (org.bouncycastle.crypto.engines.AESEngine)4 CBCBlockCipher (org.bouncycastle.crypto.modes.CBCBlockCipher)4 PaddedBufferedBlockCipher (org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher)4 ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 SecureRandom (java.security.SecureRandom)2 SecretKeySpec (javax.crypto.spec.SecretKeySpec)2 BlockCipher (org.bouncycastle.crypto.BlockCipher)2 OpenSSLPBEParametersGenerator (org.bouncycastle.crypto.generators.OpenSSLPBEParametersGenerator)2 OFBBlockCipher (org.bouncycastle.crypto.modes.OFBBlockCipher)2 BlockCipherPadding (org.bouncycastle.crypto.paddings.BlockCipherPadding)2 ISO10126d2Padding (org.bouncycastle.crypto.paddings.ISO10126d2Padding)2 SecretKey (javax.crypto.SecretKey)1 PBKDF2Params (org.bouncycastle.asn1.pkcs.PBKDF2Params)1