Search in sources :

Example 16 with SecretKey

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

the class SecretUtil method encrypt3DES.

/**
     * 加密 DESede<br>
     * 用密钥加密
     *
     * @param data 裸的原始数据
     * @param key  加密的密钥
     * @return 结果也采用base64加密
     * @throws Exception
     */
public static String encrypt3DES(String data, String key) {
    try {
        // 生成密钥
        SecretKey desKey = new SecretKeySpec(build3DesKey(key), KEY_ALGORITHM_3DES);
        // 对数据加密
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_3DES);
        cipher.init(Cipher.ENCRYPT_MODE, desKey);
        return encryptBASE64(cipher.doFinal(data.getBytes(ENCODING)));
    } catch (Exception e) {
        throw DataXException.asDataXException(FrameworkErrorCode.SECRET_ERROR, "3重DES加密出错", e);
    }
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) DataXException(com.alibaba.datax.common.exception.DataXException)

Example 17 with SecretKey

use of javax.crypto.SecretKey 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 18 with SecretKey

use of javax.crypto.SecretKey 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)

Example 19 with SecretKey

use of javax.crypto.SecretKey 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 20 with SecretKey

use of javax.crypto.SecretKey in project Android-Terminal-Emulator by jackpal.

the class ShortcutEncryption method generateKeys.

/**
     * Generates new secret keys suitable for the encryption scheme described
     * above.
     *
     * @throws GeneralSecurityException if an error occurs during key generation.
     */
public static Keys generateKeys() throws GeneralSecurityException {
    KeyGenerator gen = KeyGenerator.getInstance(ENC_ALGORITHM);
    gen.init(KEYLEN);
    SecretKey encKey = gen.generateKey();
    /* XXX: It's probably unnecessary to create a different keygen for the
         * MAC, but JCA's API design suggests we should just in case ... */
    gen = KeyGenerator.getInstance(MAC_ALGORITHM);
    gen.init(KEYLEN);
    SecretKey macKey = gen.generateKey();
    return new Keys(encKey, macKey);
}
Also used : SecretKey(javax.crypto.SecretKey) KeyGenerator(javax.crypto.KeyGenerator)

Aggregations

SecretKey (javax.crypto.SecretKey)437 Cipher (javax.crypto.Cipher)160 SecretKeySpec (javax.crypto.spec.SecretKeySpec)127 KeyGenerator (javax.crypto.KeyGenerator)112 SecretKeyFactory (javax.crypto.SecretKeyFactory)83 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)78 SecureRandom (java.security.SecureRandom)58 InvalidKeyException (java.security.InvalidKeyException)57 PBEKeySpec (javax.crypto.spec.PBEKeySpec)53 IvParameterSpec (javax.crypto.spec.IvParameterSpec)42 IOException (java.io.IOException)41 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)34 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)32 KeyStore (java.security.KeyStore)30 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)30 Test (org.junit.Test)30 BadPaddingException (javax.crypto.BadPaddingException)29 PrivateKey (java.security.PrivateKey)28 Mac (javax.crypto.Mac)28 GeneralSecurityException (java.security.GeneralSecurityException)26