最近在做DES 加密,由于后台是.NET 期间遇到了一些挫折,这里记录一下
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
/**
* Created by WRZ on 2016-12-22.
*/
public class DES {
private final static String DES = "DES";
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);
// 正式执行加密操作
return cipher.doFinal(src);
}
/**
* * @param password 密码 * @param key 加密字符串 * @return
*/
public final static String encrypt(String password, String key) {
try {
return byte2String(encrypt(password.getBytes("UTF-8"), key.getBytes("UTF-8")));
} catch (Exception e) {
}
return null;
}
public static String byte2String(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1) hs = hs + "0" + stmp;
else hs = hs + stmp;
}
return hs.toUpperCase();
}
/**
* * @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);
// 正式执行解密操作
return cipher.doFinal(src);
}
public final static String decrypt(String data, String key) {
try {
return new String(decrypt(String2byte(data.getBytes("UTF-8")), key.getBytes("UTF-8")));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static byte[] String2byte(byte[] b) {
if ((b.length % 2) != 0) throw new IllegalArgumentException("长度不是偶数");
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
}
好了大功告成
date: 123456 key :Red@pass
JAVA: B72ADC72F2C01234 end :123456
NET: 8FB6740C4EA358C2 end :123456
我去,这是啥子情况,为毛和.NET小伙伴加密结果不一样
仔细查了查资料发现
ECB是其中一种字串分割方式,除了DES以外,其他加密方式也会使用这种分割方式的,而Java默认产生的DES算法就是用ECB方法,ECB不需要向量,当然也就不支持向量了
除了ECB,DES还支持CBC、CFB、OFB,而3DES只支持ECB和CBC两种
http://www.tropsoft.com/strongenc/des3.htm
CBC支持并且必须有向量,具体算法这里就不说了。合作商给的.net代码没有声明CBC模式,似乎是.net默认的方式就是CBC的
于是把模式改成CBC
Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
/**
* Created by WRZ on 2016-12-22.
*/
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
public class DesUtils {
//解密数据
public static String decrypt(String message,String key) throws Exception {
byte[] bytesrc =convertHexString(message);
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
byte[] retByte = cipher.doFinal(bytesrc);
return new String(retByte);
}
public static byte[] encrypt(String message, String key)
throws Exception {
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
return cipher.doFinal(message.getBytes("UTF-8"));
}
public static byte[] convertHexString(String ss)
{
byte digest[] = new byte[ss.length() / 2];
for(int i = 0; i < digest.length; i++)
{
String byteString = ss.substring(2 * i, 2 * i + 2);
int byteValue = Integer.parseInt(byteString, 16);
digest[i] = (byte)byteValue;
}
return digest;
}
public static String toHexString(byte b[]) {
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < b.length; i++) {
String plainText = Integer.toHexString(0xff & b[i]);
if (plainText.length() < 2)
plainText = "0" + plainText;
hexString.append(plainText);
}
return hexString.toString();
}
}
这下加密出来的结果就和.NET 小伙伴一样啦