Search in sources :

Example 26 with KeySpec

use of java.security.spec.KeySpec in project felix by apache.

the class CryptoServiceSingleton method generateAESKey.

/**
 * Generate the AES key from the salt and the private key.
 *
 * @param salt       the salt (hexadecimal)
 * @param privateKey the private key
 * @return the generated key.
 */
private SecretKey generateAESKey(String privateKey, String salt) {
    try {
        byte[] raw = Hex.decodeHex(salt.toCharArray());
        KeySpec spec = new PBEKeySpec(privateKey.toCharArray(), raw, iterationCount, keySize);
        SecretKeyFactory factory = SecretKeyFactory.getInstance(PBKDF_2_WITH_HMAC_SHA_1);
        return new SecretKeySpec(factory.generateSecret(spec).getEncoded(), AES_ECB_ALGORITHM);
    } catch (DecoderException e) {
        throw new IllegalStateException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(e);
    } catch (InvalidKeySpecException e) {
        throw new IllegalStateException(e);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) DecoderException(org.apache.commons.codec.DecoderException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Example 27 with KeySpec

use of java.security.spec.KeySpec in project android by nextcloud.

the class EncryptionUtils method decryptPrivateKey.

/**
 * Decrypt private key with symmetric AES encryption, GCM mode mode and no padding
 *
 * @param privateKey byte64 encoded string representation of private key, IV separated with "|"
 * @param keyPhrase  key used for encryption, e.g. 12 random words
 *                   {@link EncryptionUtils#getRandomWords(int, Context)}
 * @return decrypted string
 */
public static String decryptPrivateKey(String privateKey, String keyPhrase) throws NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, InvalidKeySpecException, InvalidAlgorithmParameterException {
    // split up iv, salt
    String[] strings = privateKey.split(ivDelimiter);
    String realPrivateKey = strings[0];
    byte[] iv = decodeStringToBase64Bytes(strings[1]);
    byte[] salt = decodeStringToBase64Bytes(strings[2]);
    Cipher cipher = Cipher.getInstance(AES_CIPHER);
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    KeySpec spec = new PBEKeySpec(keyPhrase.toCharArray(), salt, iterationCount, keyStrength);
    SecretKey tmp = factory.generateSecret(spec);
    SecretKeySpec key = new SecretKeySpec(tmp.getEncoded(), AES);
    cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
    byte[] bytes = decodeStringToBase64Bytes(realPrivateKey);
    byte[] decrypted = cipher.doFinal(bytes);
    String pemKey = decodeBase64BytesToString(decrypted);
    return pemKey.replaceAll("\n", "").replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", "");
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 28 with KeySpec

use of java.security.spec.KeySpec in project pentaho-kettle by pentaho.

the class CertificateGenEncryptUtil method decodeTransmittedKey.

public static Key decodeTransmittedKey(byte[] sessionKey, byte[] transmittedKey, boolean privateKey) throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    KeySpec keySpec = null;
    Key keyKey = null;
    if (transmittedKey == null || sessionKey == null) {
        return null;
    }
    if (!privateKey) {
        keySpec = new X509EncodedKeySpec(sessionKey);
        keyKey = KeyFactory.getInstance(PUBLIC_KEY_ALGORITHM).generatePublic(keySpec);
    } else {
        keySpec = new PKCS8EncodedKeySpec(sessionKey);
        keyKey = KeyFactory.getInstance(PUBLIC_KEY_ALGORITHM).generatePrivate(keySpec);
    }
    Cipher keyCipher = Cipher.getInstance(TRANSMISSION_CIPHER_PARAMS);
    keyCipher.init(Cipher.UNWRAP_MODE, keyKey);
    return keyCipher.unwrap(transmittedKey, SINGLE_KEY_ALGORITHM, Cipher.SECRET_KEY);
}
Also used : PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) SecretKeySpec(javax.crypto.spec.SecretKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeySpec(java.security.spec.KeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) Cipher(javax.crypto.Cipher) Key(java.security.Key)

Example 29 with KeySpec

use of java.security.spec.KeySpec in project chuidiang-ejemplos by chuidiang.

the class RSAAsymetricCrypto method loadPrivateKey.

private static PrivateKey loadPrivateKey(String fileName) throws Exception {
    FileInputStream fis = new FileInputStream(fileName);
    int numBtyes = fis.available();
    byte[] bytes = new byte[numBtyes];
    fis.read(bytes);
    fis.close();
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    KeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
    PrivateKey keyFromBytes = keyFactory.generatePrivate(keySpec);
    return keyFromBytes;
}
Also used : PrivateKey(java.security.PrivateKey) KeySpec(java.security.spec.KeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) FileInputStream(java.io.FileInputStream) KeyFactory(java.security.KeyFactory)

Example 30 with KeySpec

use of java.security.spec.KeySpec in project chuidiang-ejemplos by chuidiang.

the class RSAAsymetricCrypto method loadPublicKey.

private static PublicKey loadPublicKey(String fileName) throws Exception {
    FileInputStream fis = new FileInputStream(fileName);
    int numBtyes = fis.available();
    byte[] bytes = new byte[numBtyes];
    fis.read(bytes);
    fis.close();
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    KeySpec keySpec = new X509EncodedKeySpec(bytes);
    PublicKey keyFromBytes = keyFactory.generatePublic(keySpec);
    return keyFromBytes;
}
Also used : PublicKey(java.security.PublicKey) KeySpec(java.security.spec.KeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) FileInputStream(java.io.FileInputStream) KeyFactory(java.security.KeyFactory)

Aggregations

KeySpec (java.security.spec.KeySpec)149 PBEKeySpec (javax.crypto.spec.PBEKeySpec)66 SecretKeyFactory (javax.crypto.SecretKeyFactory)62 KeyFactory (java.security.KeyFactory)46 SecretKeySpec (javax.crypto.spec.SecretKeySpec)46 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)39 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)35 SecretKey (javax.crypto.SecretKey)35 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)34 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)28 BigInteger (java.math.BigInteger)25 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)25 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)23 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)21 PrivateKey (java.security.PrivateKey)19 Cipher (javax.crypto.Cipher)16 IOException (java.io.IOException)15 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)14 PublicKey (java.security.PublicKey)13 InvalidKeyException (java.security.InvalidKeyException)12