Search in sources :

Example 6 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project DataX by alibaba.

the class DESCipher method decrypt.

/**
	 *   * 解密
	 *
	 *   *
	 *
	 *   * @param src
	 *
	 *   * 密文(字节)
	 *
	 *   * @param key
	 *
	 *   * 密钥,长度必须是8的倍数
	 *
	 *   * @return 明文(字节)
	 *
	 *   * @throws Exception
	 *
	 *   
	 */
public static byte[] decrypt(byte[] src, byte[] key) throws Exception {
    // DES算法要求有一个可信任的随机数源
    SecureRandom sr = new SecureRandom();
    // 从原始密匙数据创建一个DESKeySpec对象
    DESKeySpec dks = new DESKeySpec(key);
    // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成
    // 一个SecretKey对象
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    // Cipher对象实际完成解密操作
    Cipher cipher = Cipher.getInstance(DES);
    // 用密匙初始化Cipher对象
    cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
    return cipher.doFinal(src);
}
Also used : SecretKey(javax.crypto.SecretKey) SecureRandom(java.security.SecureRandom) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 7 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project android-pbe by nelenkov.

the class Crypto method deriveKeyPkcs12.

public static SecretKey deriveKeyPkcs12(byte[] salt, String password) {
    try {
        long start = System.currentTimeMillis();
        KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, ITERATION_COUNT, KEY_LENGTH);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(PKCS12_DERIVATION_ALGORITHM);
        SecretKey result = keyFactory.generateSecret(keySpec);
        long elapsed = System.currentTimeMillis() - start;
        Log.d(TAG, String.format("PKCS#12 key derivation took %d [ms].", elapsed));
        return result;
    } catch (GeneralSecurityException e) {
        throw new RuntimeException(e);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 8 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project android-pbe by nelenkov.

the class Crypto method deriveKeyPbkdf2.

public static SecretKey deriveKeyPbkdf2(byte[] salt, String password) {
    try {
        long start = System.currentTimeMillis();
        KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, ITERATION_COUNT, KEY_LENGTH);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(PBKDF2_DERIVATION_ALGORITHM);
        byte[] keyBytes = keyFactory.generateSecret(keySpec).getEncoded();
        Log.d(TAG, "key bytes: " + toHex(keyBytes));
        SecretKey result = new SecretKeySpec(keyBytes, "AES");
        long elapsed = System.currentTimeMillis() - start;
        Log.d(TAG, String.format("PBKDF2 key derivation took %d [ms].", elapsed));
        return result;
    } catch (GeneralSecurityException e) {
        throw new RuntimeException(e);
    }
}
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) GeneralSecurityException(java.security.GeneralSecurityException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 9 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project netty by netty.

the class SslContext method generateKeySpec.

/**
     * Generates a key specification for an (encrypted) private key.
     *
     * @param password characters, if {@code null} an unencrypted key is assumed
     * @param key bytes of the DER encoded private key
     *
     * @return a key specification
     *
     * @throws IOException if parsing {@code key} fails
     * @throws NoSuchAlgorithmException if the algorithm used to encrypt {@code key} is unkown
     * @throws NoSuchPaddingException if the padding scheme specified in the decryption algorithm is unkown
     * @throws InvalidKeySpecException if the decryption key based on {@code password} cannot be generated
     * @throws InvalidKeyException if the decryption key based on {@code password} cannot be used to decrypt
     *                             {@code key}
     * @throws InvalidAlgorithmParameterException if decryption algorithm parameters are somehow faulty
     */
protected static PKCS8EncodedKeySpec generateKeySpec(char[] password, byte[] key) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException, InvalidKeyException, InvalidAlgorithmParameterException {
    if (password == null) {
        return new PKCS8EncodedKeySpec(key);
    }
    EncryptedPrivateKeyInfo encryptedPrivateKeyInfo = new EncryptedPrivateKeyInfo(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(encryptedPrivateKeyInfo.getAlgName());
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec);
    Cipher cipher = Cipher.getInstance(encryptedPrivateKeyInfo.getAlgName());
    cipher.init(Cipher.DECRYPT_MODE, pbeKey, encryptedPrivateKeyInfo.getAlgParameters());
    return encryptedPrivateKeyInfo.getKeySpec(cipher);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 10 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project orientdb by orientechnologies.

the class OSecurityManager method getPbkdf2.

private byte[] getPbkdf2(final String iPassword, final byte[] salt, final int iterations, final int bytes, final String algorithm) {
    String cacheKey = null;
    final String hashedPassword = createSHA256(iPassword + new String(salt));
    if (SALT_CACHE != null) {
        // SEARCH IN CACHE FIRST
        cacheKey = hashedPassword + "|" + Arrays.toString(salt) + "|" + iterations + "|" + bytes;
        final byte[] encoded = SALT_CACHE.get(cacheKey);
        if (encoded != null)
            return encoded;
    }
    final PBEKeySpec spec = new PBEKeySpec(iPassword.toCharArray(), salt, iterations, bytes * 8);
    final SecretKeyFactory skf;
    try {
        skf = SecretKeyFactory.getInstance(algorithm);
        final byte[] encoded = skf.generateSecret(spec).getEncoded();
        if (SALT_CACHE != null) {
            // SAVE IT IN CACHE
            SALT_CACHE.put(cacheKey, encoded);
        }
        return encoded;
    } catch (Exception e) {
        throw OException.wrapException(new OSecurityException("Cannot create a key with '" + algorithm + "' algorithm"), e);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) OSecurityException(com.orientechnologies.orient.core.exception.OSecurityException) SecretKeyFactory(javax.crypto.SecretKeyFactory) OSecurityException(com.orientechnologies.orient.core.exception.OSecurityException) OConfigurationException(com.orientechnologies.orient.core.exception.OConfigurationException) OException(com.orientechnologies.common.exception.OException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

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