Search in sources :

Example 81 with PBEKeySpec

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

the class PBEKeySpecTest method testGetIterationCount.

/**
     * getIterationCount() method testing. Tests that returned value is equal
     * to the value specified in the constructor.
     * Also it checks that the method returns 0 if iterationCount is not
     * specified.
     */
public void testGetIterationCount() {
    char[] password = new char[] { '1', '2', '3', '4', '5' };
    byte[] salt = new byte[] { 1, 2, 3, 4, 5 };
    int iterationCount = 10;
    PBEKeySpec pbeks = new PBEKeySpec(password, salt, iterationCount);
    assertTrue("The returned iterationCount is not equal to the specified " + "in the constructor.", pbeks.getIterationCount() == iterationCount);
    pbeks = new PBEKeySpec(password);
    assertTrue("The getIterationCount() method should return 0 " + "if the iterationCount is not specified.", pbeks.getIterationCount() == 0);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec)

Example 82 with PBEKeySpec

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

the class PBEKeySpecTest method testPBEKeySpec2.

/**
     * PBEKeySpec(char[] password, byte[] salt, int iterationCount, int
     * keyLength) method testing. Tests the behavior of the method in the case
     * of inappropriate parameters and checks that array objects specified as
     * a parameters are copied during the object initialization.
     */
public void testPBEKeySpec2() {
    char[] password = new char[] { '1', '2', '3', '4', '5' };
    byte[] salt = new byte[] { 1, 2, 3, 4, 5 };
    int iterationCount = 10;
    int keyLength = 10;
    try {
        PBEKeySpec pbeks = new PBEKeySpec(null, salt, iterationCount, keyLength);
        assertTrue("An empty char[] should be used in case of null input " + "char array.", pbeks.getPassword().length == 0);
    } catch (IllegalArgumentException e) {
        fail("Unexpected IllegalArgumentException was thrown.");
    } catch (NullPointerException e) {
        fail("Unexpected NullPointerException was thrown.");
    }
    try {
        new PBEKeySpec(password, null, iterationCount, keyLength);
        fail("A NullPointerException should be was thrown " + "in the case of null salt.");
    } catch (IllegalArgumentException e) {
        fail("Unexpected IllegalArgumentException was thrown.");
    } catch (NullPointerException e) {
    }
    try {
        new PBEKeySpec(password, new byte[0], iterationCount, keyLength);
        fail("An IllegalArgumentException should be thrown " + "in the case of empty salt.");
    } catch (IllegalArgumentException e) {
    }
    try {
        new PBEKeySpec(password, salt, -1, keyLength);
        fail("An IllegalArgumentException should be thrown " + "in the case of negative iterationCount.");
    } catch (IllegalArgumentException e) {
    }
    try {
        new PBEKeySpec(password, salt, iterationCount, -1);
        fail("An IllegalArgumentException should be thrown " + "in the case of negative keyLength.");
    } catch (IllegalArgumentException e) {
    }
    try {
        new PBEKeySpec(password, salt, 0, keyLength);
        fail("An IllegalArgumentException should be thrown " + "in the case of zero iterationCount.");
    } catch (IllegalArgumentException e) {
    }
    try {
        new PBEKeySpec(password, salt, iterationCount, 0);
        fail("An IllegalArgumentException should be thrown " + "in the case of zero keyLength.");
    } catch (IllegalArgumentException e) {
    }
    PBEKeySpec pbeks = new PBEKeySpec(password, salt, iterationCount, keyLength);
    password[0]++;
    assertFalse("The change of password specified in the constructor " + "should not cause the change of internal array.", password[0] == pbeks.getPassword()[0]);
    salt[0]++;
    assertFalse("The change of salt specified in the constructor " + " should not cause the change of internal array.", salt[0] == pbeks.getSalt()[0]);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec)

Example 83 with PBEKeySpec

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

the class BcKeyStoreSpi 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) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateEncodingException(java.security.cert.CertificateEncodingException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 84 with PBEKeySpec

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

the class PKCS12KeyStoreSpi 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)

Example 85 with PBEKeySpec

use of javax.crypto.spec.PBEKeySpec in project yyl_example by Relucent.

the class PBEWithMD5AndDES_Encrypt method decrypt.

/**
	 * 将加密文本进行解密
	 * @param encryptText String
	 * @return String
	 */
public String decrypt(String encryptText) throws Exception {
    if (encryptText == null || encryptText.length() == 0) {
        return "";
    }
    PBEKeySpec pbks = new PBEKeySpec((password).toCharArray());
    SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey k = skf.generateSecret(pbks);
    StringTokenizer st = new StringTokenizer(hex2string(encryptText), " ");
    int num = 0;
    byte[] salt = new byte[8];
    while (st.hasMoreTokens() && (num < 8)) {
        salt[num] = (byte) (Integer.parseInt(st.nextToken()));
        num++;
    }
    int count = 0;
    byte[] cbtemp = new byte[2000];
    while (st.hasMoreTokens()) {
        cbtemp[count] = (byte) (Integer.parseInt(st.nextToken()));
        count++;
    }
    byte[] cb = new byte[count];
    for (int i = 0; i < cb.length; i++) {
        cb[i] = cbtemp[i];
    }
    Cipher cp = Cipher.getInstance("PBEWithMD5AndDES");
    PBEParameterSpec ps = new PBEParameterSpec(salt, 1000);
    cp.init(Cipher.DECRYPT_MODE, k, ps);
    byte[] ptext = cp.doFinal(cb);
    return new String(ptext, encoding);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) StringTokenizer(java.util.StringTokenizer) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Aggregations

PBEKeySpec (javax.crypto.spec.PBEKeySpec)110 SecretKeyFactory (javax.crypto.SecretKeyFactory)85 SecretKey (javax.crypto.SecretKey)60 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)39 Cipher (javax.crypto.Cipher)39 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)33 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)27 KeySpec (java.security.spec.KeySpec)26 SecretKeySpec (javax.crypto.spec.SecretKeySpec)17 KeyStoreException (java.security.KeyStoreException)16 IOException (java.io.IOException)15 CertificateException (java.security.cert.CertificateException)12 UnrecoverableKeyException (java.security.UnrecoverableKeyException)11 KeyStore (java.security.KeyStore)10 CertificateEncodingException (java.security.cert.CertificateEncodingException)8 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)8 EncryptedPrivateKeyInfo (javax.crypto.EncryptedPrivateKeyInfo)8 Key (java.security.Key)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 InvalidKeyException (java.security.InvalidKeyException)6