在JSP中如何实现MD5加密的方法.
- 格式:doc
- 大小:60.00 KB
- 文档页数:10
微信⼩程序MD5加密登录密码详解及实例代码微信⼩程序 MD5加密在⼩程序中,页⾯的脚本逻辑是在JsCore中运⾏,JsCore是⼀个没有窗⼝对象的环境,所以不能在脚本中使⽤window,也⽆法在脚本中操作组件。
zepto/jquery 也⽆法使⽤,因为zepto/jquery 会使⽤到window对象和document对象。
所以在微信⼩程序中不能使⽤jquery.md5.js对密码进⾏加密。
下⾯我提供⼀种MD5.js加密实例,本实例先静态演⽰,后⾯再到⼩程序中演⽰。
md5.js程序如下:/** A JavaScript implementation of the RSA Data Security, Inc. MD5 Message* Digest Algorithm, as defined in RFC 1321.* Version 1.1 Copyright (C) Paul Johnston 1999 - 2002.* Code also contributed by Greg Holt* See /site/legal.html for details.*//** 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 rol(num, cnt){return (num << cnt) | (num >>> (32 - cnt))}/** These functions implement the four basic operations the algorithm uses.*/function cmn(q, a, b, x, s, t){return safe_add(rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b)}function ff(a, b, c, d, x, s, t){return cmn((b & c) | ((~b) & d), a, b, x, s, t)}function gg(a, b, c, d, x, s, t){return cmn((b & d) | (c & (~d)), a, b, x, s, t)}function hh(a, b, c, d, x, s, t){return cmn(b ^ c ^ d, a, b, x, s, t)}function ii(a, b, c, d, x, s, t){return cmn(c ^ (b | (~d)), a, b, x, s, t)}/** Calculate the MD5 of an array of little-endian words, producing an array* of little-endian words.*/function coreMD5(x){var a = 1732584193var b = -271733879var c = -1732584194var d = 271733878for(i = 0; i < x.length; i += 16)var olda = avar oldb = bvar oldc = cvar oldd = da = ff(a, b, c, d, x[i+ 0], 7 , -680876936)d = ff(d, a, b, c, x[i+ 1], 12, -389564586) c = ff(c, d, a, b, x[i+ 2], 17, 606105819)b = ff(b, c, d, a, x[i+ 3], 22, -1044525330) a = ff(a, b, c, d, x[i+ 4], 7 , -176418897)d = ff(d, a, b, c, x[i+ 5], 12, 1200080426) c = ff(c, d, a, b, x[i+ 6], 17, -1473231341) b = ff(b, c, d, a, x[i+ 7], 22, -45705983)a = ff(a, b, c, d, x[i+ 8], 7 , 1770035416) d = ff(d, a, b, c, x[i+ 9], 12, -1958414417) c = ff(c, d, a, b, x[i+10], 17, -42063)b = ff(b, c, d, a, x[i+11], 22, -1990404162) a = ff(a, b, c, d, x[i+12], 7 , 1804603682) d = ff(d, a, b, c, x[i+13], 12, -40341101)c = ff(c, d, a, b, x[i+14], 17, -1502002290) b = ff(b, c, d, a, x[i+15], 22, 1236535329) a = gg(a, b, c, d, x[i+ 1], 5 , -165796510)d = gg(d, a, b, c, x[i+ 6], 9 , -1069501632) c = gg(c, d, a, b, x[i+11], 14, 643717713) b = gg(b, c, d, a, x[i+ 0], 20, -373897302) a = gg(a, b, c, d, x[i+ 5], 5 , -701558691) d = gg(d, a, b, c, x[i+10], 9 , 38016083)c = gg(c, d, a, b, x[i+15], 14, -660478335) b = gg(b, c, d, a, x[i+ 4], 20, -405537848) a = gg(a, b, c, d, x[i+ 9], 5 , 568446438)d = gg(d, a, b, c, x[i+14], 9 , -1019803690) c = gg(c, d, a, b, x[i+ 3], 14, -187363961) b = gg(b, c, d, a, x[i+ 8], 20, 1163531501) a = gg(a, b, c, d, x[i+13], 5 , -1444681467) d = gg(d, a, b, c, x[i+ 2], 9 , -51403784)c = gg(c, d, a, b, x[i+ 7], 14, 1735328473) b = gg(b, c, d, a, x[i+12], 20, -1926607734) a = hh(a, b, c, d, x[i+ 5], 4 , -378558)d = hh(d, a, b, c, x[i+ 8], 11, -2022574463) c = hh(c, d, a, b, x[i+11], 16, 1839030562) b = hh(b, c, d, a, x[i+14], 23, -35309556) a = hh(a, b, c, d, x[i+ 1], 4 , -1530992060) d = hh(d, a, b, c, x[i+ 4], 11, 1272893353) c = hh(c, d, a, b, x[i+ 7], 16, -155497632) b = hh(b, c, d, a, x[i+10], 23, -1094730640) a = hh(a, b, c, d, x[i+13], 4 , 681279174) d = hh(d, a, b, c, x[i+ 0], 11, -358537222) c = hh(c, d, a, b, x[i+ 3], 16, -722521979) b = hh(b, c, d, a, x[i+ 6], 23, 76029189)a = hh(a, b, c, d, x[i+ 9], 4 , -640364487) d = hh(d, a, b, c, x[i+12], 11, -421815835) c = hh(c, d, a, b, x[i+15], 16, 530742520)b = hh(b, c, d, a, x[i+ 2], 23, -995338651) a = ii(a, b, c, d, x[i+ 0], 6 , -198630844)d = ii(d, a, b, c, x[i+ 7], 10, 1126891415) c = ii(c, d, a, b, x[i+14], 15, -1416354905) b = ii(b, c, d, a, x[i+ 5], 21, -57434055)a = ii(a, b, c, d, x[i+12], 6 , 1700485571) d = ii(d, a, b, c, x[i+ 3], 10, -1894986606) c = ii(c, d, a, b, x[i+10], 15, -1051523)b = ii(b, c, d, a, x[i+ 1], 21, -2054922799) a = ii(a, b, c, d, x[i+ 8], 6 , 1873313359) d = ii(d, a, b, c, x[i+15], 10, -30611744)c = ii(c, d, a, b, x[i+ 6], 15, -1560198380) b = ii(b, c, d, a, x[i+13], 21, 1309151649) a = ii(a, b, c, d, x[i+ 4], 6 , -145523070)d = ii(d, a, b, c, x[i+11], 10, -1120210379) c = ii(c, d, a, b, x[i+ 2], 15, 718787259)b = 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 [a, b, c, d]}/** Convert an array of little-endian words to a hex string.*/function binl2hex(binarray){var hex_tab = "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 base64 encoded string.*/function binl2b64(binarray){var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" var str = ""for(var i = 0; i < binarray.length * 32; i += 6){str += tab.charAt(((binarray[i>>5] << (i%32)) & 0x3F) |((binarray[i>>5+1] >> (32-i%32)) & 0x3F))}return str}/** Convert an 8-bit character string to a sequence of 16-word blocks, stored* as an array, and append appropriate padding for MD4/5 calculation.* If any of the characters are >255, the high byte is silently ignored.*/function str2binl(str){var nblk = ((str.length + 8) >> 6) + 1 // number of 16-word blocksvar blks = new Array(nblk * 16)for(var i = 0; i < nblk * 16; i++) blks[i] = 0for(var i = 0; i < str.length; i++)blks[i>>2] |= (str.charCodeAt(i) & 0xFF) << ((i%4) * 8)blks[i>>2] |= 0x80 << ((i%4) * 8)blks[nblk*16-2] = str.length * 8return blks}/** Convert a wide-character string to a sequence of 16-word blocks, stored as* an array, and append appropriate padding for MD4/5 calculation.*/function strw2binl(str){var nblk = ((str.length + 4) >> 5) + 1 // number of 16-word blocksvar blks = new Array(nblk * 16)for(var i = 0; i < nblk * 16; i++) blks[i] = 0for(var i = 0; i < str.length; i++)blks[i>>1] |= str.charCodeAt(i) << ((i%2) * 16)blks[i>>1] |= 0x80 << ((i%2) * 16)blks[nblk*16-2] = str.length * 16return blks}/** External interface*/function hexMD5 (str) { return binl2hex(coreMD5( str2binl(str))) }function hexMD5w(str) { return binl2hex(coreMD5(strw2binl(str))) }function b64MD5 (str) { return binl2b64(coreMD5( str2binl(str))) }function b64MD5w(str) { return binl2b64(coreMD5(strw2binl(str))) }/* Backward compatibility */function calcMD5(str) { return binl2hex(coreMD5( str2binl(str))) }md5.html页⾯程序如下:<html><head><script language='javascript' src='md5.js'></script><!--引⼊MD5加密--><script language="javascript"><!--function my_do()//通过js对数据进⾏加密{//var URL="http://127.0.0.1/index.jsp?";//var dqz_out="";var dqz_in=document.my_form1.my_in_1.value;document.my_form1.my_out_1.value=hexMD5(dqz_in);document.my_form1.my_out_2.value=hexMD5w(dqz_in);document.my_form1.my_out_3.value=b64MD5(dqz_in);document.my_form1.my_out_4.value=b64MD5w(dqz_in);//URL=URL+"in="+dqz_in+"&out="+dqz_out;//my_form1.action = URL;//window.open(URL,'','');//my_form1.submit();}//--></script></head><body><form name="my_form1" method="get" action=""><p>计算MD5加密结果的例⼦<br>请输⼊加密内容:<textarea name="my_in_1" cols="70" rows="10">1234567890</textarea><br>点击右边的⽂本框显⽰结果:<br><!--mouseup某个⿏标按键被松开,mouseout⿏标从某个元素移开-->hexMD5()= <input name="my_out_1" type="text" onChange="my_do()" onMouseUp="my_do()" onMouseOut="my_do()" size="50" maxlength="50"><br> hexMD5w()=<input name="my_out_2" type="text" onChange="my_do()" onMouseUp="my_do()" onMouseOut="my_do()" size="50" maxlength="50"><br> b64MD5()= <input name="my_out_3" type="text" onChange="my_do()" onMouseUp="my_do()" onMouseOut="my_do()" size="50" maxlength="50"><br> b64MD5w()=<input name="my_out_4" type="text" onChange="my_do()" onMouseUp="my_do()" onMouseOut="my_do()" size="50" maxlength="50"><br> <br></p></form></body></html>md5.js加密效果如图:下⾯介绍微信⼩程序如何加密——模块化我们可以将⼀些公共的代码抽离成为⼀个单独的 js ⽂件,作为⼀个模块。
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加密⼯具:。
MD5加盐算法(JAVA)MD5算法是一种常用的密码散列函数,它将任意长度的输入信息通过一系列的计算转化成固定长度(一般为128位)的输出,常用于密码存储等场景。
然而,由于MD5算法的特性,存在碰撞攻击等安全问题,所以在实际使用中需要加入盐值来增加安全性。
盐值是一个随机的字符串,可以是任何长度。
它与需要加密的密码进行拼接,在进行MD5计算之前,先对拼接后的字符串进行一定的处理。
使用盐值加密后的密码是无法通过常规的MD5解密方法得到原始密码的,只能通过再次加盐加密后的结果进行对比。
下面是一个使用MD5加盐算法的JAVA示例代码:```javaimport java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.Random;public class MD5Saltpublic static void main(String[] args)String salt = generateSalt(;String encryptedPassword = encrypt(password, salt);System.out.println("原始密码:" + password);System.out.println("盐值:" + salt);System.out.println("加盐后的密码:" + encryptedPassword); }//生成随机盐值public static String generateSalRandom random = new Random(;StringBuilder sb = new StringBuilder(;for (int i = 0; i < 6; i++)sb.append((char) (random.nextInt(26) + 65));}return sb.toString(;}//使用MD5加盐算法加密密码public static String encrypt(String password, String salt) tryMessageDigest md5 = MessageDigest.getInstance("MD5"); String input = salt + password; // 将盐值和密码进行拼接byte[] bytes = md5.digest(input.getBytes();StringBuilder sb = new StringBuilder(;for (byte b : bytes)String hex = Integer.toHexString(b & 0xff);if (hex.length( == 1)sb.append("0");}sb.append(hex);}return sb.toString(;} catch (NoSuchAlgorithmException e)e.printStackTrace(;}return null;}```运行上述代码,我们将会得到如下输出结果:```盐值:EDDARH```通过加盐的处理,即使有人获得了加密后的密码,也无法通过常规的MD5解密方法得到原始密码,从而提高了密码的安全性。
js加密decode写法JS加密Decode写法:一步一步回答JS加密和解密是前端开发中常用到的技术,用于保护敏感信息或者防止数据被恶意篡改。
本文将介绍JS加密和解密的基本概念,并提供一步一步的实现方法。
一、JS加密概述JS加密是通过对数据进行转换或编码来隐藏其原始内容的过程。
它可以用于保护用户的敏感信息,如密码、信用卡号等,并防止未经授权的访问。
JS加密在前端开发中发挥着重要作用,因此了解它的实现原理以及相关的技术是很有必要的。
二、JS加密的基本原理JS加密的基本原理是利用一定的算法对原始数据进行转换,从而产生经过编码的密文。
这样,只有知道解密算法的人才能够恢复出原始数据。
三、常用的JS加密算法在实现JS加密和解密过程中,我们可以使用MD5、AES、Base64等各种算法。
这些算法具有不同的特点和应用场景,我们需要根据具体需求选择合适的算法。
1. MD5加密MD5是一种常用的哈希算法,它可将任意长度的数据转换为固定长度的哈希值。
在实际应用中,我们通常使用MD5加密用户密码等敏感信息。
下面是一个简单的使用MD5加密的例子:javascriptfunction md5Encrypt(value) {var hash = CryptoJS.MD5(value);return hash.toString();}var password = "123456";var encrypted = md5Encrypt(password);console.log(encrypted);2. AES加密AES(Advanced Encryption Standard)是一种对称加密算法,它可以加密和解密数据。
AES加密算法可以指定不同的密钥长度,其中128位密钥是最常用的。
下面是一个简单的使用AES加密的例子:javascriptfunction aesEncrypt(value, key) {var encrypted = CryptoJS.AES.encrypt(value, key);return encrypted.toString();}var data = "Hello, World!";var key = "secret_key";var encrypted = aesEncrypt(data, key);console.log(encrypted);3. Base64编码Base64是一种常用的编码方式,它可以将任意二进制数据转换为文本字符串。
jsp使用md5加密进行登录验证JSP(JavaServer Pages)是一种用于动态生成HTML页面的技术,而MD5(Message Digest Algorithm 5)是一种常用的加密算法。
在使用JSP进行登录验证时,可以结合MD5算法对用户密码进行加密存储,提高安全性。
下面我将为你详细介绍如何在JSP中使用MD5加密进行登录验证。
1.导入MD5算法库2.加密用户密码在用户注册或者保存密码的过程中,将用户输入的密码通过MD5算法进行加密,然后将加密后的密文保存到数据库中。
可以在JSP页面中编写一个函数,调用MD5算法库实现密码的加密过程。
例如:```javaString plainPassword = request.getParameter("password");String encryptedPassword = encryptPassword(plainPassword);//加密函数public String encryptPassword(String password)tryMessageDigest md = MessageDigest.getInstance("MD5");byte[] messageDigest = md.digest(password.getBytes();StringBuilder sb = new StringBuilder(;for (byte b : messageDigest)sb.append(String.format("%02x", b));}return sb.toString(;} catch (NoSuchAlgorithmException e)e.printStackTrace(;}return null;```3.登录验证在登录过程中,将用户输入的密码同样进行MD5加密,然后与存储在数据库中的密文进行比对。
MD5密码算法
MD5(Message Digest Algorithm 5)是一种常用的密码哈希函数,用于将输入的数据转换成固定长度的散列值。
MD5算法的输出长度为128位(16字节),通常表示为32个十六进制字符。
以下是MD5算法的基本步骤:
数据填充:对输入数据进行填充,使其长度满足64字节的倍数。
填充方式为在数据末尾添加一个1和若干个0,以及一个64位表示原始数据长度的二进制值。
初始化变量:定义四个32位的初始变量(A、B、C、D),作为中间计算结果的存储空间。
处理数据:将填充后的数据分割成若干个64字节的块,每次处理一个块。
分组处理:对每个块进行四轮的分组处理,每轮包括16个步骤。
更新变量:根据每个步骤中的操作,更新初始变量的值。
输出结果:处理完所有块后,将最终的初始变量值按照一定顺序连接起来,形成最终的128位(16字节)MD5散列值。
使用JAVA生成MD5编码在Java中生成MD5编码可以使用java.security.MessageDigest类。
下面是一个生成MD5编码的示例代码:```javaimport java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.Scanner;public class MD5Generatorpublic static void main(String[] args) throws NoSuchAlgorithmException//从用户输入中获取要进行MD5编码的字符串Scanner scanner = new Scanner(System.in);System.out.print("请输入要进行MD5编码的字符串: ");String input = scanner.nextLine(;scanner.close(;// 创建一个MessageDigest对象,使用MD5算法MessageDigest md = MessageDigest.getInstance("MD5");//将输入字符串转换为字节数组byte[] inputBytes = input.getBytes(;// 使用MessageDigest对象的digest(方法计算MD5哈希值byte[] md5Bytes = md.digest(inputBytes);//将字节数组转换为十六进制字符串StringBuilder hexString = new StringBuilder(;for (byte b : md5Bytes)String hex = Integer.toHexString(0xff & b);if (hex.length( == 1)hexString.append('0');}hexString.append(hex);}//打印MD5编码结果System.out.println("MD5编码结果: " + hexString.toString();}```以上代码首先从用户输入中获取要进行MD5编码的字符串。
JSP使用MD5加密进行登录验证使用MD5加密进行登录验证是常见的一种方式,本文将详细介绍如何在JSP中使用MD5加密进行登录验证。
一、什么是MD5加密MD5是一种常用的加密算法,它将任意长度的数据转化为固定长度的密文,且不可逆。
即使输入数据只发生了一个字符的改变,也会导致加密后的密文发生巨大的变化,因此MD5加密是一种非常安全的加密方式。
在应用中,用户的账号和密码都是敏感信息,为了保护用户的隐私,通常会对用户的密码进行加密保存。
使用MD5加密是一种常见的方式,因为MD5加密后的密文很难被破解,即使被黑客获取到密文,也无法还原出原始密码。
三、如何在JSP中使用MD5加密1. 引入MD5加密算法的Java类库在JSP中,我们可以使用Java提供的MessageDigest类来实现MD5加密。
首先,需要引入java.security.MessageDigest类库,在JSP页面的顶部添加如下代码:```java```2.创建MD5加密方法创建一个方法来实现MD5加密,代码如下:```javapublic String md5Encryption(String input) throws ExceptionMessageDigest messageDigest =MessageDigest.getInstance("MD5");byte[] digest = messageDigest.digest(input.getBytes();StringBuilder result = new StringBuilder(;for (byte b : digest)result.append(String.format("%02x", b & 0xff));}return result.toString(;```这个方法接受一个输入参数,将其转换为字节数组后再进行加密计算,最后将结果转换为十六进制字符串形式返回。
oracle的md5加密方法
Oracle数据库并不直接提供MD5加密函数,但是你可以使用
PL/SQL语言来实现MD5加密。
你可以通过以下方式来实现MD5加密:
1. 使用DBMS_CRYPTO包:Oracle提供了DBMS_CRYPTO包,可
以用来进行各种加密操作,包括MD5。
你可以使用该包中的HASH函
数来计算MD5值。
下面是一个示例:
sql.
DECLARE.
v_input VARCHAR2(4000) := 'your_input_string';
v_hash RAW(16);
BEGIN.
v_hash :=
DBMS_CRYPTO.HASH(UTL_RAW.CAST_TO_RAW(v_input),
DBMS_CRYPTO.HASH_MD5);
DBMS_OUTPUT.PUT_LINE(RAWTOHEX(v_hash));
END;
2. 使用Java加密库,你也可以通过在Oracle数据库中调用Java加密库来实现MD5加密。
你需要先创建一个Java存储过程或
函数,然后在PL/SQL中调用它来实现MD5加密。
无论哪种方法,都需要注意对加密结果进行适当的存储和处理,以确保安全性和正确性。
希望这些信息能够帮助到你实现在Oracle
数据库中实现MD5加密。
在JSP中如何实现MD5加密的方法这篇文章主要介绍了在JSP中如何实现MD5加密的方法,较为详细的分析了JSP采用MD5加密的功能、特点及实现技巧,具有一定参考借鉴价值,在各种应用系统的开发中,经常需要存储用户信息,很多地方都要存储用户密码,而将用户密码直接存储在服务器上显然是不安全的,本文简要介绍在JSP中如何实现MD5加密的方法,希望能抛砖引玉。
(一)消息摘要简介一个消息摘要就是一个数据块的数字指纹。
即对一个任意长度的一个数据块进行计算,产生一个唯一指印(对于SHA1是产生一个20字节的二进制数组)。
消息摘要是一种与消息认证码结合使用以确保消息完整性的技术。
主要使用单向散列函数算法,可用于检验消息的完整性,和通过散列密码直接以文本形式保存等,目前广泛使用的算法有MD4、MD5、SHA-1。
消息摘要有两个基本属性:两个不同的报文难以生成相同的摘要难以对指定的摘要生成一个报文,而可以由该报文反推算出该指定的摘要。
Java源码import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;public class MD5Digest{private MessageDigest __md5 = null;private StringBuffer __digestBuffer = null;public MD5Digest()throws NoSuchAlgorithmException{__md5 = MessageDigest.getInstance("MD5");__digestBuffer = new StringBuffer();}public String md5crypt(String s){__digestBuffer.setLength(0);byte abyte0[] = __md5.digest(s.getBytes());for(int i = 0; i < abyte0.length; i++)__digestBuffer.append(toHex(abyte0[i]));return __digestBuffer.toString();}public String toHex(byte one){String HEX="0123456789ABCDEF";char[] result=new char[2];result[0]=HEX.charAt((one & 0xf0) >> 4);result[1]=HEX.charAt(one & 0x0f);String mm=new String(result);return mm;}}/************************************************MD5 算法的Java Bean@author:Topcat TuppinLast Modified:10,Mar,2001*************************************************/package beartool;import ng.reflect.*;/*************************************************md5 类实现了RSA Data Security, Inc.在提交给IETF的RFC1321中的MD5 message-digest 算法。
*************************************************/public class MD5 {/* 下面这些S11-S44实际上是一个4*4的矩阵,在原始的C实现中是用#define 实现的,这里把它们实现成为static final是表示了只读,切能在同一个进程空间内的多个Instance间共享*/static final int S11 = 7;static final int S12 = 12;static final int S13 = 17;static final int S14 = 22;static final int S21 = 5;static final int S22 = 9;static final int S23 = 14;static final int S24 = 20;static final int S31 = 4;static final int S32 = 11;static final int S33 = 16;static final int S34 = 23;static final int S41 = 6;static final int S42 = 10;static final int S43 = 15;static final int S44 = 21;static final byte[] PADDING = { -128, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };/* 下面的三个成员是MD5计算过程中用到的3个核心数据,在原始的C实现中被定义到MD5_CTX结构中*/private long[] state = new long[4];// state (ABCD)private long[] count = new long[2];// number of bits, modulo 2^64 (lsb first)private byte[] buffer = new byte[64]; // input buffer/* digestHexStr是MD5的唯一一个公共成员,是最新一次计算结果的16进制ASCII表示.*/public String digestHexStr;/* digest,是最新一次计算结果的2进制内部表示,表示128bit的MD5值.*/private byte[] digest = new byte[16];/*getMD5ofStr是类MD5最主要的公共方法,入口参数是你想要进行MD5变换的字符串返回的是变换完的结果,这个结果是从公共成员digestHexStr取得的.*/public String getMD5ofStr(String inbuf) {md5Init();md5Update(inbuf.getBytes(), inbuf.length());md5Final();digestHexStr = "";for (int i = 0; i < 16; i++) {digestHexStr += byteHEX(digest[i]);}return digestHexStr;}// 这是MD5这个类的标准构造函数,JavaBean要求有一个public的并且没有参数的构造函数public MD5() {md5Init();return;}/* md5Init是一个初始化函数,初始化核心变量,装入标准的幻数*/private void md5Init() {count[0] = 0L;count[1] = 0L;///* Load magic initialization constants.state[0] = 0x67452301L;state[1] = 0xefcdab89L;state[2] = 0x98badcfeL;state[3] = 0x10325476L;return;}/* F, G, H ,I 是4个基本的MD5函数,在原始的MD5的C实现中,由于它们是简单的位运算,可能出于效率的考虑把它们实现成了宏,在java中,我们把它们实现成了private方法,名字保持了原来C中的。
*/private long F(long x, long y, long z) {return (x & y) | ((~x) & z);}private long G(long x, long y, long z) {return (x & z) | (y & (~z));}private long H(long x, long y, long z) {return x ^ y ^ z;}private long I(long x, long y, long z) {return y ^ (x | (~z));}/*FF,GG,HH和II将调用F,G,H,I进行近一步变换FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.Rotation is separate from addition to prevent recomputation.*/private long FF(long a, long b, long c, long d, long x, long s,long ac) {a += F (b, c, d) + x + ac;a = ((int) a << s) | ((int) a >>> (32 - s));a += b;return a;}private long GG(long a, long b, long c, long d, long x, long s,long ac) {a += G (b, c, d) + x + ac;a = ((int) a << s) | ((int) a >>> (32 - s));a += b;return a;}private long HH(long a, long b, long c, long d, long x, long s,long ac) {a += H (b, c, d) + x + ac;a = ((int) a << s) | ((int) a >>> (32 - s));a += b;return a;}private long II(long a, long b, long c, long d, long x, long s,long ac) {a += I (b, c, d) + x + ac;a = ((int) a << s) | ((int) a >>> (32 - s));a += b;return a;}/*md5Update是MD5的主计算过程,inbuf是要变换的字节串,inputlen是长度,这个函数由getMD5ofStr调用,调用之前需要调用md5init,因此把它设计成private的*/private void md5Update(byte[] inbuf, int inputLen) {int i, index, partLen;byte[] block = new byte[64];index = (int)(count[0] >>> 3) & 0x3F;// /* Update number of bits */if ((count[0] += (inputLen << 3)) < (inputLen << 3))count[1]++;count[1] += (inputLen >>> 29);partLen = 64 - index;// Transform as many times as possible.if (inputLen >= partLen) {md5Memcpy(buffer, inbuf, index, 0, partLen);md5Transform(buffer);for (i = partLen; i + 63 < inputLen; i += 64) {md5Memcpy(block, inbuf, 0, i, 64);md5Transform (block);}index = 0;} elsei = 0;///* Buffer remaining input */md5Memcpy(buffer, inbuf, index, i, inputLen - i);}/*md5Final整理和填写输出结果*/private void md5Final () {byte[] bits = new byte[8];int index, padLen;///* Save number of bits */Encode (bits, count, 8);///* Pad out to 56 mod 64.index = (int)(count[0] >>> 3) & 0x3f;padLen = (index < 56) ? (56 - index) : (120 - index);md5Update (PADDING, padLen);///* Append length (before padding) */md5Update(bits, 8);///* Store state in digest */Encode (digest, state, 16);}/* md5Memcpy是一个内部使用的byte数组的块拷贝函数,从input的inpos开始把len长度的字节拷贝到output的outpos位置开始*/private void md5Memcpy (byte[] output, byte[] input,int outpos, int inpos, int len){int i;for (i = 0; i < len; i++)output[outpos + i] = input[inpos + i];}/*md5Transform是MD5核心变换程序,有md5Update调用,block是分块的原始字节*/private void md5Transform (byte block[]) {long a = state[0], b = state[1], c = state[2], d = state[3];long[] x = new long[16];Decode (x, block, 64);/* Round 1 */a = FF (a, b, c, d, x[0], S11, 0xd76aa478L); /* 1 */d = FF (d, a, b, c, x[1], S12, 0xe8c7b756L); /* 2 */c = FF (c, d, a, b, x[2], S13, 0x242070dbL); /* 3 */b = FF (b, c, d, a, x[3], S14, 0xc1bdceeeL); /* 4 */a = FF (a, b, c, d, x[4], S11, 0xf57c0fafL); /* 5 */d = FF (d, a, b, c, x[5], S12, 0x4787c62aL); /* 6 */c = FF (c, d, a, b, x[6], S13, 0xa8304613L); /* 7 */b = FF (b, c, d, a, x[7], S14, 0xfd469501L); /* 8 */a = FF (a, b, c, d, x[8], S11, 0x698098d8L); /* 9 */d = FF (d, a, b, c, x[9], S12, 0x8b44f7afL); /* 10 */c = FF (c, d, a, b, x[10], S13, 0xffff5bb1L); /* 11 */b = FF (b, c, d, a, x[11], S14, 0x895cd7beL); /* 12 */d = FF (d, a, b, c, x[13], S12, 0xfd987193L); /* 14 */ c = FF (c, d, a, b, x[14], S13, 0xa679438eL); /* 15 */ b = FF (b, c, d, a, x[15], S14, 0x49b40821L); /* 16 */ /* Round 2 */a = GG (a, b, c, d, x[1], S21, 0xf61e2562L); /* 17 */ d = GG (d, a, b, c, x[6], S22, 0xc040b340L); /* 18 */ c = GG (c, d, a, b, x[11], S23, 0x265e5a51L); /* 19 */b = GG (b, c, d, a, x[0], S24, 0xe9b6c7aaL); /* 20 */ a = GG (a, b, c, d, x[5], S21, 0xd62f105dL); /* 21 */ d = GG (d, a, b, c, x[10], S22, 0x2441453L); /* 22 */c = GG (c, d, a, b, x[15], S23, 0xd8a1e681L); /* 23 */ b = GG (b, c, d, a, x[4], S24, 0xe7d3fbc8L); /* 24 */ a = GG (a, b, c, d, x[9], S21, 0x21e1cde6L); /* 25 */d = GG (d, a, b, c, x[14], S22, 0xc33707d6L); /* 26 */ c = GG (c, d, a, b, x[3], S23, 0xf4d50d87L); /* 27 */ b = GG (b, c, d, a, x[8], S24, 0x455a14edL); /* 28 */ a = GG (a, b, c, d, x[13], S21, 0xa9e3e905L); /* 29 */ d = GG (d, a, b, c, x[2], S22, 0xfcefa3f8L); /* 30 */c = GG (c, d, a, b, x[7], S23, 0x676f02d9L); /* 31 */ b = GG (b, c, d, a, x[12], S24, 0x8d2a4c8aL); /* 32 */ /* Round 3 */a = HH (a, b, c, d, x[5], S31, 0xfffa3942L); /* 33 */d = HH (d, a, b, c, x[8], S32, 0x8771f681L); /* 34 */ c = HH (c, d, a, b, x[11], S33, 0x6d9d6122L); /* 35 */ b = HH (b, c, d, a, x[14], S34, 0xfde5380cL); /* 36 */ a = HH (a, b, c, d, x[1], S31, 0xa4beea44L); /* 37 */ d = HH (d, a, b, c, x[4], S32, 0x4bdecfa9L); /* 38 */ c = HH (c, d, a, b, x[7], S33, 0xf6bb4b60L); /* 39 */ b = HH (b, c, d, a, x[10], S34, 0xbebfbc70L); /* 40 */ a = HH (a, b, c, d, x[13], S31, 0x289b7ec6L); /* 41 */ d = HH (d, a, b, c, x[0], S32, 0xeaa127faL); /* 42 */ c = HH (c, d, a, b, x[3], S33, 0xd4ef3085L); /* 43 */ b = HH (b, c, d, a, x[6], S34, 0x4881d05L); /* 44 */ a = HH (a, b, c, d, x[9], S31, 0xd9d4d039L); /* 45 */ d = HH (d, a, b, c, x[12], S32, 0xe6db99e5L); /* 46 */ c = HH (c, d, a, b, x[15], S33, 0x1fa27cf8L); /* 47 */ b = HH (b, c, d, a, x[2], S34, 0xc4ac5665L); /* 48 */ /* Round 4 */a = II (a, b, c, d, x[0], S41, 0xf4292244L); /* 49 */d = II (d, a, b, c, x[7], S42, 0x432aff97L); /* 50 */c = II (c, d, a, b, x[14], S43, 0xab9423a7L); /* 51 */ b = II (b, c, d, a, x[5], S44, 0xfc93a039L); /* 52 */a = II (a, b, c, d, x[12], S41, 0x655b59c3L); /* 53 */c = II (c, d, a, b, x[10], S43, 0xffeff47dL); /* 55 */b = II (b, c, d, a, x[1], S44, 0x85845dd1L); /* 56 */a = II (a, b, c, d, x[8], S41, 0x6fa87e4fL); /* 57 */d = II (d, a, b, c, x[15], S42, 0xfe2ce6e0L); /* 58 */c = II (c, d, a, b, x[6], S43, 0xa3014314L); /* 59 */b = II (b, c, d, a, x[13], S44, 0x4e0811a1L); /* 60 */a = II (a, b, c, d, x[4], S41, 0xf7537e82L); /* 61 */d = II (d, a, b, c, x[11], S42, 0xbd3af235L); /* 62 */c = II (c, d, a, b, x[2], S43, 0x2ad7d2bbL); /* 63 */b = II (b, c, d, a, x[9], S44, 0xeb86d391L); /* 64 */state[0] += a;state[1] += b;state[2] += c;state[3] += d;}/*Encode把long数组按顺序拆成byte数组,因为java的long类型是64bit的,只拆低32bit,以适应原始C实现的用途*/private void Encode (byte[] output, long[] input, int len) {int i, j;for (i = 0, j = 0; j < len; i++, j += 4) {output[j] = (byte)(input[i] & 0xffL);output[j + 1] = (byte)((input[i] >>> 8) & 0xffL);output[j + 2] = (byte)((input[i] >>> 16) & 0xffL);output[j + 3] = (byte)((input[i] >>> 24) & 0xffL);}}/*Decode把byte数组按顺序合成成long数组,因为java的long类型是64bit的,只合成低32bit,高32bit清零,以适应原始C实现的用途*/private void Decode (long[] output, byte[] input, int len) {int i, j;for (i = 0, j = 0; j < len; i++, j += 4)output[i] = b2iu(input[j]) |(b2iu(input[j + 1]) << 8) |(b2iu(input[j + 2]) << 16) |(b2iu(input[j + 3]) << 24);return;}/*b2iu是我写的一个把byte按照不考虑正负号的原则的"升位"程序,因为java没有unsigned运算*/public static long b2iu(byte b) {return b < 0 ? b & 0x7F + 128 : b;}/*byteHEX(),用来把一个byte类型的数转换成十六进制的ASCII表示,因为java中的byte的toString无法实现这一点,我们又没有C语言中的sprintf(outbuf,"%02X",ib)*/public static String byteHEX(byte ib) {char[] Digit = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };char [] ob = new char[2];ob[0] = Digit[(ib >>> 4) & 0X0F];ob[1] = Digit[ib & 0X0F];String s = new String(ob);return s;}public static void main(String args[]) {MD5 m = new MD5();if (Array.getLength(args) == 0) {//如果没有参数,执行标准的Test SuiteSystem.out.println("MD5 Test suite:");System.out.println("MD5(\"\"):"+m.getMD5ofStr(""));System.out.println("MD5(\"a\"):"+m.getMD5ofStr("a"));System.out.println("MD5(\"abc\"):"+m.getMD5ofStr("abc"));System.out.println("MD5(\"message digest\"):"+m.getMD5ofStr("message digest"));System.out.println("MD5(\"abcdefghijklmnopqrstuvwxyz\"):"+m.getMD5ofStr("klmnopqrstuvwxyz"));System.out.println("MD5(\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\"):"+ m.getMD5ofStr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"));}elseSystem.out.println("MD5(" + args[0] + ")=" + m.getMD5ofStr(args[0]));}}JSP中的使用方法<%@ page language='java' %><jsp:useBean id='oMD5' scope='request' class='beartool.MD5'/><%@ page import='java.util.*'%><%@ page import='java.sql.*'%><html><body><%String userid = request.getParameter("UserID");//获取用户输入UserIDString password = request.getParameter("Password"); //获取用户输入的PasswordString pwdmd5 = oMD5.getMD5ofStr(password);//计算MD5的值PrintWriter rp = response.getWriter();Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");Connection con = DriverManager.getConnection("jdbc:odbc:community", "", "");Statement stmt = Statement();ResultSet rs = stmt.executeQuery("select * from users where userID ='"+userid+"' and pwdmd5= '" + pwdmd5+"'" ); if (rs.next()){rp.print("Login OK");}else{rp.print("Login Fail");}stmt.close();con.close();%></body></html>。