Search in sources :

Example 56 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter 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 57 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter in project cxf by apache.

the class PbesHmacAesWrapKeyEncryptionAlgorithm method createDerivedKey.

static byte[] createDerivedKey(String keyAlgoJwt, int keySize, byte[] password, byte[] saltInput, int pbesCount) {
    byte[] saltValue = createSaltValue(keyAlgoJwt, saltInput);
    Digest digest = null;
    int macSigSize = PBES_HMAC_MAP.get(keyAlgoJwt);
    if (macSigSize == 256) {
        digest = new SHA256Digest();
    } else if (macSigSize == 384) {
        digest = new SHA384Digest();
    } else {
        digest = new SHA512Digest();
    }
    PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(digest);
    gen.init(password, saltValue, pbesCount);
    return ((KeyParameter) gen.generateDerivedParameters(keySize * 8)).getKey();
}
Also used : SHA512Digest(org.bouncycastle.crypto.digests.SHA512Digest) PKCS5S2ParametersGenerator(org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator) SHA384Digest(org.bouncycastle.crypto.digests.SHA384Digest) Digest(org.bouncycastle.crypto.Digest) SHA256Digest(org.bouncycastle.crypto.digests.SHA256Digest) SHA512Digest(org.bouncycastle.crypto.digests.SHA512Digest) SHA256Digest(org.bouncycastle.crypto.digests.SHA256Digest) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) SHA384Digest(org.bouncycastle.crypto.digests.SHA384Digest)

Example 58 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter 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)

Aggregations

KeyParameter (org.bouncycastle.crypto.params.KeyParameter)58 ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)28 CipherParameters (org.bouncycastle.crypto.CipherParameters)21 AESEngine (org.bouncycastle.crypto.engines.AESEngine)17 GCMBlockCipher (org.bouncycastle.crypto.modes.GCMBlockCipher)16 AEADParameters (org.bouncycastle.crypto.params.AEADParameters)14 IvParameterSpec (javax.crypto.spec.IvParameterSpec)13 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)11 InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)11 AEADBlockCipher (org.bouncycastle.crypto.modes.AEADBlockCipher)10 InvalidKeyException (java.security.InvalidKeyException)9 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)9 CBCBlockCipher (org.bouncycastle.crypto.modes.CBCBlockCipher)9 SecureRandom (java.security.SecureRandom)7 SecretKeySpec (javax.crypto.spec.SecretKeySpec)7 PaddedBufferedBlockCipher (org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher)7 SecretKey (javax.crypto.SecretKey)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)4