1、DES算法:
Java代码 收藏代码
/**
- 加解密算法
- @param data 加解密数据
- @param key 秘钥
- @param mode 模式
-
@return 加解密结果
*/
public static byte[] desCryt(byte[] data, byte[] key, int mode){
byte[] result = null ;
try {
SecureRandom sr = new SecureRandom();
SecretKeyFactory keyFactory;
DESKeySpec dks = new DESKeySpec(key);
keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretkey = keyFactory.generateSecret(dks);
//创建Cipher对象
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
//初始化Cipher对象
cipher.init(mode, secretkey, sr);
//加解密
result = cipher.doFinal(data);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}return result;
}2、byte数组转换成16进制字符串
Java代码 收藏代码
/**
- byte数组转换成16进制字符串
- @param b
- @return
*/
ublic static String bytes2HexString(byte[] b) {
String ret = "";
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += hex.toUpperCase();
}
return ret;
3、16进制字符串转成byte数组
Java代码 收藏代码
/**
- 16进制字符串转成byte数组
- @param src
- @return
/
ublic static byte[] hexString2Bytes(String src){
byte[] ret = new byte[8];
byte[] tmp = src.getBytes();
for(int i=0; i<8; i++){
ret[i] = uniteBytes(tmp[i2], tmp[i*2+1]);
}
return ret;
}
Java代码 收藏代码
public static byte uniteBytes(byte src0, byte src1) {
byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
_b0 = (byte)(_b0 << 4);
byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
byte ret = (byte)(_b0 ^ _b1);
return ret;
}
执行
Java代码 收藏代码
public static void main(String[] args) {
//加解密模式
int mode = Cipher.ENCRYPT_MODE;
//被加解密byte数组16进制字符串
String dataHexString = "1234567887654321";
//秘钥byte数组16进制字符串
String keyHexString = "9AAB1D2EE004AAC3";
byte[] data = hexString2Bytes(dataHexString);
byte[] key = hexString2Bytes(keyHexString);
byte[] result = desCryt(data, key, mode);
//打印结果
System.out.println("结果:"+bytes2HexString(result));
}
结果:7D592BF239849E76
执行
Java代码 收藏代码
public static void main(String[] args) {
//加解密模式
int mode = Cipher.DECRYPT_MODE;
//被加解密byte数组16进制字符串
String dataHexString = "7D592BF239849E76";
//秘钥byte数组16进制字符串
String keyHexString = "9AAB1D2EE004AAC3";
byte[] data = hexString2Bytes(dataHexString);
byte[] key = hexString2Bytes(keyHexString);
byte[] result = desCryt(data, key, mode);
//打印结果
System.out.println("结果:"+bytes2HexString(result));
}
结果:1234567887654321
PS:
获取Cipher对象的时候一定要写成
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
不要写成
Cipher cipher = Cipher.getInstance("DES");
否则解密的时候会报错:
Given final block not properly padded
原因是Cipher cipher = Cipher.getInstance("DES");与Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");等同,填充方式错误,加密的时候会得到16长度的字节数组。
JCE详解传送门:http://docs.oracle.com/javase/1.5.0/docs/guide/security/jce/JCERefGuide.html