Search in sources :

Example 1 with PBEParameterSpec

use of javax.crypto.spec.PBEParameterSpec in project Signal-Android by WhisperSystems.

the class MasterSecretUtil method getCipherFromPassphrase.

private static Cipher getCipherFromPassphrase(String passphrase, byte[] salt, int iterations, int opMode) throws GeneralSecurityException {
    SecretKey key = getKeyFromPassphrase(passphrase, salt, iterations);
    Cipher cipher = Cipher.getInstance(key.getAlgorithm());
    cipher.init(opMode, key, new PBEParameterSpec(salt, iterations));
    return cipher;
}
Also used : SecretKey(javax.crypto.SecretKey) Cipher(javax.crypto.Cipher) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 2 with PBEParameterSpec

use of javax.crypto.spec.PBEParameterSpec in project android-pbe by nelenkov.

the class Crypto method encryptPkcs12.

public static String encryptPkcs12(String plaintext, SecretKey key, byte[] salt) {
    try {
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, ITERATION_COUNT);
        cipher.init(Cipher.ENCRYPT_MODE, key, pbeSpec);
        Log.d(TAG, "Cipher IV: " + toHex(cipher.getIV()));
        byte[] cipherText = cipher.doFinal(plaintext.getBytes("UTF-8"));
        return String.format("%s%s%s", toBase64(salt), DELIMITER, toBase64(cipherText));
    } catch (GeneralSecurityException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Cipher(javax.crypto.Cipher) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 3 with PBEParameterSpec

use of javax.crypto.spec.PBEParameterSpec in project XobotOS by xamarin.

the class JCEMac 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 JCEPBEKey) {
        JCEPBEKey k = (JCEPBEKey) 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 4 with PBEParameterSpec

use of javax.crypto.spec.PBEParameterSpec in project XobotOS by xamarin.

the class JDKPKCS12KeyStore method unwrapKey.

protected PrivateKey unwrapKey(AlgorithmIdentifier algId, byte[] data, char[] password, boolean wrongPKCS12Zero) throws IOException {
    String algorithm = algId.getObjectId().getId();
    PKCS12PBEParams pbeParams = new PKCS12PBEParams((ASN1Sequence) algId.getParameters());
    PBEKeySpec pbeSpec = new PBEKeySpec(password);
    PrivateKey out;
    try {
        SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm, bcProvider);
        PBEParameterSpec defParams = new PBEParameterSpec(pbeParams.getIV(), pbeParams.getIterations().intValue());
        SecretKey k = keyFact.generateSecret(pbeSpec);
        ((JCEPBEKey) k).setTryWrongPKCS12Zero(wrongPKCS12Zero);
        Cipher cipher = Cipher.getInstance(algorithm, bcProvider);
        cipher.init(Cipher.UNWRAP_MODE, k, defParams);
        // we pass "" as the key algorithm type as it is unknown at this point
        out = (PrivateKey) cipher.unwrap(data, "", Cipher.PRIVATE_KEY);
    } catch (Exception e) {
        throw new IOException("exception unwrapping private key - " + e.toString());
    }
    return out;
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) PrivateKey(java.security.PrivateKey) PKCS12PBEParams(org.bouncycastle.asn1.pkcs.PKCS12PBEParams) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) DERBMPString(org.bouncycastle.asn1.DERBMPString) BERConstructedOctetString(org.bouncycastle.asn1.BERConstructedOctetString) DEROctetString(org.bouncycastle.asn1.DEROctetString) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec) 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)

Example 5 with PBEParameterSpec

use of javax.crypto.spec.PBEParameterSpec in project XobotOS by xamarin.

the class JDKPKCS12KeyStore method wrapKey.

protected byte[] wrapKey(String algorithm, Key key, PKCS12PBEParams pbeParams, char[] password) throws IOException {
    PBEKeySpec pbeSpec = new PBEKeySpec(password);
    byte[] out;
    try {
        SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm, bcProvider);
        PBEParameterSpec defParams = new PBEParameterSpec(pbeParams.getIV(), pbeParams.getIterations().intValue());
        Cipher cipher = Cipher.getInstance(algorithm, bcProvider);
        cipher.init(Cipher.WRAP_MODE, keyFact.generateSecret(pbeSpec), defParams);
        out = cipher.wrap(key);
    } catch (Exception e) {
        throw new IOException("exception encrypting data - " + e.toString());
    }
    return out;
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec) 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)

Aggregations

PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)101 SecretKey (javax.crypto.SecretKey)72 Cipher (javax.crypto.Cipher)65 PBEKeySpec (javax.crypto.spec.PBEKeySpec)59 SecretKeyFactory (javax.crypto.SecretKeyFactory)51 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)19 IvParameterSpec (javax.crypto.spec.IvParameterSpec)18 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)17 InvalidKeyException (java.security.InvalidKeyException)17 KeyStoreException (java.security.KeyStoreException)14 UnrecoverableKeyException (java.security.UnrecoverableKeyException)14 CertificateException (java.security.cert.CertificateException)14 AlgorithmParameters (java.security.AlgorithmParameters)12 SecureRandom (java.security.SecureRandom)12 CipherParameters (org.bouncycastle.crypto.CipherParameters)12 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)12 ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)12 IOException (java.io.IOException)11 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)9 Key (java.security.Key)8