ITPub博客

首页 > IT职业 > IT生活 > JAVA语言DES算法------

JAVA语言DES算法------

IT生活 作者:fwang3 时间:2021-09-09 08:57:45 0 删除 编辑

1、DES算法:

Java代码 收藏代码

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代码 收藏代码

ublic static String bytes2HexString(byte[] b) {
String ret = "";
for (int i = 0; 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 ret[i] = uniteBytes(tmp[i
      2], 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 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

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4365/viewspace-2807470/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-12

  • 博文量
    0
  • 访问量
    508