Search in sources :

Example 36 with PBEParameterSpec

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

the class JDKKeyStore method makePBECipher.

protected Cipher makePBECipher(String algorithm, int mode, char[] password, byte[] salt, int iterationCount) throws IOException {
    try {
        PBEKeySpec pbeSpec = new PBEKeySpec(password);
        SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm, BouncyCastleProvider.PROVIDER_NAME);
        PBEParameterSpec defParams = new PBEParameterSpec(salt, iterationCount);
        Cipher cipher = Cipher.getInstance(algorithm, BouncyCastleProvider.PROVIDER_NAME);
        cipher.init(mode, keyFact.generateSecret(pbeSpec), defParams);
        return cipher;
    } catch (Exception e) {
        throw new IOException("Error initialising store of key store: " + e);
    }
}
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) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException) CertificateEncodingException(java.security.cert.CertificateEncodingException)

Example 37 with PBEParameterSpec

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

the class JDKPKCS12KeyStore method calculatePbeMac.

private static byte[] calculatePbeMac(DERObjectIdentifier oid, byte[] salt, int itCount, char[] password, boolean wrongPkcs12Zero, byte[] data) throws Exception {
    SecretKeyFactory keyFact = SecretKeyFactory.getInstance(oid.getId(), bcProvider);
    PBEParameterSpec defParams = new PBEParameterSpec(salt, itCount);
    PBEKeySpec pbeSpec = new PBEKeySpec(password);
    JCEPBEKey key = (JCEPBEKey) keyFact.generateSecret(pbeSpec);
    key.setTryWrongPKCS12Zero(wrongPkcs12Zero);
    Mac mac = Mac.getInstance(oid.getId(), bcProvider);
    mac.init(key, defParams);
    mac.update(data);
    return mac.doFinal();
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec) Mac(javax.crypto.Mac)

Example 38 with PBEParameterSpec

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

the class WrapCipherSpi method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    if (key instanceof JCEPBEKey) {
        JCEPBEKey k = (JCEPBEKey) key;
        if (params instanceof PBEParameterSpec) {
            param = PBE.Util.makePBEParameters(k, params, wrapEngine.getAlgorithmName());
        } else if (k.getParam() != null) {
            param = k.getParam();
        } else {
            throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
        }
    } else {
        param = new KeyParameter(key.getEncoded());
    }
    if (params instanceof javax.crypto.spec.IvParameterSpec) {
        IvParameterSpec iv = (IvParameterSpec) params;
        param = new ParametersWithIV(param, iv.getIV());
    }
    if (param instanceof KeyParameter && ivSize != 0) {
        iv = new byte[ivSize];
        random.nextBytes(iv);
        param = new ParametersWithIV(param, iv);
    }
    switch(opmode) {
        case Cipher.WRAP_MODE:
            wrapEngine.init(true, param);
            break;
        case Cipher.UNWRAP_MODE:
            wrapEngine.init(false, param);
            break;
        case Cipher.ENCRYPT_MODE:
        case Cipher.DECRYPT_MODE:
            throw new IllegalArgumentException("engine only valid for wrapping");
        default:
            System.out.println("eeek!");
    }
}
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) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 39 with PBEParameterSpec

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

the class PBEParameterSpecTest method testGetSalt.

/**
     * getSalt() method testing. Tests that returned salt is equal
     * to the salt specified in the constructor and that the change of
     * returned array does not cause the change of internal array.
     */
public void testGetSalt() {
    byte[] salt = new byte[] { 1, 2, 3, 4, 5 };
    int iterationCount = 10;
    PBEParameterSpec pbeps = new PBEParameterSpec(salt, iterationCount);
    byte[] result = pbeps.getSalt();
    if (!Arrays.equals(salt, result)) {
        fail("The returned salt is not equal to the specified " + "in the constructor.");
    }
    result[0]++;
    assertFalse("The change of returned by getSalt() method salt" + "should not cause the change of internal array.", result[0] == pbeps.getSalt()[0]);
}
Also used : PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 40 with PBEParameterSpec

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

the class PBEParameterSpecTest method testPBEParameterSpec.

/**
     * PBEParameterSpec(byte[] salt, int iterationCount) method testing.
     * Tests the behavior of the method in the case of null input array
     * and tests that input array is copied during the object initialization.
     */
public void testPBEParameterSpec() {
    byte[] salt = { 1, 2, 3, 4, 5 };
    int iterationCount = 10;
    try {
        new PBEParameterSpec(null, iterationCount);
        fail("A NullPointerException should be was thrown " + "in the case of null salt.");
    } catch (NullPointerException e) {
    }
    PBEParameterSpec pbeps = new PBEParameterSpec(salt, iterationCount);
    salt[0]++;
    assertFalse("The change of salt specified in the constructor " + "should not cause the change of internal array.", salt[0] == pbeps.getSalt()[0]);
}
Also used : PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Aggregations

PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)58 SecretKey (javax.crypto.SecretKey)36 Cipher (javax.crypto.Cipher)34 PBEKeySpec (javax.crypto.spec.PBEKeySpec)33 SecretKeyFactory (javax.crypto.SecretKeyFactory)27 IvParameterSpec (javax.crypto.spec.IvParameterSpec)14 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)13 KeyStoreException (java.security.KeyStoreException)11 UnrecoverableKeyException (java.security.UnrecoverableKeyException)11 CertificateException (java.security.cert.CertificateException)11 InvalidKeyException (java.security.InvalidKeyException)10 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)9 CipherParameters (org.bouncycastle.crypto.CipherParameters)9 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)9 ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)9 IOException (java.io.IOException)8 AlgorithmParameters (java.security.AlgorithmParameters)8 SecureRandom (java.security.SecureRandom)8 CertificateEncodingException (java.security.cert.CertificateEncodingException)8 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)8