DES加解密工具类
这两天在跟友商对接接口,在对外暴露接口的时候,因为友商不需要登录即可访问对于系统来说存在安全隐患,所以需要友商在调用接口的时候需要将数据加密,系统解密验证后才执行业务。所有的加密方式并不是万能的,只是增加了破解的成本高低而已~~
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.SecureRandom;
public class DesEncryptUtils {
public static final String KEY = "marklogzhu202306111454";
/**
* 加密
*
* @param content
* 需要加密的内容
* @param password
* 加密密码
* @return
*/
public static byte[] encrypt(String content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(password.getBytes());
kgen.init(128, random);
// kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(byteContent);
return result; // 加密
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 解密
*
* @param content
* 待解密内容
* @param password
* 解密密钥
* @return
*/
public static byte[] decrypt(byte[] content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(password.getBytes());
kgen.init(128, random);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(content);
return result; // 加密
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将二进制转换成16进制
*
* @param buf
* @return
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/**
* 将16进制转换为二进制
*
* @param hexStr
* @return
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
/**
* 加密
*
* @param content
* 需要加密的内容
* @param password
* 加密密码
* @return
*/
public static byte[] encrypt2(String content, String password) {
try {
SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(byteContent);
return result; // 加密
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String getDecryptResult(String bizData) throws UnsupportedEncodingException {
byte[] decode = DesEncryptUtils.parseHexStr2Byte(bizData);
byte[] decryptResult = DesEncryptUtils.decrypt(decode, DesEncryptUtils.KEY);
String decryptStr = new String(decryptResult, "UTF-8");
return decryptStr;
}
public static void main(String[] args) throws UnsupportedEncodingException {
String content = "hello 中国--2018";
// 加密
System.out.println("加密前:" + content);
byte[] encode = DesEncryptUtils.encrypt(content, DesEncryptUtils.KEY);
//传输过程,不转成16进制的字符串,程序就会崩溃掉
String code = DesEncryptUtils.parseByte2HexStr(encode);
System.out.println("密文字符串:" + code);
byte[] decode = DesEncryptUtils.parseHexStr2Byte(code);
// 解密
byte[] decryptResult = DesEncryptUtils.decrypt(decode, DesEncryptUtils.KEY);
System.out.println("解密后:" + new String(decryptResult, "UTF-8")); //不转码会乱码
}
}
启动运行
加密前:hello 中国--2018
密文字符串:3461AA5F7D3434A58C47F4BFBE2AA9D4953E5E3D5717D2EF6ABAD1E421C82B7E
解密后:hello 中国--2018
遇到的问题
现象
在window 环境下加解密都可以,但是 Linux 下解密报空指针异常。
原因
SecureRandom 实现完全随操作系统本身的內部状态,除非调用方在调用 getInstance 方法,然后调用 setSeed 方法;该实现在 windows 上每次生成的 key 都相同,但是在 solaris 或部分 linux 系统上则不同。关于SecureRandom类的详细介绍,见 http://yangzb.iteye.com/blog/325264
解决办法
将如下代码替换:
kgen.init(128, new SecureRandom(password.getBytes()));
替换为:
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(password.getBytes());
kgen.init(128, random);
DES加解密工具类的更多相关文章
- RSA加解密工具类RSAUtils.java,实现公钥加密私钥解密和私钥解密公钥解密
package com.geostar.gfstack.cas.util; import org.apache.commons.codec.binary.Base64; import javax.cr ...
- Java中的AES加解密工具类:AESUtils
本人手写已测试,大家可以参考使用 package com.mirana.frame.utils.encrypt; import com.mirana.frame.constants.SysConsta ...
- Java中的RSA加解密工具类:RSAUtils
本人手写已测试,大家可以参考使用 package com.mirana.frame.utils.encrypt; import com.mirana.frame.utils.log.LogUtils; ...
- vue 核心加解密工具类 方法
1 /* base64 加解密 2 */ 3 export let Base64 = require('js-base64').Base64 4 5 /* md5 加解密 6 */ 7 export ...
- Des加解密工具
import java.security.Key; import java.security.Security; import java.util.Date; import javax.crypto. ...
- des 加密解密工具类
最近在做des的双对称加密解密,特此记录一下. des对称加密,是一种比较传统的加密方式,其加密运算.解密运算使用的是同样的密钥,信息的发送者和信息的接收者在进行信息的传输与处理时,必须共同持有该密码 ...
- 加解密工具类(含keystore导出pfx)
java代码如下: package sign; import java.io.FileInputStream; import java.io.FileOutputStream; import java ...
- C# 加解密工具类
using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace Clov ...
- PHP 基础篇 - PHP 中 DES 加解密详解
一.简介 DES 是对称性加密里面常见一种,全称为 Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法.密钥长度是64位(bit),超过位数密钥被忽略.所谓对 ...
随机推荐
- git rebase 理解
摘录自:https://blog.csdn.net/wangnan9279/article/details/79287631
- BFS(宽度优先搜索) -例题
原题地址 https://vjudge.net/contest/313171 密码:algorithm A - Rescue Angel was caught by the MOLIGPY! ...
- React入门理解demo
1.React文档结构 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- Presto Event Listener开发
简介 同Hive Hook一样,Presto也支持自定义实现Event Listener,用于侦听Presto引擎执行查询时发生的事件,并作出相应的处理.我们可以利用该功能实现诸如自定义日志记录.调试 ...
- 【pycharm】Pycharm对 axios语法的支持问题
问题: 解决办法: 1,找到pychar的settings 2,ECMAScript6
- powermockito单元测试之深入实践
概述 由于最近工作需要, 在项目中要做单元测试, 以达到指定的测试用例覆盖率指标.项目中我们引入的powermockito来编写测试用例, JaCoCo来监控单元测试覆盖率.关于框架的选择, 网上讨论 ...
- 有容云:上车 | 听老司机谈Docker安全合规建设
编者注: 本文根据7月19日DockOne社群分享内容整理而成,分享嘉宾蒋运龙,有容云高级咨询顾问,一个IT的老兵,十年来混迹于存储.三网融合.多屏互动.智能穿戴.第三方支付.Docker等行业:经历 ...
- Python基础总结之认识lambda函数、map函数、filter() 函数。第十二天开始(新手可相互督促)
今天周日,白天在学习,晚上更新一些笔记,希望对大家能更好的理解.学习python~ lambda函数,也就是大家说的匿名函数.它没有具体的名称,也可以叫做一句话函数,我觉得也不过分,大家看下代码,来体 ...
- 使用 Docker 生成 Let’s Encrypt 证书
概念 什么是 Container ? https://www.docker.com/resources/what-container https://www.docker.com/why-docker ...
- Docker系列之烹饪披萨(二)
前言 上一篇我们讲解了虚拟机和容器的区别,本节我们来讲讲Docker中关于Dockerfile.镜像.容器等基本概念.Docker是一个在容器内开发.部署.运行应用程序的平台,Docker本质上是容器 ...