【Java】MD5字符串的加密解密
生活随笔
收集整理的这篇文章主要介绍了
【Java】MD5字符串的加密解密
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
至于为什么要给字符串加密解密,各种加密解密的方式优缺点对比等等,本文都不会讲,弄点实用的代码,给需要应急的朋友应急
废话不多说,看代码了:
import java.security.Key; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.spec.AlgorithmParameterSpec;import javax.crypto.Cipher; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import javax.crypto.spec.IvParameterSpec;import org.apache.commons.lang.StringUtils;import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder;public class MD5Tool {//向量(同时拥有向量和密匙才能解密),此向量必须是8byte,多少都报错private final byte[] DESIV = new byte[] { 0x22, 0x54, 0x36, 110, 0x40, (byte) 0xac, (byte) 0xad, (byte) 0xdf };// 向量 private AlgorithmParameterSpec iv = null;// 加密算法的参数接口private Key key = null;private String charset = "utf-8";public static void main(String[] args) {try {String value = "caililiang蔡立亮_微信:caililiangcaililiang;邮箱:785553790@qq.com";String key = "qwrwrww十多个";// 自定义密钥,个数不能太短,太短报错,过长,它默认只取前N位(N的具体值,大家另行查找资料)MD5Tool mt= new MD5Tool(key, "utf-8");System.out.println("加密前的字符:" + value);System.out.println("加密后的字符:" + mt.encode(value));System.out.println("解密后的字符:" + mt.decode(mt.encode(value)));System.out.println("字符串的MD5值:"+getMD5Value(value));} catch (Exception e) {e.printStackTrace();}}/*** 构造函数* @param deSkey* @param charset* @throws Exception*/public MD5Tool(String deSkey, String charset) throws Exception {if (StringUtils.isNotBlank(charset)) {this.charset = charset;}DESKeySpec keySpec = new DESKeySpec(deSkey.getBytes(this.charset));// 设置密钥参数iv = new IvParameterSpec(DESIV);// 设置向量SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂key = keyFactory.generateSecret(keySpec);// 得到密钥对象}/*** 加密* @param data* @return* @throws Exception*/public String encode(String data) throws Exception {Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");// 得到加密对象CipherenCipher.init(Cipher.ENCRYPT_MODE, key, iv);// 设置工作模式为加密模式,给出密钥和向量byte[] pasByte = enCipher.doFinal(data.getBytes(this.charset));BASE64Encoder base64Encoder = new BASE64Encoder();return base64Encoder.encode(pasByte);}/*** 解密* @param data* @return* @throws Exception*/public String decode(String data) throws Exception {Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");deCipher.init(Cipher.DECRYPT_MODE, key, iv);BASE64Decoder base64Decoder = new BASE64Decoder();//此处注意doFinal()的参数的位数必须是8的倍数,否则会报错(通过encode加密的字符串读出来都是8的倍数位,但写入文件再读出来,就可能因为读取的方式的问题,导致最后此处的doFinal()的参数的位数不是8的倍数)//此处必须用base64Decoder,若用data。getBytes()则获取的字符串的byte数组的个数极可能不是8的倍数,而且不与上面的BASE64Encoder对应(即使解密不报错也不会得到正确结果)byte[] pasByte = deCipher.doFinal(base64Decoder.decodeBuffer(data));return new String(pasByte, this.charset);}/*** 获取MD5的值,可用于对比校验* @param sourceStr* @return*/private static String getMD5Value(String sourceStr) {String result = "";try {MessageDigest md = MessageDigest.getInstance("MD5");md.update(sourceStr.getBytes());byte b[] = md.digest();int i;StringBuffer buf = new StringBuffer("");for (int offset = 0; offset < b.length; offset++) {i = b[offset];if (i < 0)i += 256;if (i < 16)buf.append("0");buf.append(Integer.toHexString(i));}result = buf.toString();} catch (NoSuchAlgorithmException e) {}return result;} }运行结果: 加密前的字符:caililiang蔡立亮_微信:caililiangcaililiang;邮箱:785553790@qq.com 加密后的字符:G2spygunutcaaUcf5mUmVsUlrXUem9zPnG624B2KLdpNg7vJOdpCPl7n/eu7umaqGGEWlZdplHiI HZSHGN9X4IK1MTHZxJ/SjHcP1VIPq8U= 解密后的字符:caililiang蔡立亮_微信:caililiangcaililiang;邮箱:785553790@qq.com 字符串的MD5值:d5416a341766390368ab75d220a6c051此处废话几句,就是如果把加密之后的文件写入到某个文件后,解密时读取文件内容时,千万不要读取很多空的byte数据,而是有多少数据读多少,不能多也不能少
例如:
File f = new File("C://ceshi.txt");
Reader r =new java.io.FileReader(f);
char[] c = new char[1024];
r.read(c);//把数据都读到c中
String text = new String(c);
这样读出来的数据是不行的,文件中的值读取出来少于1024,但c会把缺少的补空(如:[a, s, f, s, f, s, s, d, ....]),正确方式可以
File f = new File("C://ceshi.txt");
Reader r =new java.io.FileReader(f);
char[] c = new char[1024];
int len =r.read(c);//把数据都读到c中
String text = new String(c,0,len);
这样读取的长度不再是1024了,而是实际长度,这样读取到的内容拿去解密才能得到正确内容
总结
以上是生活随笔为你收集整理的【Java】MD5字符串的加密解密的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 【ABAP】BASE64加密及解密
- 下一篇: 【学习笔记】Javascript函数调用