Search in sources :

Example 71 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project tomcat by apache.

the class SecretKeyCredentialHandler method setAlgorithm.

@Override
public void setAlgorithm(String algorithm) throws NoSuchAlgorithmException {
    SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(algorithm);
    this.secretKeyFactory = secretKeyFactory;
}
Also used : SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 72 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project translationstudio8 by heartsome.

the class DESImpl method decrypt.

/**
	 * 解密.
	 * @param src
	 *            数据源
	 * @param key
	 *            密钥,长度必须是8的倍数
	 * @return byte[]
	 * 			  解密后的原始数据
	 * @throws Exception
	 *             the exception
	 */
public static byte[] decrypt(byte[] src, byte[] key) throws Exception {
    SecureRandom sr = new SecureRandom();
    DESKeySpec dks = new DESKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    Cipher cipher = Cipher.getInstance(DES);
    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 73 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project translationstudio8 by heartsome.

the class DESImpl method encrypt.

/**
	 * 加密.
	 * @param src
	 *            数据源
	 * @param key
	 *            密钥,长度必须是8的倍数
	 * @return byte[]
	 * 			  加密后的数据
	 * @throws Exception
	 *             the exception
	 */
public static byte[] encrypt(byte[] src, byte[] key) throws Exception {
    SecureRandom sr = new SecureRandom();
    DESKeySpec dks = new DESKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    Cipher cipher = Cipher.getInstance(DES);
    cipher.init(Cipher.ENCRYPT_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 74 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project musicbrainz-android by jdamcd.

the class SimpleEncrypt method crypto.

private static String crypto(int mode, String secret, String input) throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
    DESKeySpec keySpec = new DESKeySpec(secret.getBytes(CHARSET));
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
    SecretKey encryptKey = keyFactory.generateSecret(keySpec);
    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(mode, encryptKey);
    if (mode == Cipher.ENCRYPT_MODE) {
        byte[] plainText = input.getBytes(CHARSET);
        return Base64.encodeToString(cipher.doFinal(plainText), Base64.DEFAULT);
    } else if (mode == Cipher.DECRYPT_MODE) {
        byte[] cipherText = Base64.decode(input, Base64.DEFAULT);
        return new String(cipher.doFinal(cipherText), CHARSET);
    }
    return null;
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 75 with SecretKeyFactory

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

the class OSymmetricKey method create.

protected void create() {
    try {
        SecureRandom secureRandom = new SecureRandom();
        byte[] salt = secureRandom.generateSeed(saltLength);
        KeySpec keySpec = new PBEKeySpec(seedPhrase.toCharArray(), salt, iteration, keySize);
        SecretKeyFactory factory = SecretKeyFactory.getInstance(seedAlgorithm);
        SecretKey tempKey = factory.generateSecret(keySpec);
        secretKey = new SecretKeySpec(tempKey.getEncoded(), secretKeyAlgorithm);
    } catch (Exception ex) {
        throw new OSecurityException("OSymmetricKey.create() Exception: " + ex);
    }
}
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) SecureRandom(java.security.SecureRandom) OSecurityException(com.orientechnologies.orient.core.exception.OSecurityException) SecretKeyFactory(javax.crypto.SecretKeyFactory) OException(com.orientechnologies.common.exception.OException) KeyStoreException(java.security.KeyStoreException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) OSecurityException(com.orientechnologies.orient.core.exception.OSecurityException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) BadPaddingException(javax.crypto.BadPaddingException) 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