JAVA语言DES算法
来源:互联网 发布:凡事有偶然的凑巧知乎 编辑:程序博客网 时间:2024/06/01 08:43
1、DES算法:
- /**
- * 加解密算法
- * @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进制字符串
- /**
- * 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数组
- /**
- * 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[i*2], tmp[i*2+1]);
- }
- return ret;
- }
- 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;
- }
执行
- 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
执行
- 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
0 0
- JAVA语言DES算法
- Java.DES算法
- Java DES 算法加密
- java实现DES算法
- DES算法C语言源码
- C语言实现DES算法
- des算法的java实现!
- Java 的DES算法实现
- Java的DES算法实现
- java与C++ DES 算法
- java des加解密算法
- des算法java模拟实现
- DES算法的Java实现
- Linux下c语言实现DES和3DES算法
- Linux下c语言实现DES和3DES算法
- DES算法的C语言代码
- 求des算法的c语言实现
- 关于go语言des加解密算法
- 【MDCC技术大咖秀】Android内存优化之OOM
- d3搭建Apache服务器
- 程序员修炼之道---从小工到专家(第5章)
- OAuth 2.0系列教程(九) 契约请求和响应
- 数据分析的几种经典的理论
- JAVA语言DES算法
- (一) Ionic 环境搭建
- VisualSVN设置提交时必须输入log信息
- Python计算机视觉编程练习2:import 自定义模块
- 解决iOS、Android、Java加解密不一致的问题(DES、AES)
- Turtlebot 学习记录for ros
- 程序员修炼之道---从小工到专家(第6章)
- 感知机学习策略
- 谷歌代码库已超过 20 亿行代码,他们是如何管理的?