MD5加密算法
2016-04-08 00:00
507 查看
package com.lc.MD5src;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;
public class MyMd5Class {
private static final String NUMS_STR = "0123456789ABCDEF";
private static final Integer SALT_LENGTH = 12;
private static MessageDigest md = null;
/**
* 初始化MD5
*/
public static void init() {
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
/**
* 将16进制字符串转换成字节数组
*
@param String hex
*
@return byte[] result
*/
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) (NUMS_STR.indexOf(hexChars[pos]) << 4 | NUMS_STR
.indexOf(hexChars[pos + 1]));
}
return result;
}
/**
* 将16进制字节数组转换成字符串
*
@param byte[] bytes
*
@return String sb.toString()
*/
public static String byteToHexString(byte[] bytes) {
StringBuffer sb=new StringBuffer();
for (byte b : bytes) {
String hex=Integer.toHexString(b & 0xFF);
if (hex.length()==1) {
hex="0"+hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/**
* 验证口令是否有效
*
@param password
* @param passwordInDb
* @return
* @throws UnsupportedEncodingException
*/
public static boolean validPassword(String password,String passwordInDb) throws UnsupportedEncodingException{
byte[] pwdInDb=hexStringToByte(passwordInDb);
byte[] salt=new byte[SALT_LENGTH];
System.arraycopy(pwdInDb, 0, salt, 0, SALT_LENGTH);
init();
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 UnsupportedEncodingException
*/
public static String getEncryptedPwd(String password) throws UnsupportedEncodingException{
byte[] pwd=null;
SecureRandom random=new SecureRandom();
byte[] salt=new byte[SALT_LENGTH];
random.nextBytes(salt);
init();
md.update(salt);
md.update(password.getBytes("UTF-8"));
byte[] digest=md.digest();
pwd=new byte[digest.length+SALT_LENGTH];
System.arraycopy(salt, 0, pwd, 0, SALT_LENGTH);
System.arraycopy(digest, 0, pwd, SALT_LENGTH,digest.length );
return byteToHexString(pwd);
}
public static void main(String[] args) throws UnsupportedEncodingException {
System.out.println(getEncryptedPwd("888888"));
System.out.println(validPassword("888888", "423E1CEA748ACCC9725ABAF1AE451816DF936AC4169927DA61DC4489"));
}
}
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;
public class MyMd5Class {
private static final String NUMS_STR = "0123456789ABCDEF";
private static final Integer SALT_LENGTH = 12;
private static MessageDigest md = null;
/**
* 初始化MD5
*/
public static void init() {
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
/**
* 将16进制字符串转换成字节数组
*
@param String hex
*
@return byte[] result
*/
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) (NUMS_STR.indexOf(hexChars[pos]) << 4 | NUMS_STR
.indexOf(hexChars[pos + 1]));
}
return result;
}
/**
* 将16进制字节数组转换成字符串
*
@param byte[] bytes
*
@return String sb.toString()
*/
public static String byteToHexString(byte[] bytes) {
StringBuffer sb=new StringBuffer();
for (byte b : bytes) {
String hex=Integer.toHexString(b & 0xFF);
if (hex.length()==1) {
hex="0"+hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/**
* 验证口令是否有效
*
@param password
* @param passwordInDb
* @return
* @throws UnsupportedEncodingException
*/
public static boolean validPassword(String password,String passwordInDb) throws UnsupportedEncodingException{
byte[] pwdInDb=hexStringToByte(passwordInDb);
byte[] salt=new byte[SALT_LENGTH];
System.arraycopy(pwdInDb, 0, salt, 0, SALT_LENGTH);
init();
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 UnsupportedEncodingException
*/
public static String getEncryptedPwd(String password) throws UnsupportedEncodingException{
byte[] pwd=null;
SecureRandom random=new SecureRandom();
byte[] salt=new byte[SALT_LENGTH];
random.nextBytes(salt);
init();
md.update(salt);
md.update(password.getBytes("UTF-8"));
byte[] digest=md.digest();
pwd=new byte[digest.length+SALT_LENGTH];
System.arraycopy(salt, 0, pwd, 0, SALT_LENGTH);
System.arraycopy(digest, 0, pwd, SALT_LENGTH,digest.length );
return byteToHexString(pwd);
}
public static void main(String[] args) throws UnsupportedEncodingException {
System.out.println(getEncryptedPwd("888888"));
System.out.println(validPassword("888888", "423E1CEA748ACCC9725ABAF1AE451816DF936AC4169927DA61DC4489"));
}
}
相关文章推荐
- 数据结构(C语言)---单链表的简单应用
- 字符串转成整数大数乘法
- 有序表的最小和
- 二叉树的非递归遍历方法(前序,中序,后序)
- 数据结构学习笔记(四)
- 数据结构(9):二叉树的遍历、构建以及显示(凹入法)
- 数据结构(8):二叉树的存储结构
- 第17章 二叉树,堆和优先队列(2/3)
- 数据结构(7):树的存储结构
- 数据结构学习-串(1)
- c语言数据结构(4)
- 链表-Linked List Cycle II(判断一个链表是否有环)
- C语言和数据结构的书单-再次推荐
- 数据结构双向循环链表的C语言实现(插入,查询,删除)
- 【数据结构与算法】LCA
- 顺序结构存储串实现串通配符匹配的算法
- 第17章 二叉树,堆和优先队列(1/3)
- 哈希表处理冲突的方法
- 【动态树】【Link Cut Tree】动态树的理解(入门)
- 数据结构之线性表代码实现顺序存储,链式存储,静态链表(选自大话数据结构)