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);
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations