Search in sources :

Example 26 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project JamsMusicPlayer by psaravan.

the class Base64SharedPreferences method initCiphers.

protected void initCiphers(String secureKey) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException, InvalidAlgorithmParameterException {
    IvParameterSpec ivSpec = getIv();
    SecretKeySpec secretKey = getSecretKey(secureKey);
    writer.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
    reader.init(Cipher.DECRYPT_MODE, secretKey, ivSpec);
    keyWriter.init(Cipher.ENCRYPT_MODE, secretKey);
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec)

Example 27 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project ratpack by ratpack.

the class DefaultCrypto method decrypt.

@Override
public ByteBuf decrypt(ByteBuf message, ByteBufAllocator allocator) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, ShortBufferException, IllegalBlockSizeException {
    Cipher cipher = Cipher.getInstance(algorithm);
    if (isInitializationVectorRequired) {
        int ivByteLength = message.readByte();
        byte[] iv = new byte[ivByteLength];
        message.readBytes(iv);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(iv));
    } else {
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
    }
    int messageLength = message.readableBytes();
    ByteBuf decMessage = allocator.buffer(cipher.getOutputSize(messageLength));
    ByteBuf byteBuf = message.readBytes(messageLength);
    try {
        int count = cipher.doFinal(byteBuf.nioBuffer(), decMessage.nioBuffer(0, messageLength));
        if (!hasPadding) {
            for (int i = count - 1; i >= 0; i--) {
                if (decMessage.getByte(i) == 0x00) {
                    count--;
                } else {
                    break;
                }
            }
        }
        decMessage.writerIndex(count);
        return decMessage;
    } catch (Exception e) {
        decMessage.release();
        throw e;
    } finally {
        byteBuf.release();
    }
}
Also used : IvParameterSpec(javax.crypto.spec.IvParameterSpec) ByteBuf(io.netty.buffer.ByteBuf) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException)

Example 28 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project robovm by robovm.

the class PKCS12KeyStoreSpi method unwrapKey.

protected PrivateKey unwrapKey(AlgorithmIdentifier algId, byte[] data, char[] password, boolean wrongPKCS12Zero) throws IOException {
    ASN1ObjectIdentifier algorithm = algId.getAlgorithm();
    try {
        if (algorithm.on(PKCSObjectIdentifiers.pkcs_12PbeIds)) {
            PKCS12PBEParams pbeParams = PKCS12PBEParams.getInstance(algId.getParameters());
            PBEKeySpec pbeSpec = new PBEKeySpec(password);
            PrivateKey out;
            SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm.getId(), bcProvider);
            PBEParameterSpec defParams = new PBEParameterSpec(pbeParams.getIV(), pbeParams.getIterations().intValue());
            SecretKey k = keyFact.generateSecret(pbeSpec);
            ((BCPBEKey) k).setTryWrongPKCS12Zero(wrongPKCS12Zero);
            Cipher cipher = Cipher.getInstance(algorithm.getId(), bcProvider);
            cipher.init(Cipher.UNWRAP_MODE, k, defParams);
            // we pass "" as the key algorithm type as it is unknown at this point
            return (PrivateKey) cipher.unwrap(data, "", Cipher.PRIVATE_KEY);
        } else if (algorithm.equals(PKCSObjectIdentifiers.id_PBES2)) {
            PBES2Parameters alg = PBES2Parameters.getInstance(algId.getParameters());
            PBKDF2Params func = PBKDF2Params.getInstance(alg.getKeyDerivationFunc().getParameters());
            SecretKeyFactory keyFact = SecretKeyFactory.getInstance(alg.getKeyDerivationFunc().getAlgorithm().getId(), bcProvider);
            SecretKey k = keyFact.generateSecret(new PBEKeySpec(password, func.getSalt(), func.getIterationCount().intValue(), SecretKeyUtil.getKeySize(alg.getEncryptionScheme().getAlgorithm())));
            Cipher cipher = Cipher.getInstance(alg.getEncryptionScheme().getAlgorithm().getId(), bcProvider);
            cipher.init(Cipher.UNWRAP_MODE, k, new IvParameterSpec(ASN1OctetString.getInstance(alg.getEncryptionScheme().getParameters()).getOctets()));
            // we pass "" as the key algorithm type as it is unknown at this point
            return (PrivateKey) cipher.unwrap(data, "", Cipher.PRIVATE_KEY);
        }
    } catch (Exception e) {
        throw new IOException("exception unwrapping private key - " + e.toString());
    }
    throw new IOException("exception unwrapping private key - cannot recognise: " + algorithm);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) PBES2Parameters(org.bouncycastle.asn1.pkcs.PBES2Parameters) PrivateKey(java.security.PrivateKey) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateEncodingException(java.security.cert.CertificateEncodingException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SecretKey(javax.crypto.SecretKey) PKCS12PBEParams(org.bouncycastle.asn1.pkcs.PKCS12PBEParams) BCPBEKey(org.bouncycastle.jcajce.provider.symmetric.util.BCPBEKey) PBKDF2Params(org.bouncycastle.asn1.pkcs.PBKDF2Params) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 29 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project robovm by robovm.

the class BaseMac method engineInit.

protected void engineInit(Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    if (key == null) {
        throw new InvalidKeyException("key is null");
    }
    if (key instanceof BCPBEKey) {
        BCPBEKey k = (BCPBEKey) key;
        if (k.getParam() != null) {
            param = k.getParam();
        } else if (params instanceof PBEParameterSpec) {
            param = PBE.Util.makePBEMacParameters(k, params);
        } else {
            throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
        }
    } else if (params instanceof IvParameterSpec) {
        param = new ParametersWithIV(new KeyParameter(key.getEncoded()), ((IvParameterSpec) params).getIV());
    } else if (params == null) {
        param = new KeyParameter(key.getEncoded());
    } else {
        throw new InvalidAlgorithmParameterException("unknown parameter type.");
    }
    macEngine.init(param);
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) IvParameterSpec(javax.crypto.spec.IvParameterSpec) InvalidKeyException(java.security.InvalidKeyException) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 30 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project robovm by robovm.

the class OpenSSLCipher method engineInit.

@Override
protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    final byte[] iv;
    if (params instanceof IvParameterSpec) {
        IvParameterSpec ivParams = (IvParameterSpec) params;
        iv = ivParams.getIV();
    } else {
        iv = null;
    }
    engineInitInternal(opmode, key, iv, random);
}
Also used : IvParameterSpec(javax.crypto.spec.IvParameterSpec)

Aggregations

IvParameterSpec (javax.crypto.spec.IvParameterSpec)202 Cipher (javax.crypto.Cipher)130 SecretKeySpec (javax.crypto.spec.SecretKeySpec)96 SecretKey (javax.crypto.SecretKey)45 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)44 InvalidKeyException (java.security.InvalidKeyException)40 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)37 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)36 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)29 BadPaddingException (javax.crypto.BadPaddingException)27 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)23 KeyGenerator (javax.crypto.KeyGenerator)19 Key (java.security.Key)18 SecureRandom (java.security.SecureRandom)17 IOException (java.io.IOException)15 MyCipher (org.apache.harmony.crypto.tests.support.MyCipher)15 GeneralSecurityException (java.security.GeneralSecurityException)13 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)13 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)13 UnsupportedEncodingException (java.io.UnsupportedEncodingException)11