Search in sources :

Example 21 with PBEKeySpec

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

the class CipherTest method getEncryptKey.

private static synchronized Key getEncryptKey(String algorithm) throws Exception {
    Key key = ENCRYPT_KEYS.get(algorithm);
    if (key != null) {
        return key;
    }
    if (algorithm.startsWith("RSA")) {
        KeyFactory kf = KeyFactory.getInstance("RSA");
        RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
        key = kf.generatePrivate(keySpec);
    } else if (isPBE(algorithm)) {
        SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);
        key = skf.generateSecret(new PBEKeySpec("secret".toCharArray()));
    } else {
        KeyGenerator kg = KeyGenerator.getInstance(getBaseAlgorithm(algorithm));
        key = kg.generateKey();
    }
    ENCRYPT_KEYS.put(algorithm, key);
    return key;
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyGenerator(javax.crypto.KeyGenerator) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PublicKey(java.security.PublicKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) SecretKey(javax.crypto.SecretKey) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory)

Example 22 with PBEKeySpec

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

the class CipherPBEThread method crypt.

@Override
public void crypt() throws Exception {
    byte[] output = new byte[128];
    byte[] decrypted = new byte[128];
    byte[] input = getData().getBytes();
    byte[] salt = new byte[8];
    SecureRandom sr = new SecureRandom();
    PBEKeySpec keySpec = new PBEKeySpec("top sicret password".toCharArray());
    SecretKeyFactory skf = SecretKeyFactory.getInstance(getAlgName());
    SecretKey key = skf.generateSecret(keySpec);
    Cipher cip = Cipher.getInstance(getAlgName() + "/" + getMode() + "/" + getPadding());
    sr.nextBytes(salt);
    PBEParameterSpec parSpec = new PBEParameterSpec(salt, getKeyLength());
    cip.init(Cipher.ENCRYPT_MODE, key, parSpec);
    cip.doFinal(input, 0, input.length, output);
    int outputSize = cip.getOutputSize(input.length);
    cip.init(Cipher.DECRYPT_MODE, key, parSpec);
    cip.doFinal(output, 0, outputSize, decrypted);
    checkEncodedData(getData().getBytes(), decrypted);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) SecureRandom(java.security.SecureRandom) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 23 with PBEKeySpec

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

the class SecretKeyFactoryThread method test.

@Override
public void test() throws Exception {
    SecretKeyFactory skf = SecretKeyFactory.getInstance(algName);
    byte[] b = new byte[24];
    KeySpec ks = (KeySpec) ((algName == "DES") ? new DESKeySpec(b) : (algName == "DESede") ? new DESedeKeySpec(b) : new PBEKeySpec("passw".toCharArray()));
    skf.generateSecret(ks);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) KeySpec(java.security.spec.KeySpec) DESKeySpec(javax.crypto.spec.DESKeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) DESKeySpec(javax.crypto.spec.DESKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 24 with PBEKeySpec

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

the class PBEKeySpecTest 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.
     * Also it checks that the method returns null if salt is not
     * specified.
     */
public void testGetSalt() {
    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);
    byte[] result = pbeks.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] == pbeks.getSalt()[0]);
    pbeks = new PBEKeySpec(password);
    assertNull("The getSalt() method should return null if the salt " + "is not specified.", pbeks.getSalt());
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec)

Example 25 with PBEKeySpec

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

the class PBEKeySpecTest method testGetPassword.

/**
     * getPassword() method testing. Tests that returned password is equal
     * to the password specified in the constructor and that the change of
     * returned array does not cause the change of internal array.
     */
public void testGetPassword() {
    char[] password = new char[] { '1', '2', '3', '4', '5' };
    PBEKeySpec pbeks = new PBEKeySpec(password);
    char[] result = pbeks.getPassword();
    if (!Arrays.equals(password, result)) {
        fail("The returned password is not equal to the specified " + "in the constructor.");
    }
    result[0]++;
    assertFalse("The change of returned by getPassword() method password " + "should not cause the change of internal array.", result[0] == pbeks.getPassword()[0]);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec)

Aggregations

PBEKeySpec (javax.crypto.spec.PBEKeySpec)249 SecretKeyFactory (javax.crypto.SecretKeyFactory)190 SecretKey (javax.crypto.SecretKey)118 Cipher (javax.crypto.Cipher)82 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)73 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)63 KeySpec (java.security.spec.KeySpec)59 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)59 SecretKeySpec (javax.crypto.spec.SecretKeySpec)49 IOException (java.io.IOException)25 KeyStoreException (java.security.KeyStoreException)23 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)22 EncryptedPrivateKeyInfo (javax.crypto.EncryptedPrivateKeyInfo)17 CertificateException (java.security.cert.CertificateException)15 GeneralSecurityException (java.security.GeneralSecurityException)14 UnrecoverableKeyException (java.security.UnrecoverableKeyException)14 AlgorithmParameters (java.security.AlgorithmParameters)13 Key (java.security.Key)13 KeyStore (java.security.KeyStore)13 InvalidKeyException (java.security.InvalidKeyException)12