MD5算法学习及其对用户密码加密的应用
来源:互联网 发布:多益网络客户端下载 编辑:程序博客网 时间:2024/05/16 09:14
前段时间有个客户提交了一个需求,说我们的系统中,subscriber的密码是以明文方式存在系统中的,不安全(汗颜啊……)。因此我们要做点改进,把subscriber的密码加一下密。借着机会,我也学习学习一下MD5算法(再次汗颜……)。
MD5(Message-Digest Algorithm 5)加密算法是一种不可逆的算法,也即,即使源程序和算法描述可见,也无法将一个MD5值恢复到加密前的值。
Message-Digest指的是字节串的Hash变换。这种变换只与字节的值有关,与字符集和编码方式无关。它将任意长字节串变换成一个128bit的 整数。相同的字节串计算出的MD5值总是相同的;不同的字节串计算出的MD5值是肯定不相同的。因此,MD5加密算法在日常生活中有许多应用。例如在很多系统和软件中,登录时所用的用户密码是以MD5值的方式保存的。用户注册时,将其设置的密码计算为MD5值并保存在数据库中。用户登录时,系统根据登录者输入的用户名计算出MD5值,与数据库中保存的该用户密码的MD5值进行比较,如果相同,才允许用户登录。
MD5的另一个应用就是防止文件被“篡改”。如果一个文件被修改过,那么修改之前对文件计算出的MD5值和修改之后的MD5值是肯定不相同的。在实际开发过程中,developer们有时也会遇到MD5的应用。比如在version1,developer deliver了一个包module.rpm。然后我进行了修改,然后打包又生成了一个module.rpm,但是我想确认一下我现在做的rpm包是否是我做了修改后生成的包(有的时候也的确会出现这类问题)。我就需要比较一下前一个版本和后一个版本的module.rpm的MD5值。
为了完成工作,我添加了一个工具类HashMD5,提供方法来对输入参数进行加密。
import java.security.*;
/**
* @author Sam
* Hash the input password, using MD5 algorithm.
*/
public class HashMD5 {
public final static char hexChar[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
public static String convertBytesToString(byte[] bytes) {
StringBuffer result = new StringBuffer();
for (int i=0; i<bytes.length; i++) {
result.append(convertByteToHex(bytes[i]));
}
return result.toString();
}
private static String convertByteToHex(byte b) {
int n = b;
int d1 = 0;
int d2 = 0;
if (n<0){
n = 256 + n;
}
d1 = n/16;
d2 = n%16;
StringBuffer str = new StringBuffer();
str.append(hexChar[d1]).append(hexChar[d2]);
return str.toString();
}
public static String Encryption(String inputpassword) {
String encryptedPassword = null;
try {
byte[] strTmp = inputpassword.getBytes();
MessageDigest mdTmp = MessageDigest.getInstance("MD5");
mdTmp.update(strTmp);
byte[] md = mdTmp.digest();
encryptedPassword = convertBytesToString (md);
return new String(encryptedPassword);
}
catch (Exception e){
return null;
}
}
}
/**
* @author Sam
* Hash the input password, using MD5 algorithm.
*/
public class HashMD5 {
public final static char hexChar[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
public static String convertBytesToString(byte[] bytes) {
StringBuffer result = new StringBuffer();
for (int i=0; i<bytes.length; i++) {
result.append(convertByteToHex(bytes[i]));
}
return result.toString();
}
private static String convertByteToHex(byte b) {
int n = b;
int d1 = 0;
int d2 = 0;
if (n<0){
n = 256 + n;
}
d1 = n/16;
d2 = n%16;
StringBuffer str = new StringBuffer();
str.append(hexChar[d1]).append(hexChar[d2]);
return str.toString();
}
public static String Encryption(String inputpassword) {
String encryptedPassword = null;
try {
byte[] strTmp = inputpassword.getBytes();
MessageDigest mdTmp = MessageDigest.getInstance("MD5");
mdTmp.update(strTmp);
byte[] md = mdTmp.digest();
encryptedPassword = convertBytesToString (md);
return new String(encryptedPassword);
}
catch (Exception e){
return null;
}
}
}
在原来的程序中保存密码的地方,调用一下HashMD5.Encryption()函数,将加密结果保存在数据库中。然后用户登录的时候校验一下输入密码的MD5值。这下大功告成了!
- MD5算法学习及其对用户密码加密的应用
- C#实现MD5算法,对用户密码加密
- 使用MD5对用户密码加密与解密
- wordpress用户密码加密原理及其算法分析
- C# MD5加密用户密码的类
- 用md5加密用户密码
- 使用MD5加密用户密码
- oracle 用户密码md5加密
- MD5 用户密码加密
- 用md5加密用户密码
- 使用MD5加密用户密码
- Md5加密原理及其实现算法
- 使用JDK提供的MD5算法对字符串进行加密
- JIRA 对用户密码加密的代码
- JIRA 对用户密码加密的代码
- MD5 JAVA用户密码加密实现
- MD5 JAVA用户密码加密实现
- 用户密码MD5和SHA加密
- c语言中产生随机数
- 程序员接项目的四点技巧
- 【原创】GridView实现多表头
- 朋友站点
- 李嘉诚:性格才是命运的决定因素
- MD5算法学习及其对用户密码加密的应用
- 从两道经典试题谈C/C++中联合体(union)的使用
- 使用JAVASCRIPT实现弹出框,过一段时间自动消失
- TCP/IP学习笔记之五 --- RARP: 逆地址解析协议
- Vector、ArrayList、List使用深入剖析
- 各种语言的 Hello world 程序
- Asp.Net缓存Cache使用一例
- JAVA新手 如何不走弯路的把JAVA学好 加124145214交流
- Tacert命令用法