use of javax.crypto.spec.DESKeySpec in project smartmodule by carozhu.
the class DESUtils method encrypt.
/**
* 加密
*
* @param bytesContent
* 待加密内容
* @param key
* 加密的密钥
* @return
*/
public static byte[] encrypt(byte[] bytesContent, String key) {
try {
SecureRandom random = new SecureRandom();
DESKeySpec desKey = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(desKey);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
byte[] result = cipher.doFinal(bytesContent);
return result;
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
use of javax.crypto.spec.DESKeySpec in project yyl_example by Relucent.
the class DesExample method encrypt.
/**
* 根据密匙加密数据*
*/
public static byte[] encrypt(byte[] data, byte[] keydate) throws Exception {
DESKeySpec dks = new DESKeySpec(keydate);
// 创建一个密匙工厂
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(dks);
// Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance("DES");
// 用密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, key, new SecureRandom());
// 执行加密操作
return cipher.doFinal(data);
}
use of javax.crypto.spec.DESKeySpec in project yyl_example by Relucent.
the class DesExample method decode.
public static byte[] decode(byte[] bytes, byte[] keydate) throws Exception {
// 从原始密匙数据创建一个DESKeySpec对象
DESKeySpec dks = new DESKeySpec(keydate);
// 创建一个密匙工厂获得SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(dks);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance("DES");
// 用密匙初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, key, new SecureRandom());
// 执行解密操作
return cipher.doFinal(bytes);
}
use of javax.crypto.spec.DESKeySpec in project newsrob by marianokamp.
the class EntryManager method getSecretKey.
private static SecretKey getSecretKey() throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeySpecException {
DESKeySpec keySpec = new DESKeySpec("EntryManager.class".getBytes("UTF8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(keySpec);
return secretKey;
}
use of javax.crypto.spec.DESKeySpec 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);
}
Aggregations