Search in sources :

Example 41 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project jdk8u_jdk by JetBrains.

the class PBKDF2TranslateTest method testMyOwnSecretKey.

/**
     * The test case scenario implemented in the method: - derive Key1 for the
     * given PBKDF2 algorithm - create my own secret Key2 as an instance of a
     * class implements PBEKey - translate Key2 - check if the key value of the
     * translated key and Key1 are the same.
     */
private void testMyOwnSecretKey(byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException {
    SecretKey key1 = getSecretKeyForPBKDF2(algoForTest, salt);
    SecretKey key2 = getMyOwnSecretKey(salt);
    // Is it actually the same?
    if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) {
        throw new RuntimeException("We shouldn't be here. The key1 and key2 values in its" + " primary encoding format have to be the same!");
    }
    // translate key
    SecretKeyFactory skf = SecretKeyFactory.getInstance(algoForTest);
    SecretKey key3 = skf.translateKey(key2);
    // Check if it still the same after translation
    if (!Arrays.equals(key1.getEncoded(), key3.getEncoded())) {
        System.out.println("Key1=" + new String(key1.getEncoded()) + " key3=" + new String(key3.getEncoded()) + " salt=" + new String(salt));
        throw new RuntimeException("testMyOwnSecretKey test case failed: the key1  and key3" + " values in its primary encoding format are not" + " the same for " + algoForTest + " algorithm.");
    }
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 42 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project jdk8u_jdk by JetBrains.

the class PBKDF2TranslateTest method getSecretKeyForPBKDF2.

/**
     * Generate a PBKDF2 secret key using given algorithm.
     */
private SecretKey getSecretKeyForPBKDF2(String algoDeriveKey, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException {
    SecretKeyFactory skf = SecretKeyFactory.getInstance(algoDeriveKey);
    PBEKeySpec spec = new PBEKeySpec(PASS_PHRASE.toCharArray(), salt, ITERATION_COUNT, KEY_SIZE);
    return skf.generateSecret(spec);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 43 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project jdk8u_jdk by JetBrains.

the class PBKDF2TranslateTest method generateAndTranslateKey.

/**
     * The test case scenario implemented in the method: - derive PBKDF2 key
     * using the given algorithm; - translate the key - check if the translated
     * and original keys have the same key value.
     *
     */
public void generateAndTranslateKey(byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException {
    // derive PBKDF2 key
    SecretKey key1 = getSecretKeyForPBKDF2(algoForTest, salt);
    // translate key
    SecretKeyFactory skf = SecretKeyFactory.getInstance(algoForTest);
    SecretKey key2 = skf.translateKey(key1);
    // Check if it still the same after translation
    if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) {
        System.out.println("Key1=" + new String(key1.getEncoded()) + " key2=" + new String(key2.getEncoded()) + " salt=" + new String(salt));
        throw new RuntimeException("generateAndTranslateKey test case failed: the  key1 and" + " key2 values in its primary encoding format are" + " not the same for " + algoForTest + " algorithm.");
    }
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 44 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project jdk8u_jdk by JetBrains.

the class MyPBKDF2SecretKey method generateAndTranslateKey.

/**
     * The test case scenario implemented in the method: - derive PBKDF2 key
     * using the given algorithm; - translate the key - check if the translated
     * and original keys have the same key value.
     *
     * @return true if the test case passed; false - otherwise.
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeySpecException
     * @throws InvalidKeyException
     */
public boolean generateAndTranslateKey() throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException {
    // derive PBKDF2 key
    SecretKey key1 = getSecretKeyForPBKDF2(algoToTest);
    // translate key
    SecretKeyFactory skf = SecretKeyFactory.getInstance(algoToTest);
    SecretKey key2 = skf.translateKey(key1);
    // check if it still the same after translation
    if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) {
        System.err.println("generateAndTranslateKey test case failed: the " + "key1 and key2 values in its primary encoding format are " + "not the same for " + algoToTest + "algorithm.");
        return false;
    }
    return true;
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 45 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project jdk8u_jdk by JetBrains.

the class MyPBKDF2SecretKey method getSecretKeyForPBKDF2.

/**
     * Generate a PBKDF2 secret key using given algorithm.
     *
     * @param algoToDeriveKey PBKDF2 algorithm
     * @return PBKDF2 secret key
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeySpecException
     */
private SecretKey getSecretKeyForPBKDF2(String algoToDeriveKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
    SecretKeyFactory skf = SecretKeyFactory.getInstance(algoToDeriveKey);
    PBEKeySpec spec = new PBEKeySpec(PASS_PHRASE.toCharArray(), this.salt, ITERATION_COUNT, KEY_SIZE);
    return skf.generateSecret(spec);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Aggregations

SecretKeyFactory (javax.crypto.SecretKeyFactory)129 SecretKey (javax.crypto.SecretKey)84 PBEKeySpec (javax.crypto.spec.PBEKeySpec)75 Cipher (javax.crypto.Cipher)58 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)39 DESKeySpec (javax.crypto.spec.DESKeySpec)28 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)26 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)26 KeySpec (java.security.spec.KeySpec)25 SecretKeySpec (javax.crypto.spec.SecretKeySpec)23 SecureRandom (java.security.SecureRandom)18 KeyStoreException (java.security.KeyStoreException)16 IOException (java.io.IOException)15 InvalidKeyException (java.security.InvalidKeyException)14 PrivateKey (java.security.PrivateKey)12 CertificateException (java.security.cert.CertificateException)12 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)12 UnrecoverableKeyException (java.security.UnrecoverableKeyException)11 Key (java.security.Key)10 KeyFactory (java.security.KeyFactory)10