Search in sources :

Example 86 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter in project Skein3Fish by wernerd.

the class SkeinMac method init.

public void init(CipherParameters params) throws IllegalArgumentException {
    ParametersForSkein p = (ParametersForSkein) params;
    KeyParameter kp = (KeyParameter) (p.getParameters());
    skein = new Skein(p.getStateSize(), p.getMacSize(), 0, kp.getKey());
    Xsave = skein.getState();
}
Also used : Skein(org.bouncycastle.crypto.digests.Skein) ParametersForSkein(org.bouncycastle.crypto.params.ParametersForSkein) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) ParametersForSkein(org.bouncycastle.crypto.params.ParametersForSkein)

Example 87 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter in project elastic-core-maven by OrdinaryDude.

the class Crypto method aesGCMDecrypt.

public static byte[] aesGCMDecrypt(byte[] ivCiphertext, byte[] key) {
    try {
        if (ivCiphertext.length < 16) {
            throw new InvalidCipherTextException("invalid ivCiphertext length");
        }
        byte[] iv = Arrays.copyOfRange(ivCiphertext, 0, 16);
        byte[] ciphertext = Arrays.copyOfRange(ivCiphertext, 16, ivCiphertext.length);
        GCMBlockCipher aes = new GCMBlockCipher(new AESEngine());
        CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
        aes.init(false, ivAndKey);
        byte[] output = new byte[aes.getOutputSize(ciphertext.length)];
        int plaintextLength = aes.processBytes(ciphertext, 0, ciphertext.length, output, 0);
        plaintextLength += aes.doFinal(output, plaintextLength);
        byte[] result = new byte[plaintextLength];
        System.arraycopy(output, 0, result, 0, result.length);
        return result;
    } catch (InvalidCipherTextException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) AESEngine(org.bouncycastle.crypto.engines.AESEngine) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher)

Example 88 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter in project elastic-core-maven by OrdinaryDude.

the class Crypto method aesEncrypt.

public static byte[] aesEncrypt(byte[] plaintext, byte[] key) {
    try {
        byte[] iv = new byte[16];
        secureRandom.get().nextBytes(iv);
        PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
        CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
        aes.init(true, ivAndKey);
        byte[] output = new byte[aes.getOutputSize(plaintext.length)];
        int ciphertextLength = aes.processBytes(plaintext, 0, plaintext.length, output, 0);
        ciphertextLength += aes.doFinal(output, ciphertextLength);
        byte[] result = new byte[iv.length + ciphertextLength];
        System.arraycopy(iv, 0, result, 0, iv.length);
        System.arraycopy(output, 0, result, iv.length, ciphertextLength);
        return result;
    } catch (InvalidCipherTextException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
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) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher)

Example 89 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter in project elastic-core-maven by OrdinaryDude.

the class Crypto method aesGCMEncrypt.

public static byte[] aesGCMEncrypt(byte[] plaintext, byte[] key) {
    try {
        byte[] iv = new byte[16];
        secureRandom.get().nextBytes(iv);
        GCMBlockCipher aes = new GCMBlockCipher(new AESEngine());
        CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
        aes.init(true, ivAndKey);
        byte[] output = new byte[aes.getOutputSize(plaintext.length)];
        int ciphertextLength = aes.processBytes(plaintext, 0, plaintext.length, output, 0);
        ciphertextLength += aes.doFinal(output, ciphertextLength);
        byte[] result = new byte[iv.length + ciphertextLength];
        System.arraycopy(iv, 0, result, 0, iv.length);
        System.arraycopy(output, 0, result, iv.length, ciphertextLength);
        return result;
    } catch (InvalidCipherTextException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) AESEngine(org.bouncycastle.crypto.engines.AESEngine) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher)

Example 90 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);
    final Digest digest;
    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)

Aggregations

KeyParameter (org.bouncycastle.crypto.params.KeyParameter)119 ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)51 AESEngine (org.bouncycastle.crypto.engines.AESEngine)37 CipherParameters (org.bouncycastle.crypto.CipherParameters)35 GCMBlockCipher (org.bouncycastle.crypto.modes.GCMBlockCipher)24 AEADParameters (org.bouncycastle.crypto.params.AEADParameters)22 CBCBlockCipher (org.bouncycastle.crypto.modes.CBCBlockCipher)21 PaddedBufferedBlockCipher (org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher)21 IvParameterSpec (javax.crypto.spec.IvParameterSpec)19 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)16 InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)16 AEADBlockCipher (org.bouncycastle.crypto.modes.AEADBlockCipher)16 InvalidKeyException (java.security.InvalidKeyException)13 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)12 SecretKeySpec (javax.crypto.spec.SecretKeySpec)12 BufferedBlockCipher (org.bouncycastle.crypto.BufferedBlockCipher)12 PKCS5S2ParametersGenerator (org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator)12 SecureRandom (java.security.SecureRandom)11 SecretKey (javax.crypto.SecretKey)10 BlockCipher (org.bouncycastle.crypto.BlockCipher)8