Search in sources :

Example 86 with SecretKeyFactory

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

the class mySecretKeyFactory method testSecretKeyFactory09.

/**
     * Test for <code>getInstance(String algorithm, Provider provider)</code>
     * method
     * Assertion: returns SecretKeyFactory object
     */
public void testSecretKeyFactory09() throws NoSuchAlgorithmException {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    for (int i = 0; i < validValues.length; i++) {
        SecretKeyFactory secKF = SecretKeyFactory.getInstance(validValues[i], defaultProvider);
        assertEquals("Incorrect algorithm", secKF.getAlgorithm(), validValues[i]);
        assertEquals("Incorrect provider", secKF.getProvider(), defaultProvider);
    }
}
Also used : SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 87 with SecretKeyFactory

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

the class mySecretKeyFactory method testSecretKeyFactory03.

/**
     * Test for <code>getInstance(String algorithm)</code> method
     * Assertion: returns SecretKeyObject
     */
public void testSecretKeyFactory03() throws NoSuchAlgorithmException {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    for (int i = 0; i < validValues.length; i++) {
        SecretKeyFactory secKF = SecretKeyFactory.getInstance(validValues[i]);
        assertEquals("Incorrect algorithm", secKF.getAlgorithm(), validValues[i]);
    }
}
Also used : SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 88 with SecretKeyFactory

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

the class EncryptUtil 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 89 with SecretKeyFactory

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

the class EncryptUtil method encrypt.

/**
     * ????
     * 
     * @param src ????
     * @param key ???????????????8
     * @return ????????????
     * @throws Exception
     */
public static byte[] encrypt(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.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 90 with SecretKeyFactory

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

the class DESCipher method encrypt.

/**
	 *   * 加密
	 *
	 *   *
	 *
	 *   * @param src
	 *
	 *   * 明文(字节)
	 *
	 *   * @param key
	 *
	 *   * 密钥,长度必须是8的倍数
	 *
	 *   * @return 密文(字节)
	 *
	 *   * @throws Exception
	 *
	 *   
	 */
public static byte[] encrypt(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.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)

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