网上查了下关于ECB和CBC加密,只有一些图文介绍,没有详细步骤,现在在这里记录一下自己的使用过程。
参考资料:http://blog.csdn.net/aaaaatiger/article/details/2525561
需要详细了解的朋友请自行查找其它资料,我只在这里介绍一下中间加密器的过程,以16字节密钥为例。
一、DES加密和解密
/**
* DES加密
*
*/
public static byte[] encryptDes(byte[] key, byte[] src) {
try {
// 创建一个DESKeySpec对象
DESKeySpec desKey = new DESKeySpec(key);
// 创建一个密匙工厂
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
// 将DESKeySpec对象转换成SecretKey对象
SecretKey secretKey = keyFactory.generateSecret(desKey);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
// 用密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
// 现在,获取数据并加密
// 正式执行加密操作
return cipher.doFinal(src);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* des解密
*
* @param key
* @param src
* @return
*/
public static byte[] decryptDes(byte[] key, byte[] src) {
try {
// DES算法要求有一个可信任的随机数源
SecureRandom random = new SecureRandom();
// 创建一个DESKeySpec对象
DESKeySpec desKey = new DESKeySpec(key);
// 创建一个密匙工厂
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
// 将DESKeySpec对象转换成SecretKey对象
SecretKey secretKey = keyFactory.generateSecret(desKey);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
// 用密匙初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, secretKey, random);
// 现在,获取数据并加密
// 正式执行加密操作
return cipher.doFinal(src);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
二、ECB加密
/**
*加密
*/
public static String encryptECB3Des(String key, String src) {
System.out.println("encryptECB3Des->" + "key:" + key);
System.out.println("encryptECB3Des->" + "src:" + src);
int len = key.length();
if (key == null || src == null) {
return null;
}
if (src.length() % 16 != 0) {
return null;
}
if (len == 32) {
String outData = "";
String str = "";
for (int i = 0; i < src.length() / 16; i++) {
str = src.substring(i * 16, (i + 1) * 16);
outData += encECB3Des(key, str);
}
return outData;
}
return null;
}
public static String encECB3Des(String key, String src) {
byte[] temp = null;
byte[] temp1 = null;
temp1 = encryptDes(stringToHexBytes(key.substring(0, 16)), stringToHexBytes(src));
temp = decryptDes(stringToHexBytes(key.substring(16, 32)), temp1);
temp1 = encryptDes(stringToHexBytes(key.substring(0, 16)), temp);
return byte2HexString(temp1, "");
}
public static String decECB3Des(String key, String src) {
byte[] temp2 = decryptDes(stringToHexBytes(key.substring(0, 16)), stringToHexBytes(src));
byte[] temp1 = encryptDes(stringToHexBytes(key.substring(16, 32)), temp2);
byte[] dest = decryptDes(stringToHexBytes(key.substring(0, 16)), temp1);
return byte2HexString(dest, "");
}
/**
* 3DES(双倍长) 解密
*
* @param keybyte
* @param src
* @return
*/
public static String decryptECB3Des(String key, String src) {
if (key == null || src == null) {
return null;
}
if (src.length() % 16 != 0) {
return null;
}
if (key.length() == 32) {
String outData = "";
String str = "";
for (int i = 0; i < src.length() / 16; i++) {
str = src.substring(i * 16, (i + 1) * 16);
outData += decECB3Des(key, str);
}
return outData;
}
return null;
}
2、使用密钥的后8个字节对temp1进行des解密得到temp2。
3、使用密钥的前8个字节对temp2做des加密得到最后的密文。
代码中间有几处转换,转换原理为“0x11“转换成字符串“11”,和将字符串“11”转换成字节“0x11”。
三、CBC加密和解密
public static String encryptCBC3Des(String key, String src) {
String IV = "0000000000000000";
return encryptCBC3Des(IV, key, src);
}
/**
* 3DES(双倍长) 加密
*
* @param keybyte
* @param src
* @return
*/
public static String encryptCBC3Des(String IV, String key, String src) {
String outData = "";
String enc = IV;
byte[] strBs = null;
byte[] encBs = null;
byte[] b2 = new byte[8];
for (int i = 0; i < src.length() / 16; i++) {
strBs = stringToHexBytes(src.substring(i * 16, (i + 1) * 16));
encBs = stringToHexBytes(enc);
for (int j = 0; j < b2.length; j++) {
b2[j] = (byte) (encBs[j] ^ strBs[j]);
}
enc = encECB3Des(key, byte2HexString(b2, ""));
outData += enc;
}
return outData;
}
public static String decryptCBC3Des(String key, String src) {
String IV = "0000000000000000";
return decryptCBC3Des(IV, key, src);
}
/**
* 3DES(双倍长) 解密
*
* @param keybyte
* @param src
* @return
*/
public static String decryptCBC3Des(String IV, String key, String src) {
String outData = "";
String enc = IV;
String str = "";
byte[] encBs = null;
byte[] decBs = null;
byte[] b2 = new byte[8];
for (int i = 0; i < src.length() / 16; i++) {
str = src.substring(i * 16, (i + 1) * 16);
decBs = stringToHexBytes(decECB3Des(key, str));
encBs = stringToHexBytes(enc);
for (int j = 0; j < b2.length; j++) {
b2[j] = (byte) (encBs[j] ^ decBs[j]);
}
enc = str;
outData += byte2HexString(b2, "");
}
return outData;