Java http数据MD5、AES、DES加密
一,数据加密
1、提供了,md5,Hex,Sha等不可逆算法加密
2、AES加密,此加密方式瘦平台影响较重,所以只适合同类平台加密解密
3、DES自定义加密,跨平台,兼容性好
1.org.apache.commons.codec.digest.DigestUtils
提供了,md5,Hex,Sha等不可逆算法加密
public static String MD5(String src)
{
return DigestUtils.md5Hex(src);
} public static String sha256Hex(String src)
{
return DigestUtils.sha256Hex(src);
}
2. AES加密,此加密方式瘦平台影响较重,所以只适合同类平台加密解密
/**
* 解密
*
* @param content
* 待解密内容
* @param key
* 解密的密钥
* @return
* @throws Exception
*/
public static String AESDecrypt(String content, String key) throws Exception {
if (content.length() < 1)
return null;
byte[] byteRresult = new byte[content.length() / 2];
for (int i = 0; i < content.length() / 2; i++) {
int high = Integer.parseInt(content.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(content.substring(i * 2 + 1, i * 2 + 2), 16);
byteRresult[i] = (byte) (high * 16 + low);
}
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(key.getBytes());
kgen.init(128,random); SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] result = cipher.doFinal(byteRresult);
return new String(result); } /**
* 解密
* @param content
* 待解密内容
* @param key
* 解密的密钥
* @return
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
*/
public static String AESEncrypt(String content, String key) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(key.getBytes());
kgen.init(128,random); SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] byteRresult = cipher.doFinal(byteContent);
String sb = new String(""); for (int i = 0; i < byteRresult.length; i++) {
String hex = Integer.toHexString(byteRresult[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb = sb.concat(hex.toUpperCase());
}
return sb; }
3.DES自定义加密,跨平台,兼容性好
package cn.twt.svx.utils; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest; import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec; import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder; /**
* 功能描述 加密常用类
*/
public class EncryptUtil {
// 密钥是16位长度的byte[]进行Base64转换后得到的字符串
public static String key = "LmMGStGtOpF4xNyvYt54EQ=="; /**
* <li>
* 方法名称:encrypt</li> <li>
* 加密方法
*
* @param xmlStr
* 需要加密的消息字符串
* @return 加密后的字符串
*/
public static String encrypt(String xmlStr) { // 使用DES算法使用加密消息体
String result = null;
try {
// 取需要加密内容的utf-8编码。
byte[] encrypt = xmlStr.getBytes("utf-8");
// 取MD5Hash码,并组合加密数组
byte[] md5Hasn = EncryptUtil.MD5Hash(encrypt, 0, encrypt.length);
// 组合消息体
byte[] totalByte = EncryptUtil.addMD5(md5Hasn, encrypt);
// 取密钥和偏转向量
byte[] key = new byte[8];
byte[] iv = new byte[8];
getKeyIV(EncryptUtil.key, key, iv);
SecretKeySpec deskey = new SecretKeySpec(key, "DES");
IvParameterSpec ivParam = new IvParameterSpec(iv); byte[] temp = EncryptUtil.DES_CBC_Encrypt(totalByte, deskey,
ivParam);
result = new BASE64Encoder().encode(temp);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} // 使用Base64加密后返回
return result;
} /**
* <li>
* 方法名称:encrypt</li> <li>
* 功能描述:
*
* <pre>
* 解密方法
* </pre>
*
* </li>
*
* @param xmlStr
* 需要解密的消息字符串
* @return 解密后的字符串
* @throws Exception
*/
public static String decrypt(String xmlStr) {
// 使用DES算法解密
String result = null;
try {
// base64解码
BASE64Decoder decoder = new BASE64Decoder();
byte[] encBuf = decoder.decodeBuffer(xmlStr);
// 取密钥和偏转向量
byte[] key = new byte[8];
byte[] iv = new byte[8];
getKeyIV(EncryptUtil.key, key, iv);
SecretKeySpec deskey = new SecretKeySpec(key, "DES");
IvParameterSpec ivParam = new IvParameterSpec(iv);
byte[] temp = EncryptUtil.DES_CBC_Decrypt(encBuf, deskey, ivParam);
// 进行解密后的md5Hash校验
byte[] md5Hash = EncryptUtil.MD5Hash(temp, 16, temp.length - 16);
// 进行解密校检
for (int i = 0; i < md5Hash.length; i++) {
if (md5Hash[i] != temp[i]) {
throw new Exception();
}
}
result = new String(temp, 16, temp.length - 16, "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
// 返回解密后的数组,其中前16位MD5Hash码要除去。
return result;
} /**
* <li>
* 方法名称:TripleDES_CBC_Encrypt</li> <li>
* 功能描述:
*
* <pre>
* 经过封装的三重DES/CBC加密算法,如果包含中文,请注意编码。
* </pre>
*
* </li>
*
* @param sourceBuf
* 需要加密内容的字节数组。
* @param deskey
* KEY 由24位字节数组通过SecretKeySpec类转换而成。
* @param ivParam
* IV偏转向量,由8位字节数组通过IvParameterSpec类转换而成。
* @return 加密后的字节数组
* @throws Exception
*/
public static byte[] TripleDES_CBC_Encrypt(byte[] sourceBuf,
SecretKeySpec deskey, IvParameterSpec ivParam) throws Exception {
byte[] cipherByte;
// 使用DES对称加密算法的CBC模式加密
Cipher encrypt = Cipher.getInstance("TripleDES/CBC/PKCS5Padding"); encrypt.init(Cipher.ENCRYPT_MODE, deskey, ivParam); cipherByte = encrypt.doFinal(sourceBuf, 0, sourceBuf.length);
// 返回加密后的字节数组
return cipherByte;
} /**
* <li>
* 方法名称:TripleDES_CBC_Decrypt</li> <li>
* 功能描述:
*
* <pre>
* 经过封装的三重DES / CBC解密算法
* </pre>
*
* </li>
*
* @param sourceBuf
* 需要解密内容的字节数组
* @param deskey
* KEY 由24位字节数组通过SecretKeySpec类转换而成。
* @param ivParam
* IV偏转向量,由6位字节数组通过IvParameterSpec类转换而成。
* @return 解密后的字节数组
* @throws Exception
*/
public static byte[] TripleDES_CBC_Decrypt(byte[] sourceBuf,
SecretKeySpec deskey, IvParameterSpec ivParam) throws Exception { byte[] cipherByte;
// 获得Cipher实例,使用CBC模式。
Cipher decrypt = Cipher.getInstance("TripleDES/CBC/PKCS5Padding");
// 初始化加密实例,定义为解密功能,并传入密钥,偏转向量
decrypt.init(Cipher.DECRYPT_MODE, deskey, ivParam); cipherByte = decrypt.doFinal(sourceBuf, 0, sourceBuf.length);
// 返回解密后的字节数组
return cipherByte;
} /**
* <li>
* 方法名称:DES_CBC_Encrypt</li> <li>
* 功能描述:
*
* <pre>
* 经过封装的DES/CBC加密算法,如果包含中文,请注意编码。
* </pre>
*
* </li>
*
* @param sourceBuf
* 需要加密内容的字节数组。
* @param deskey
* KEY 由8位字节数组通过SecretKeySpec类转换而成。
* @param ivParam
* IV偏转向量,由8位字节数组通过IvParameterSpec类转换而成。
* @return 加密后的字节数组
* @throws Exception
*/
public static byte[] DES_CBC_Encrypt(byte[] sourceBuf,
SecretKeySpec deskey, IvParameterSpec ivParam) throws Exception {
byte[] cipherByte;
// 使用DES对称加密算法的CBC模式加密
Cipher encrypt = Cipher.getInstance("DES/CBC/PKCS5Padding"); encrypt.init(Cipher.ENCRYPT_MODE, deskey, ivParam); cipherByte = encrypt.doFinal(sourceBuf, 0, sourceBuf.length);
// 返回加密后的字节数组
return cipherByte;
} /**
* <li>
* 方法名称:DES_CBC_Decrypt</li> <li>
* 功能描述:
*
* <pre>
* 经过封装的DES/CBC解密算法。
* </pre>
*
* </li>
*
* @param sourceBuf
* 需要解密内容的字节数组
* @param deskey
* KEY 由8位字节数组通过SecretKeySpec类转换而成。
* @param ivParam
* IV偏转向量,由6位字节数组通过IvParameterSpec类转换而成。
* @return 解密后的字节数组
* @throws Exception
*/
public static byte[] DES_CBC_Decrypt(byte[] sourceBuf,
SecretKeySpec deskey, IvParameterSpec ivParam) throws Exception { byte[] cipherByte;
// 获得Cipher实例,使用CBC模式。
Cipher decrypt = Cipher.getInstance("DES/CBC/PKCS5Padding");
// 初始化加密实例,定义为解密功能,并传入密钥,偏转向量
decrypt.init(Cipher.DECRYPT_MODE, deskey, ivParam); cipherByte = decrypt.doFinal(sourceBuf, 0, sourceBuf.length);
// 返回解密后的字节数组
return cipherByte;
} /**
* <li>
* 方法名称:MD5Hash</li> <li>
* 功能描述:
*
* <pre>
* MD5,进行了简单的封装,以适用于加,解密字符串的校验。
* </pre>
*
* </li>
*
* @param buf
* 需要MD5加密字节数组。
* @param offset
* 加密数据起始位置。
* @param length
* 需要加密的数组长度。
* @return
* @throws Exception
*/
public static byte[] MD5Hash(byte[] buf, int offset, int length)
throws Exception {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(buf, offset, length);
return md.digest();
} /**
* <li>
* 方法名称:byte2hex</li> <li>
* 功能描述:
*
* <pre>
* 字节数组转换为二行制表示
* </pre>
*
* </li>
*
* @param inStr
* 需要转换字节数组。
* @return 字节数组的二进制表示。
*/
public static String byte2hex(byte[] inStr) {
String stmp;
// out = new StringBuffer(inStr.length * 2);
String out = new String("");
for (int n = 0; n < inStr.length; n++) {
// 字节做"与"运算,去除高位置字节 11111111
stmp = Integer.toHexString(inStr[n] & 0xFF);
if (stmp.length() == 1) {
// 如果是0至F的单位字符串,则添加0
out = out + "0" + stmp;
} else {
out = out + stmp;
}
}
return out;
} /**
* <li>
* 方法名称:addMD5</li> <li>
* 功能描述:
*
* <pre>
* MD校验码 组合方法,前16位放MD5Hash码。 把MD5验证码byte[],加密内容byte[]组合的方法。
* </pre>
*
* </li>
*
* @param md5Byte
* 加密内容的MD5Hash字节数组。
* @param bodyByte
* 加密内容字节数组
* @return 组合后的字节数组,比加密内容长16个字节。
*/
public static byte[] addMD5(byte[] md5Byte, byte[] bodyByte) {
int length = bodyByte.length + md5Byte.length;
byte[] resutlByte = new byte[length]; // 前16位放MD5Hash码
for (int i = 0; i < length; i++) {
if (i < md5Byte.length) {
resutlByte[i] = md5Byte[i];
} else {
resutlByte[i] = bodyByte[i - md5Byte.length];
}
} return resutlByte;
} /**
* <li>
* 方法名称:getKeyIV</li> <li>
* 功能描述:
*
* <pre>
*
* </pre>
* </li>
*
* @param encryptKey
* @param key
* @param iv
*/
public static void getKeyIV(String encryptKey, byte[] key, byte[] iv) {
// 密钥Base64解密
BASE64Decoder decoder = new BASE64Decoder();
byte[] buf = null;
try {
buf = decoder.decodeBuffer(encryptKey);
} catch (IOException e) {
e.printStackTrace();
}
// 前8位为key
int i;
for (i = 0; i < key.length; i++) {
key[i] = buf[i];
}
// 后8位为iv向量
for (i = 0; i < iv.length; i++) {
iv[i] = buf[i + 8];
}
} }
Java http数据MD5、AES、DES加密的更多相关文章
- Golang之AES/DES加密解密
AES/DES加密/解密涉及4个概念:1. Block, 也叫分组, 相应加密/解密的算法. 2. BlockMode, 模式, 相应加密/解密的处理.3. InitalVectory, 初始向量4. ...
- java MD5/AES/DES加解密汇总
package com.test.test1.util; import java.security.MessageDigest; import java.security.SecureRandom; ...
- python中常用的base64 md5 aes des crc32等的加密解密
1.base64 Python内置的base64模块可以实现base64.base32.base16.base85.urlsafe_base64的编码解码,python 3.x通常输入输出都是二进制形 ...
- iOS 开发之路(AES/DES加密实现) 三
最近接触的这个项目由于以前服务器上用的是DES/CBC/PKCS5Padding加密方式,为了让在iOS上的加密结果与服务器端保持一致,我做了很多尝试,现在分享给大家.PS:现在不推荐用DES了,只是 ...
- java和delphi共用的des加密解密
java: import antlr.StringUtils;import org.jeecgframework.core.util.StringUtil; import java.security. ...
- MD5和DES加密方法
/// <summary> /// MD5加密 /// </summary> /// <param name=&q ...
- JAVA和PYTHON同时实现AES的加密解密操作---且生成的BASE62编码一致
终于有机会生产JAVA的东东了. 有点兴奋. 花了一天搞完.. java(关键key及算法有缩减): package com.security; import javax.crypto.Cipher; ...
- JAVA实现DES加密实现详解
package util; import java.security.SecureRandom; import javax.crypto.spec.DESKeySpec; import javax.c ...
- 【Java】JAVA-加密-DES加密代码详解
package util; import java.security.SecureRandom; import javax.crypto.spec.DESKeySpec; import javax.c ...
随机推荐
- JavaScript编程:浏览器对象模型BOM
4.浏览器对象模型BOM: document.body.offsetwidth可以获取浏览器宽度. Window对象: 窗口操作: 1.moveBy(dx,dy ...
- Html.Partial("")与Html.RenderPartial("")区别
文章有点长,但大多是代码,看看很快的,不要压力太大.网上有很多关于这两个方法的区别,都说出了它本质的区别(不看代码,只看这个结论,就已经足够了,如果觉得有必要从代码中得出这个结论,那就继续往下看),这 ...
- 重操JS旧业第一弹:Script与JS加载
不管js被包装成什么样子,最终交给浏览器执行的js都是原生的,都离不开原生js的原理. Script标签纸html中用来加载js的标签,我们知道js可以是来自外部,本地,或者内部一段代码,在这里只讨论 ...
- javascript属性一览
getElementsByTagName() 方法可返回带有指定标签名的对象的集合. getElementsByName() 方法可返回带有指定名称的对象的集合. getAttribute() 方法返 ...
- FastDFS概要
本篇文章是我上级老大所写. 留在这里为了不弄丢. FastDFS是一款开源的轻量级分布式文件系统 纯C实现,支持Linux, FreeBSD等UNIX系统 类google FS, 不是通用的文件系统, ...
- 插件 - 提示窗体(ArtDialog)
效果: 代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default. ...
- Delphi颜色的表示(一共5种表示法)
//全以红色举例: //1. RGB 模式:Self.Color := $0000ff; //不过和HTML.PhotoShop.FireWorks中的 #ff0000 是完全反的,应该叫 BGR. ...
- JavaScript + CSS3 实现的海报画廊特效
原文:JavaScript + CSS3 实现的海报画廊特效 这是慕课网上<CSS3+JS 实现超炫的散列画廊特效>的源代码,我修改了一些 bug 和调优了一些细节,并把学习过程中并不了解 ...
- Database Connection Pool Library | Libzdb
Database Connection Pool Library | Libzdb A small, easy to use Open Source Database Connection Pool ...
- Velocity教程-脚本语法详解(转)
Velocity是一个基于java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象. 当Veloci ...