public classDesEncrypt {//DES加密密钥key
public static String key = "sdfDA12r3JHV214IJrwerDSO892BK2345nrekk35oewr4wrwrenlklknsdlemifzkw8iiiifegJG7649UJNDFJSvgsfdjFGDFGj435jUhjhjbkajb12kj987gsjh9834tbAXiudhf9B3PM4bt98dyf9Q2m97jjyf417aliD";//DES加密明文plaintext
@SuppressWarnings("static-access")public staticString encryptDES(String plaintext) {try{//首先,DES算法要求有一个可信任的随机数源,可以通过 SecureRandom类,内置两种随机数算法,NativePRNG和SHA1PRNG
SecureRandom random = newSecureRandom();//创建一个DESKeySpec对象
DESKeySpec desKey = newDESKeySpec(key.getBytes());//创建一个密匙工厂
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");//将DESKeySpec对象转换成SecretKey对象
SecretKey securekey =keyFactory.generateSecret(desKey);//Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance("DES");//用密匙初始化Cipher对象
cipher.init(cipher.ENCRYPT_MODE, securekey, random);//加密生成密文byte数组
byte[] cipherBytes =cipher.doFinal(plaintext.getBytes());//将密文byte数组转化为16进制密文
String ciphertext =byteToHex(cipherBytes);returnciphertext;
}catch(Throwable e) {
e.printStackTrace();
}return null;
}//DES解密
@SuppressWarnings("static-access")public staticString decryptDES(String ciphertext) {try{//DES算法要求有一个可信任的随机数源,SecureRandom内置两种随机数算法,NativePRNG和SHA1PRNG,
// 通过new来初始化,默认来说会使用NativePRNG算法生成随机数,但是也可以配置-Djava.security参数来修改调用的算法,
// 如果是/dev/[u]random两者之一就是NativePRNG,否则就是SHA1PRNG。
SecureRandom random = newSecureRandom();//创建一个DESKeySpec对象
DESKeySpec desKey = newDESKeySpec(key.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);//将16进制密文转化为密文byte数组
byte[] cipherBytes =hexToByte(ciphertext);//真正开始解密操作
String plaintext = newString(cipher.doFinal(cipherBytes));returnplaintext;
}catch(Throwable e) {
e.printStackTrace();
}return null;
}//将byte转化为16进制
public static String byteToHex(byte[] bs) {if (0 ==bs.length) {return "";
}else{
StringBuffer sb= newStringBuffer();for (int i = 0; i < bs.length; i++) {
String s= Integer.toHexString(bs[i] & 0xFF);if (1 ==s.length()) {
sb.append("0");
}
sb=sb.append(s.toUpperCase());
}returnsb.toString();
}
}//将16进制转化为byte
public static byte[] hexToByte(String ciphertext) {byte[] cipherBytes =ciphertext.getBytes();if ((cipherBytes.length % 2) != 0) {throw new IllegalArgumentException("长度不为偶数");
}else{byte[] result = new byte[cipherBytes.length / 2];for (int i = 0; i < cipherBytes.length; i += 2) {
String item= new String(cipherBytes, i, 2);
result[i/ 2] = (byte) Integer.parseInt(item, 16);
}returnresult;
}
}
// 进行测试public static voidmain(String[] args) {//当前时间戳
long timestamp =System.currentTimeMillis();
System.out.println(timestamp);//待加密内容
String str = "测试内容:Mr.JimYi" +timestamp;//密码,长度要是8的倍数
String ciphertext =encryptDES(str);
System.out.println("加密后:" +ciphertext);
String plaintext=decryptDES(ciphertext);
System.out.println("解密后:" +plaintext);
String t= plaintext.substring(plaintext.length() - 13,
plaintext.length());
System.out.println(t);
}
}