java生成MD5加密

合集下载

Java实现MD5加密及解密的代码实例分享

Java实现MD5加密及解密的代码实例分享

Java实现MD5加密及解密的代码实例分享基础:MessageDigest类的使⽤其实要在Java中完成MD5加密,MessageDigest类⼤部分都帮你实现好了,⼏⾏代码⾜矣:/*** 对字符串md5加密** @param str* @return*/import java.security.MessageDigest;public static String getMD5(String str) {try {// ⽣成⼀个MD5加密计算摘要MessageDigest md = MessageDigest.getInstance("MD5");// 计算md5函数md.update(str.getBytes());// digest()最后确定返回md5 hash值,返回值为8为字符串。

因为md5 hash值是16位的hex值,实际上就是8位的字符// BigInteger函数则将8位的字符串转换成16位hex值,⽤字符串来表⽰;得到字符串形式的hash值return new BigInteger(1, md.digest()).toString(16);} catch (Exception e) {throw new SpeedException("MD5加密出现错误");}}进阶:加密及解密类Java实现MD5加密以及解密类,附带测试类,具体见代码。

MD5加密解密类——MyMD5Util,代码如下package com.zyg.security.md5;import java.io.UnsupportedEncodingException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;import java.util.Arrays;public class MyMD5Util {private static final String HEX_NUMS_STR="0123456789ABCDEF";private static final Integer SALT_LENGTH = 12;/*** 将16进制字符串转换成字节数组* @param hex* @return*/public static byte[] hexStringToByte(String hex) {int len = (hex.length() / 2);byte[] result = new byte[len];char[] hexChars = hex.toCharArray();for (int i = 0; i < len; i++) {int pos = i * 2;result[i] = (byte) (HEX_NUMS_STR.indexOf(hexChars[pos]) << 4| HEX_NUMS_STR.indexOf(hexChars[pos + 1]));}return result;}/*** 将指定byte数组转换成16进制字符串* @param b* @return*/public static String byteToHexString(byte[] b) {StringBuffer hexString = new StringBuffer();for (int i = 0; i < b.length; i++) {String hex = Integer.toHexString(b[i] & 0xFF);if (hex.length() == 1) {hex = '0' + hex;}hexString.append(hex.toUpperCase());}return hexString.toString();}/*** 验证⼝令是否合法* @param password* @param passwordInDb* @return* @throws NoSuchAlgorithmException* @throws UnsupportedEncodingException*/public static boolean validPassword(String password, String passwordInDb)throws NoSuchAlgorithmException, UnsupportedEncodingException {//将16进制字符串格式⼝令转换成字节数组byte[] pwdInDb = hexStringToByte(passwordInDb);//声明盐变量byte[] salt = new byte[SALT_LENGTH];//将盐从数据库中保存的⼝令字节数组中提取出来System.arraycopy(pwdInDb, 0, salt, 0, SALT_LENGTH);//创建消息摘要对象MessageDigest md = MessageDigest.getInstance("MD5");//将盐数据传⼊消息摘要对象md.update(salt);//将⼝令的数据传给消息摘要对象md.update(password.getBytes("UTF-8"));//⽣成输⼊⼝令的消息摘要byte[] digest = md.digest();//声明⼀个保存数据库中⼝令消息摘要的变量byte[] digestInDb = new byte[pwdInDb.length - SALT_LENGTH];//取得数据库中⼝令的消息摘要System.arraycopy(pwdInDb, SALT_LENGTH, digestInDb, 0, digestInDb.length); //⽐较根据输⼊⼝令⽣成的消息摘要和数据库中消息摘要是否相同if (Arrays.equals(digest, digestInDb)) {//⼝令正确返回⼝令匹配消息return true;} else {//⼝令不正确返回⼝令不匹配消息return false;}}/*** 获得加密后的16进制形式⼝令* @param password* @return* @throws NoSuchAlgorithmException* @throws UnsupportedEncodingException*/public static String getEncryptedPwd(String password)throws NoSuchAlgorithmException, UnsupportedEncodingException {//声明加密后的⼝令数组变量byte[] pwd = null;//随机数⽣成器SecureRandom random = new SecureRandom();//声明盐数组变量byte[] salt = new byte[SALT_LENGTH];//将随机数放⼊盐变量中random.nextBytes(salt);//声明消息摘要对象MessageDigest md = null;//创建消息摘要md = MessageDigest.getInstance("MD5");//将盐数据传⼊消息摘要对象md.update(salt);//将⼝令的数据传给消息摘要对象md.update(password.getBytes("UTF-8"));//获得消息摘要的字节数组byte[] digest = md.digest();//因为要在⼝令的字节数组中存放盐,所以加上盐的字节长度pwd = new byte[digest.length + SALT_LENGTH];//将盐的字节拷贝到⽣成的加密⼝令字节数组的前12个字节,以便在验证⼝令时取出盐 System.arraycopy(salt, 0, pwd, 0, SALT_LENGTH);//将消息摘要拷贝到加密⼝令字节数组从第13个字节开始的字节System.arraycopy(digest, 0, pwd, SALT_LENGTH, digest.length);//将字节数组格式加密后的⼝令转化为16进制字符串格式的⼝令return byteToHexString(pwd);}}测试类——Client,代码如下:package com.zyg.security.md5;import java.io.UnsupportedEncodingException;import java.security.NoSuchAlgorithmException;import java.util.HashMap;import java.util.Map;public class Client {private static Map users = new HashMap();public static void main(String[] args){String userName = "zyg";String password = "123";registerUser(userName,password);userName = "changong";password = "456";registerUser(userName,password);String loginUserId = "zyg";String pwd = "1232";try {if(loginValid(loginUserId,pwd)){System.out.println("欢迎登陆");}else{System.out.println("⼝令错误,请重新输⼊");}} catch (NoSuchAlgorithmException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/*** 注册⽤户** @param userName* @param password*/public static void registerUser(String userName,String password){String encryptedPwd = null;try {encryptedPwd = MyMD5Util.getEncryptedPwd(password);users.put(userName, encryptedPwd);} catch (NoSuchAlgorithmException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/*** 验证登陆** @param userName* @param password* @return* @throws UnsupportedEncodingException* @throws NoSuchAlgorithmException*/public static boolean loginValid(String userName,String password)throws NoSuchAlgorithmException, UnsupportedEncodingException{String pwdInDb = (String)users.get(userName);if(null!=pwdInDb){ // 该⽤户存在return MyMD5Util.validPassword(password, pwdInDb);}else{System.out.println("不存在该⽤户");return false;}}}PS:这⾥再为⼤家提供2款MD5加密⼯具,感兴趣的朋友可以参考⼀下:MD5在线加密⼯具:在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密⼯具:。

JAVA生成MD5校验码及算法实现

JAVA生成MD5校验码及算法实现

JAVA生成MD5校验码及算法实现在Java中,java.security.MessageDigest (rt.jar中)已经定义了MD5 的计算,所以我们只需要简单地调用即可得到MD5 的128 位整数。

然后将此128 位计16 个字节转换成16 进制表示即可。

下面是一个可生成字符串或文件MD5校验码的例子,测试过,可当做工具类直接使用,其中最主要的是getMD5String(String s)和getFileMD5String(File file)两个方法,分别用于生成字符串的md5校验值和生成文件的md5校验值,getFileMD5String_old(File file)方法可删除,不建议使用:package com.why.md5;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.nio.MappedByteBuffer;import java.nio.channels.FileChannel;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;public class MD5Util {/*** 默认的密码字符串组合,用来将字节转换成16 进制表示的字符,apache 校验下载的文件的正确性用的就是默认的这个组合*/protected static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6','7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };protected static MessageDigest messagedigest = null;static {try {messagedigest = MessageDigest.getInstance("MD5");} catch (NoSuchAlgorithmException nsaex) {System.err.println(MD5Util.class.getName()+ "初始化失败,MessageDigest不支持MD5Util。

Java详解单向加密--MD5、SHA和HMAC及简单实现实例

Java详解单向加密--MD5、SHA和HMAC及简单实现实例

Java详解单向加密--MD5、SHA和HMAC及简单实现实例Java 详解单向加密--MD5、SHA和HMAC及简单实现实例概要:MD5、SHA、HMAC这三种加密算法,可谓是⾮可逆加密,就是不可解密的加密⽅法。

MD5MD5即Message-Digest Algorithm 5(信息-摘要算法5),⽤于确保信息传输完整⼀致。

MD5是输⼊不定长度信息,输出固定长度128-bits的算法。

MD5算法具有以下特点:1、压缩性:任意长度的数据,算出的MD5值长度都是固定的。

2、容易计算:从原数据计算出MD5值很容易。

3、抗修改性:对原数据进⾏任何改动,哪怕只修改1个字节,所得到的MD5值都有很⼤区别。

4、强抗碰撞:已知原数据和其MD5值,想找到⼀个具有相同MD5值的数据(即伪造数据)是⾮常困难的。

MD5还⼴泛⽤于操作系统的登陆认证上,如Unix、各类BSD系统登录密码、数字签名等诸多⽅⾯。

如在Unix系统中⽤户的密码是以MD5(或其它类似的算法)经Hash运算后存储在⽂件系统中。

当⽤户登录的时候,系统把⽤户输⼊的密码进⾏MD5 Hash运算,然后再去和保存在⽂件系统中的MD5值进⾏⽐较,进⽽确定输⼊的密码是否正确。

通过这样的步骤,系统在并不知道⽤户密码的明码的情况下就可以确定⽤户登录系统的合法性。

这可以避免⽤户的密码被具有系统管理员权限的⽤户知道。

MD5将任意长度的“字节串”映射为⼀个128bit的⼤整数,并且通过该128bit反推原始字符串是⾮常困难的。

SHASHA(Secure Hash Algorithm,安全散列算法),数字签名等密码学应⽤中重要的⼯具,被⼴泛地应⽤于电⼦商务等信息安全领域。

虽然SHA与MD5通过碰撞法都被破解了,但是SHA仍然是公认的安全加密算法,较之MD5更为安全。

SHA所定义的长度下表中的中继散列值(internal state)表⽰对每个数据区块压缩散列过后的中继值(internal hash sum)。

MD5加密法

MD5加密法

* java.security包中的MessageDigest类提供了计算消息摘要的方法,首先生成对象,执行其update( )方法可以将原始数据传递给该对象,然后执行其digest( )方法即可得到消息摘要。

*/package org.qrsx.util;import java.io.UnsupportedEncodingException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;/*** MD5加密* @author Administrator**/public class DigestPass {private MessageDigest messageDigest;private String result = "";private byte[] args = null;public String getDigestPassWord(String userpass) {try {// 生成MessageDigest对象,传入所用算法的参数(MD5)messageDigest = MessageDigest.getInstance("MD5");// 使用getBytes( )方法生成字符串数组messageDigest.update(userpass.getBytes("GBK"));// 执行MessageDigest对象的digest( )方法完成计算,计算的结果通过字节类型的数组返回args = messageDigest.digest();} catch (NoSuchAlgorithmException e) {e.printStackTrace();throw new RuntimeException();} catch (UnsupportedEncodingException ee) {ee.printStackTrace();throw new RuntimeException();} finally {messageDigest.reset();}// 将结果转换成字符串result = "";// result清空,否则它会自动累加!!!for (int i = 0; i < args.length; i++) {result += Integer.toHexString((0x000000ff & args[i]) | 0xffffff00).substring(6);}return result;}。

Java和JSMD5加密-附盐值加密demo

Java和JSMD5加密-附盐值加密demo

Java和JSMD5加密-附盐值加密demo JAVA和JS的MD5加密经过测试:字母和数据好使,中⽂不好使。

源码如下:*** 类MD5Util.java的实现描述:**/public class MD5Util {// 获得MD5摘要算法的 MessageDigest 对象private static MessageDigest _mdInst = null;private static char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};private static MessageDigest getMdInst() {if (_mdInst == null) {try {_mdInst = MessageDigest.getInstance("MD5");} catch (NoSuchAlgorithmException e) {e.printStackTrace();}}return _mdInst;}private MD5Util() {}/*** Returns a MessageDigest for the given <code>algorithm</code>.** @return An MD5 digest instance.* @throws RuntimeException when a {@link NoSuchAlgorithmException} is caught*/static MessageDigest getDigest() {try {return MessageDigest.getInstance("MD5");} catch (NoSuchAlgorithmException e) {throw new RuntimeException(e);}}/*** Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.** @param data Data to digest* @return MD5 digest*/public static byte[] md5(byte[] data) {return getDigest().digest(data);}/*** Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.** @param data Data to digest* @return MD5 digest*/public static byte[] md5(String data) {return md5(data.getBytes());}/*** Calculates the MD5 digest and returns the value as a 32 character hex string.** @param data Data to digest* @return MD5 digest as a hex string*//*public static String md5Hex(byte[] data) {return HexUtil.toHexString(md5(data));}*//*** Calculates the MD5 digest and returns the value as a 32 character hex string. ** @param data Data to digest* @return MD5 digest as a hex string*//*public static String md5Hex(String data) {return HexUtil.toHexString(md5(data));}*//*** 对密码字符串进⾏MD5加密** @param pass* @return*/public static String encoding(String pass) {if (pass == null) {return "";} else {StringBuffer buf = null;MessageDigest md5 = null;try {// 获取md5摘要md5 = MessageDigest.getInstance("MD5");// 指定utf-8编码md5.update(pass.getBytes("utf-8"));byte[] b = md5.digest();int i;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");}// 产⽣16进制保存buf.append(Integer.toHexString(i));}} catch (NoSuchAlgorithmException e) {e.printStackTrace();} catch (UnsupportedEncodingException e) {e.printStackTrace();}return buf.toString();}}public final static String encode(String s) {try {byte[] btInput = s.getBytes();// 使⽤指定的字节更新摘要getMdInst().update(btInput);// 获得密⽂byte[] md = getMdInst().digest();// 把密⽂转换成⼗六进制的字符串形式int j = md.length;char str[] = new char[j * 2];int k = 0;for (int i = 0; i < j; i++) {byte byte0 = md[i];str[k++] = hexDigits[byte0 >>> 4 & 0xf];str[k++] = hexDigits[byte0 & 0xf];}return new String(str);} catch (Exception e) {e.printStackTrace();return null;}}}md5.js/** A JavaScript implementation of the RSA Data Security, Inc. MD5 Message* Digest Algorithm, as defined in RFC 1321.* Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet* Distributed under the BSD License* See /crypt/md5 for more info.*//** Configurable variables. You may need to tweak these to be compatible with* the server-side, but the defaults work in most cases.*/var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode *//** These are the functions you'll usually want to call* They take string arguments and return either hex or base-64 encoded strings*/function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));} function b64_md5(s){ return binl2b64(core_md5(str2binl(s), s.length * chrsz));} function str_md5(s){ return binl2str(core_md5(str2binl(s), s.length * chrsz));} function hex_hmac_md5(key, data) { return binl2hex(core_hmac_md5(key, data)); } function b64_hmac_md5(key, data) { return binl2b64(core_hmac_md5(key, data)); } function str_hmac_md5(key, data) { return binl2str(core_hmac_md5(key, data)); } /** Perform a simple self-test to see if the VM is working*/function md5_vm_test(){return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72";}/** Calculate the MD5 of an array of little-endian words, and a bit length*/function core_md5(x, len){/* append padding */x[len >> 5] |= 0x80 << ((len) % 32);x[(((len + 64) >>> 9) << 4) + 14] = len;var a = 1732584193;var b = -271733879;var c = -1732584194;var d = 271733878;for(var i = 0; i < x.length; i += 16){var olda = a;var oldb = b;var oldc = c;var oldd = d;a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);c = md5_ff(c, d, a, b, x[i+ 2], 17, 606105819);b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);d = md5_ff(d, a, b, c, x[i+ 5], 12, 1200080426);c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);a = md5_ff(a, b, c, d, x[i+ 8], 7 , 1770035416);d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);c = md5_ff(c, d, a, b, x[i+10], 17, -42063);b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);a = md5_ff(a, b, c, d, x[i+12], 7 , 1804603682);d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);b = md5_ff(b, c, d, a, x[i+15], 22, 1236535329);a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);c = md5_gg(c, d, a, b, x[i+11], 14, 643717713);b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);d = md5_gg(d, a, b, c, x[i+10], 9 , 38016083);c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);a = md5_gg(a, b, c, d, x[i+ 9], 5 , 568446438);d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);b = md5_gg(b, c, d, a, x[i+ 8], 20, 1163531501);a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);c = md5_gg(c, d, a, b, x[i+ 7], 14, 1735328473);b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);c = md5_hh(c, d, a, b, x[i+11], 16, 1839030562);b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);d = md5_hh(d, a, b, c, x[i+ 4], 11, 1272893353);c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);a = md5_hh(a, b, c, d, x[i+13], 4 , 681279174);d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);b = md5_hh(b, c, d, a, x[i+ 6], 23, 76029189);a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);c = md5_hh(c, d, a, b, x[i+15], 16, 530742520);b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);d = md5_ii(d, a, b, c, x[i+ 7], 10, 1126891415);c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);a = md5_ii(a, b, c, d, x[i+12], 6 , 1700485571);d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);a = md5_ii(a, b, c, d, x[i+ 8], 6 , 1873313359);d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);b = md5_ii(b, c, d, a, x[i+13], 21, 1309151649);a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);c = md5_ii(c, d, a, b, x[i+ 2], 15, 718787259);b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);a = safe_add(a, olda);b = safe_add(b, oldb);c = safe_add(c, oldc);d = safe_add(d, oldd);}return Array(a, b, c, d);}/** These functions implement the four basic operations the algorithm uses. */function md5_cmn(q, a, b, x, s, t){return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);}function md5_ff(a, b, c, d, x, s, t){return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);}function md5_gg(a, b, c, d, x, s, t){return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);}function md5_hh(a, b, c, d, x, s, t){return md5_cmn(b ^ c ^ d, a, b, x, s, t);}function md5_ii(a, b, c, d, x, s, t){return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);}/** Calculate the HMAC-MD5, of a key and some data*/function core_hmac_md5(key, data){var bkey = str2binl(key);if(bkey.length > 16) bkey = core_md5(bkey, key.length * chrsz);var ipad = Array(16), opad = Array(16);for(var i = 0; i < 16; i++){ipad[i] = bkey[i] ^ 0x36363636;opad[i] = bkey[i] ^ 0x5C5C5C5C;}var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);return core_md5(opad.concat(hash), 512 + 128);}/** Add integers, wrapping at 2^32. This uses 16-bit operations internally* to work around bugs in some JS interpreters.*/function safe_add(x, y){var lsw = (x & 0xFFFF) + (y & 0xFFFF);var msw = (x >> 16) + (y >> 16) + (lsw >> 16);return (msw << 16) | (lsw & 0xFFFF);}/** Bitwise rotate a 32-bit number to the left.*/function bit_rol(num, cnt){return (num << cnt) | (num >>> (32 - cnt));}/** Convert a string to an array of little-endian words* If chrsz is ASCII, characters >255 have their hi-byte silently ignored.*/function str2binl(str){var bin = Array();var mask = (1 << chrsz) - 1;for(var i = 0; i < str.length * chrsz; i += chrsz)bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);return bin;}/** Convert an array of little-endian words to a string*/function binl2str(bin){var str = "";var mask = (1 << chrsz) - 1;for(var i = 0; i < bin.length * 32; i += chrsz)str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask);return str;}/** Convert an array of little-endian words to a hex string.*/function binl2hex(binarray){var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";var str = "";for(var i = 0; i < binarray.length * 4; i++){str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +hex_tab.charAt((binarray[i>>2] >> ((i%4)*8 )) & 0xF);}return str;}/** Convert an array of little-endian words to a base-64 string*/function binl2b64(binarray){var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; var str = "";for(var i = 0; i < binarray.length * 4; i += 3){var triplet = (((binarray[i >> 2] >> 8 * ( i %4)) & 0xFF) << 16)| (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 )| ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF);for(var j = 0; j < 4; j++){if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);}}return str;}测试DEMO类(⼯具类加密后转成⼩写):public class Test {public static void main(String[] args) throws Exception {String source = "123AB";String salt = "asdklsakjfdlsadjfl";System.out.printf("source is %s,result is %s",salt+source,MD5Util.encode(salt+source).toLowerCase()); }}运⾏结果:source is asdklsakjfdlsadjfl123AB,result is d5976dad58ad32000e3867f0601e236c。

JAVA中获取文件MD5值的四种方法

JAVA中获取文件MD5值的四种方法

JAVA中获取⽂件MD5值的四种⽅法 JAVA中获取⽂件MD5值的四种⽅法其实都很类似,因为核⼼都是通过JAVA⾃带的MessageDigest类来实现。

获取⽂件MD5值主要分为三个步骤,第⼀步获取⽂件的byte信息,第⼆步通过MessageDigest类进⾏MD5加密,第三步转换成16进制的MD5码值。

⼏种⽅法的不同点主要在第⼀步和第三步上。

具体可以看下⾯的例⼦:⽅法⼀、1 private final static String[] strHex = { "0", "1", "2", "3", "4", "5",2 "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };34 public static String getMD5One(String path) {5 StringBuffer sb = new StringBuffer();6 try {7 MessageDigest md = MessageDigest.getInstance("MD5");8 byte[] b = md.digest(FileUtils.readFileToByteArray(new File(path)));9 for (int i = 0; i < b.length; i++) {10 int d = b[i];11 if (d < 0) {12 d += 256;13 }14 int d1 = d / 16;15 int d2 = d % 16;16 sb.append(strHex[d1] + strHex[d2]);17 }18 } catch (NoSuchAlgorithmException e) {19 e.printStackTrace();20 } catch (IOException e) {21 e.printStackTrace();22 }23 return sb.toString();24 } ⽅法⼀是⽐较原始的⼀种实现⽅法,⾸先将⽂件⼀次性读⼊内存,然后通过MessageDigest进⾏MD5加密,最后再⼿动将其转换为16进制的MD5值。

MD5的使用方法

MD5的使用方法

MD5的使⽤⽅法MD5加密的Java实现在各种应⽤系统中,如果需要设置账户,那么就会涉及到存储⽤户账户信息的问题,为了保证所存储账户信息的安全,通常会采⽤MD5加密的⽅式来,进⾏存储。

⾸先,简单得介绍⼀下,什么是MD5加密。

MD5的全称是Message-Digest Algorithm 5 (信息-摘要算法),在90年代初,由MIT Laboratory for Computer Scientce 和RSA Data Security Inc 的 Ronald L.Rivest开发出来,经MD2、MD3和MD4发展⽽来。

是让⼤容量信息在⽤数字签名软件签署私⼈密匙前被"压缩"成⼀种保密的格式(就是把⼀个任意长度的字节串变换成⼀定长的⼤整数)。

不管是MD2、MD4还是MD5,它们都需要获得⼀个随机长度的信息并产⽣⼀个128位的信息摘要。

虽然这些算法的结构或多或少有些相似,但MD2的设计与MD4和MD5完全不同,那是因为MD2是为8位机器做过设计优化的,⽽MD4和MD5却是⾯向32位的电脑。

这三个算法的描述和C语⾔源代码在Internet RFCs 1321中有详细的描述,这是⼀份最权威的⽂档,由Ronald L.Rivest在1992年8⽉向IETF提交。

(⼀)消息摘要简介⼀个消息摘要就是⼀个数据块的数字指纹。

即对⼀个任意长度的⼀个数据块进⾏计算,产⽣⼀个唯⼀指印(对于SHA1是产⽣⼀个20字节的⼆进制数组)。

消息摘要是⼀种与消息认证码结合使⽤以确保消息完整性的技术。

主要使⽤单向散列函数算法,可⽤于检验消息的完整性,和通过散列密码直接以⽂本形式保存等,⽬前⼴泛使⽤的算法由MD4、MD5、SHA-1.消息摘要有两个基本属性:1.两个不同的报⽂难以⽣成相同的摘要2.难以对指定的摘要⽣成⼀个报⽂,⽽可以由改报⽂反推算出该指定的摘要代表:美国国家标准技术研究所的SHA1和⿇省理⼯学院Ronald Rivest提出的MD5(⼆)对字符串进⾏加密package test;import java.io.UnsupportedEncodingException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import sun.misc.BASE64Encoder;/*** 对字符串进⾏加密* @param str 待加密的字符串* @return 加密后的字符串* @throws NoSuchAlgorithmException 没有这种产⽣消息摘要的算法* @throws UnsupportedEncodingException*/public class Demo01 {public static String EncoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException{//确定算法MessageDigest md5 = MessageDigest.getInstance("MD5");BASE64Encoder base64en = new BASE64Encoder();//加密后的字符串String newstr = base64en.encode(md5.digest(str.getBytes("utf-8")));return newstr;}public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {String str = "0123456789";System.out.println(EncoderByMd5(str));}}(三)验证密码是否正确package test;import java.io.UnsupportedEncodingException;import java.security.NoSuchAlgorithmException;/*** 判断⽤户密码是否正确* @param newpasswd ⽤户输⼊的密码* @param oldpasswd 数据库中存储的密码--⽤户密码的摘要* @return* @throws NoSuchAlgorithmException* @throws UnsupportedEncodingException**/public class Demo02 {public static boolean checkpassword(String newpasswd, String oldpasswd) throws NoSuchAlgorithmException, UnsupportedEncodingException{if (Demo01.EncoderByMd5(newpasswd).equals(oldpasswd)) {return true;} else {return false;}}public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {System.out.println("old:"+Demo01.EncoderByMd5("123"));System.out.println("new:"+Demo01.EncoderByMd5("123"));System.out.println(checkpassword("123",Demo01.EncoderByMd5("123")));}}因为MD5是基于消息摘要原理的,消息摘要的基本特征就是很难根据摘要推算出消息报⽂,因此要验证密码是否正确,就必须对输⼊密码(消息报⽂)重新计算其摘要,和数据库中存储的摘要进⾏对⽐(即数据库中存储的其实为⽤户密码的摘要),若两个摘要相同,则说明密码正确,不同,则说明密码错误。

java加密解密算法MD5SHA1,DSA

java加密解密算法MD5SHA1,DSA

java加密解密算法MD5SHA1,DSA通常,使⽤的加密算法⽐较简便⾼效,密钥简短,加解密速度快,破译极其困难。

本⽂介绍了MD5/SHA1,DSA,DESede/DES,Diffie-Hellman的使⽤。

第1章基础知识1.1. 单钥密码体制单钥密码体制是⼀种传统的加密,是指信息的发送⽅和接收⽅共同使⽤同⼀把密钥进⾏加解密。

通常,使⽤的加密算法⽐较简便⾼效,密钥简短,加解密速度快,破译极其困难。

但是加密的安全性依靠密钥保管的安全性,在公开的上安全地传送和保管密钥是⼀个严峻的问题,并且如果在多⽤户的情况下密钥的保管安全性也是⼀个问题。

单钥密码体制的代表是美国的DES1.2. 消息摘要⼀个消息摘要就是⼀个数据块的数字指纹。

即对⼀个任意长度的⼀个数据块进⾏计算,产⽣⼀个唯⼀指印(对于SHA1是产⽣⼀个20字节的⼆进制数组)。

消息摘要有两个基本属性:两个不同的报⽂难以⽣成相同的摘要难以对指定的摘要⽣成⼀个报⽂,⽽由该报⽂反推算出该指定的摘要代表:美国国家标准技术研究所的SHA1和⿇省理⼯学院Ronald Rivest提出的MD51.3. Diffie-Hellman密钥⼀致协议密钥⼀致协议是由公开密钥密码体制的奠基⼈Diffie和Hellman所提出的⼀种思想。

先决条件,允许两名⽤户在公开媒体上交换信息以⽣成"⼀致"的,可以共享的密钥代表:指数密钥⼀致协议(Exponential Key Agreement Protocol)1.4. ⾮对称算法与公钥体系1976 年,Dittie和Hellman为解决密钥管理问题,在他们的奠基性的⼯作"密码学的新⽅向"⼀⽂中,提出⼀种密钥交换协议,允许在不安全的媒体上通过通讯双⽅交换信息,安全地传送秘密密钥。

在此新思想的基础上,很快出现了⾮对称密钥密码体制,即公钥密码体制。

在公钥体制中,加密密钥不同于解密密钥,加密密钥公之于众,谁都可以使⽤;解密密钥只有解密⼈⾃⼰知道。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

使用Java 生成MD5 编码
MD5即Message-Digest Algorithm 5(信息-摘要算法5),是一种用于产生数字签名的单项散列算法,在1991年由MIT Laboratory for Computer Science(IT计算机科学实验室)和RSA Data Security Inc(RSA数据安全公司)的Ronald L. Rivest教授开发出来,经由MD2、MD3和MD4发展而来。

MD5算法的使用不需要支付任何版权费用。

它的作用是让大容量信息在用数字签名软件签私人密匙前被"压缩"成一种保密的格式(将一个任意长度的“字节串”通过一个不可逆的字符串变换算法变换成一个128bit的大整数,换句话说就是,即使你看到源程序和算法描述,也无法将一个MD5的值变换回原始的字符串,从数学原理上说,是因为原始的字符串有无穷多个,这有点象不存在反函数的数学函数。


在Java 中,java.security.MessageDigest 中已经定义了MD5 的计算,所以我们只需要简单地调用即可得到MD5 的128 位整数。

然后将此128 位计16 个字节转换成16 进制表示即可。

代码如下:
package com.tsinghua;
/**
* MD5的算法在RFC1321 中定义
* 在RFC 1321中,给出了Test suite用来检验你的实现是否正确:
* MD5 ("") = d41d8cd98f00b204e9800998ecf8427e
* MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661
* MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72
* MD5 ("message digest") = f96b697d7cb7938d525a2f31aaf161d0
* MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b
*
* @author haogj
*
* 传入参数:一个字节数组
* 传出参数:字节数组的MD5 结果字符串
*/
public class MD5 {
public static String getMD5(byte[] source) {
String s = null;
char hexDigits[] = { // 用来将字节转换成16 进制表示的字符
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
try
{
java.security.MessageDigest md = java.security.MessageDigest.getInstance( "MD5" );
md.update( source );
byte tmp[] = md.digest(); // MD5 的计算结果是一个128 位的长整数,
// 用字节表示就是16 个字节
char str[] = new char[16 * 2]; // 每个字节用16 进制表示的话,使用两个字符,
// 所以表示成16 进制需要32 个字符
int k = 0; // 表示转换结果中对应的字符位置
for (int i = 0; i < 16; i++) { // 从第一个字节开始,对MD5 的每一个字节
// 转换成16 进制字符的转换
byte byte0 = tmp[i]; // 取第i 个字节
str[k++] = hexDigits[byte0 >>> 4 & 0xf]; // 取字节中高4 位的数字转换, // >>> 为逻辑右移,将符号位一起右移str[k++] = hexDigits[byte0 & 0xf]; // 取字节中低4 位的数字转换}
s = new String(str); // 换后的结果转换为字符串
}catch( Exception e )
{
e.printStackTrace();
}
return s;
}
}
测试代码如下:
import com.tsinghua.*;
public class TestMD5
{
public static void main( String xu[] )
{ // 计算"a" 的MD5 代码,应该为:0cc175b9c0f1b6a831c399e269772661 System.out.println( MD5.getMD5("a".getBytes()) );
}
}。

相关文档
最新文档