本文整理汇总了Java中javax.crypto.spec.DESKeySpec类的典型用法代码示例。如果您正苦于以下问题:Java DESKeySpec类的具体用法?Java DESKeySpec怎么用?Java DESKeySpec使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DESKeySpec类属于javax.crypto.spec包,在下文中一共展示了DESKeySpec类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: calculateKeyedChecksum
import javax.crypto.spec.DESKeySpec; //导入依赖的package包/类
/**
* Calculates keyed checksum.
* @param data the data used to generate the checksum.
* @param size length of the data.
* @param key the key used to encrypt the checksum.
* @return keyed checksum.
*
* @modified by Yanni Zhang, 12/08/99.
*/
public byte[] calculateKeyedChecksum(byte[] data, int size, byte[] key,
int usage) throws KrbCryptoException {
//check for weak keys
try {
if (DESKeySpec.isWeak(key, 0)) {
key[7] = (byte)(key[7] ^ 0xF0);
}
} catch (InvalidKeyException ex) {
// swallow, since it should never happen
}
byte[] ivec = new byte[key.length];
System.arraycopy(key, 0, ivec, 0, key.length);
byte[] cksum = Des.des_cksum(ivec, data, key);
return cksum;
}
示例2: encrypt
import javax.crypto.spec.DESKeySpec; //导入依赖的package包/类
/**
* 数据加密,算法(DES)
*
* @param data
* 要进行加密的数据
* @param desKey DES密钥
* @return 加密后的数据
*/
public static String encrypt(String data, byte[] desKey) {
String encryptedData = null;
try {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
DESKeySpec deskey = new DESKeySpec(desKey);
// 创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(deskey);
// 加密对象
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key, sr);
// 加密,并把字节数组编码成字符串
encryptedData = Base64.getEncoder().encodeToString(cipher.doFinal(data.getBytes()));
} catch (Exception e) {
// log.error("加密错误,错误信息:", e);
throw new RuntimeException("加密错误,错误信息:", e);
}
return encryptedData;
}
示例3: decrypt
import javax.crypto.spec.DESKeySpec; //导入依赖的package包/类
/**
* 数据解密,算法(DES)
*
* @param cryptData
* 加密数据
* @param desKey DES密钥
* @return 解密后的数据
*/
public static String decrypt(String cryptData, byte[] desKey) {
String decryptedData = null;
try {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
DESKeySpec deskey = new DESKeySpec(desKey);
// 创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(deskey);
// 解密对象
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key, sr);
// 把字符串解码为字节数组,并解密
decryptedData = new String(cipher.doFinal(Base64.getDecoder().decode(cryptData)));
} catch (Exception e) {
// log.error("解密错误,错误信息:", e);
throw new RuntimeException("解密错误,错误信息:", e);
}
return decryptedData;
}
示例4: encode
import javax.crypto.spec.DESKeySpec; //导入依赖的package包/类
/**
* DES算法,加密
*
* @param data
* 待加密字符串
* @param key
* 加密私钥,长度不能够小于8位
* @return 加密后的字节数组,一般结合Base64编码使用
* @throws Exception
*/
public static String encode(String key, String data) {
if (data == null)
return null;
try {
DESKeySpec dks = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
// key的长度不能够小于8位字节
Key secretKey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
AlgorithmParameterSpec paramSpec = iv;
cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
byte[] bytes = cipher.doFinal(data.getBytes());
return byte2String(bytes);
} catch (Exception e) {
e.printStackTrace();
return data;
}
}
示例5: encode
import javax.crypto.spec.DESKeySpec; //导入依赖的package包/类
/**
* DES算法,加密
*
* @param data 待加密字符串
* @param key 加密私钥,长度不能够小于8位
* @return 加密后的字节数组,一般结合Base64编码使用
* @throws InvalidAlgorithmParameterException
* @throws Exception
*/
public static String encode(String key,String data) {
if(data == null)
return null;
try{
DESKeySpec dks = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
//key的长度不能够小于8位字节
Key secretKey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
IvParameterSpec iv = new IvParameterSpec(key.getBytes());
AlgorithmParameterSpec paramSpec = iv;
cipher.init(Cipher.ENCRYPT_MODE, secretKey,paramSpec);
byte[] bytes = cipher.doFinal(data.getBytes());
return byte2hex(bytes);
}catch(Exception e){
e.printStackTrace();
return data;
}
}
示例6: decode
import javax.crypto.spec.DESKeySpec; //导入依赖的package包/类
/**
* DES算法,解密
*
* @param data 待解密字符串
* @param key 解密私钥,长度不能够小于8位
* @return 解密后的字节数组
* @throws Exception 异常
*/
public static String decode(String key,String data) {
if(data == null)
return null;
try {
DESKeySpec dks = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
//key的长度不能够小于8位字节
Key secretKey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
IvParameterSpec iv = new IvParameterSpec(key.getBytes());
AlgorithmParameterSpec paramSpec = iv;
cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
return new String(cipher.doFinal(hex2byte(data.getBytes())));
} catch (Exception e){
e.printStackTrace();
return data;
}
}
示例7: encrypt
import javax.crypto.spec.DESKeySpec; //导入依赖的package包/类
/**
* 加密
* @param data
* @param password
* @return
*/
public static byte[] encrypt(byte[] data, String password) {
try {
SecureRandom random = new SecureRandom();
DESKeySpec desKey = new DESKeySpec(password.getBytes());
//创建一个密匙工厂,然后用它把DESKeySpec转换成
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(desKey);
//Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance("DES");
//用密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
//现在,获取数据并加密
//正式执行加密操作
return cipher.doFinal(data);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
示例8: decrypt
import javax.crypto.spec.DESKeySpec; //导入依赖的package包/类
/**
* DES
* @param src
* @param password
* @return
*/
public static byte[] decrypt(byte []src, String password) {
try {
// DES算法要求有一个可信任的随机数源
SecureRandom random = new SecureRandom();
// 创建一个DESKeySpec对象
DESKeySpec desKey = new DESKeySpec(password.getBytes());
// 创建一个密匙工厂
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
// 将DESKeySpec对象转换成SecretKey对象
SecretKey securekey = keyFactory.generateSecret(desKey);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance("DES");
// 用密匙初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, random);
// 真正开始解密操作
return cipher.doFinal(src);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
示例9: encrypt
import javax.crypto.spec.DESKeySpec; //导入依赖的package包/类
/**
* DES加密
*
* @author : chenssy
* @date : 2016年5月20日 下午5:51:37
*
* @param data
* 待加密字符串
* @param key
* 校验位
* @return
*/
@SuppressWarnings("restriction")
protected static String encrypt(String data,String key) {
String encryptedData = null;
try {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
DESKeySpec deskey = new DESKeySpec(key.getBytes());
// 创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(deskey);
// 加密对象
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, sr);
// 加密,并把字节数组编码成字符串
encryptedData = new sun.misc.BASE64Encoder().encode(cipher.doFinal(data.getBytes()));
} catch (Exception e) {
throw new RuntimeException("加密错误,错误信息:", e);
}
return encryptedData;
}
示例10: decrypt
import javax.crypto.spec.DESKeySpec; //导入依赖的package包/类
/**
* DES解密
*
* @author : chenssy
* @date : 2016年5月20日 下午5:52:23
*
* @param cryptData
* 待解密密文
* @param key
* 校验位
* @return
*/
@SuppressWarnings("restriction")
protected static String decrypt(String cryptData,String key) {
String decryptedData = null;
try {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
DESKeySpec deskey = new DESKeySpec(key.getBytes());
// 创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(deskey);
// 解密对象
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, secretKey, sr);
// 把字符串解码为字节数组,并解密
decryptedData = new String(cipher.doFinal(new sun.misc.BASE64Decoder().decodeBuffer(cryptData)));
} catch (Exception e) {
throw new RuntimeException("解密错误,错误信息:", e);
}
return decryptedData;
}
示例11: encrypt
import javax.crypto.spec.DESKeySpec; //导入依赖的package包/类
/**
* DES加密
*
* @param src
* @param key
* @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);
}
示例12: decrypt
import javax.crypto.spec.DESKeySpec; //导入依赖的package包/类
/**
* DES解密
*
* @param src
* @param key
* @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);
}
示例13: main
import javax.crypto.spec.DESKeySpec; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
boolean failed = false;
for (int i = 0; i < weakKeys.length; i++) {
DESKeySpec desSpec = new DESKeySpec(weakKeys[i]);
if (!DESKeySpec.isWeak(weakKeys[i], 0)) {
failed = true;
System.out.println("Entry " + i + " should be weak");
}
}
if (failed) {
throw new Exception("Failed test!!!");
}
System.out.println("Passed test.");
}
示例14: decrypt
import javax.crypto.spec.DESKeySpec; //导入依赖的package包/类
/**
* Description 根据键值进行解密
*/
private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
// 生成一个可信任的随机数源
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(data);
}
示例15: desEncrypt
import javax.crypto.spec.DESKeySpec; //导入依赖的package包/类
/**
* Description 根据键值进行加密
*
* @param data
* @param key 加密键byte数组
* @return
* @throws Exception
*/
private static byte[] desEncrypt(byte[] key, byte[] data) throws Exception {
// 生成一个可信任的随机数源
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(data);
}