您的位置:首页 > 编程语言 > Java开发

java.security.MessageDigest

2014-11-07 17:17 288 查看

最近研究jive,把jive源码移植到我的myeclipse中,在使jive跑起来的过程中来理解jive的技术点,首先是用户登录,了解了登录模块需要的加密算法。

java.security.MessageDigest简介: 

常用场景: 
    一般我们数据库登录时,需要对用户的密码进行加密操作,不直接将原密码保存到数据库中,用户登录时候也是采用加密算法进行比较 

本案只介绍使用java.security.MessageDigest,对数据进行加密操作。该方法是单向的加密算法 

详细代码如下所表示: 
//基本的公用类 

package com.xue.security;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SecurityTest
{
// MD5是16位,SHA是20位(这是两种报文摘要的算法) 定义两种加密算法的类
private static final String MD5_ALGORITHM = "MD5";

private static final String SHA_1_ALGORITHM = "SHA-1";

/** 十六进制 */
private static final int HEX_NUM = 16;

/** 十六进制码 */
private static final String[] HEX_DIGITS = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d",
"e", "f"};

}

//对数据进行加密的方法: 

/**对语句进行单向加密
* <功能详细描述>
* @param message   需要加密的信息
* @return
* @throws NoSuchAlgorithmException
* @throws IOException [参数说明]
*
* @return byte[] [返回类型说明]
* @exception throws [违例类型] [违例说明]
* @see [类、类#方法、类#成员]
*/
public byte[] encrypt(String message)
throws NoSuchAlgorithmException, IOException
{

// 加密
MessageDigest encrypt = MessageDigest.getInstance(SecurityTest.MD5_ALGORITHM);

// 添加需要加密的信息
encrypt.update(message.getBytes());

// 对信息信息加密
byte[] encryptMD5 = encrypt.digest();

//获得加密算法
System.out.println(encrypt.getAlgorithm());

//得到加密算法的长度
System.out.println(encrypt.getDigestLength());
return encryptMD5;

}

//对数据进行base64转发,有可能某些byte是不可打印的字符。 
存在几种处理方式, 
方式一:转化为base64 

/** 将数据转化为BASE64码进行保存
* <功能详细描述>
* @param encryptMD5
* @return [参数说明]
*
* @return String [返回类型说明]
* @exception throws [违例类型] [违例说明]
* @see [类、类#方法、类#成员]
*/
public String transform(byte[] encryptMD5)
{
//对数据进行base64转发,有可能某些byte是不可打印的字符。
BASE64Encoder basEncoder = new BASE64Encoder();
return basEncoder.encode(encryptMD5);
}

//将byte转化为16进制 
有两种方式转化为16进制 
第一种: 

/**
* 转化方式二
* 2进制转16进制方式二 <功能详细描述>
*
* @param b
* @return [参数说明]
*
* @return String [返回类型说明]
* @exception throws [违例类型] [违例说明]
* @see [类、类#方法、类#成员]
*/
public static String byte2hex(byte[] b)
{
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++)
{
stmp = (java.lang.Integer.toHexString(b
& 0XFF));

if (stmp.length() == 1)
{
hs = hs + "0" + stmp;
}
else
{
hs = hs + stmp;
}
}

return hs.toUpperCase();

}

第二种: 

/**
* 转化方式二
* 将数据转化为16进制进行保存,因为有些byte是不能打印的字符 字节数组的转化
*
* @param b 字节数组
* @return [参数说明]
*
* @return String [返回类型说明]
* @exception throws [违例类型] [违例说明]
* @see [类、类#方法、类#成员]
*/
public static String b
4000
yteArrayToHexString(byte[] b)
{
StringBuffer result = new StringBuffer(128);
for (int i = 0; i < b.length; i++)
{
result.append(byteToHexString(b[i]));
}
return result.toString();
}

/**
* 转化方式二
* 将数据转化为16进制进行保存,因为有些byte是不能打印的字符 单字节转化
*
* @param b
* @return [参数说明]
*
* @return String [返回类型说明]
* @exception throws [违例类型] [违例说明]
* @see [类、类#方法、类#成员]
*/
public static String byteToHexString(byte b)
{
int n = b;
if (0 > n)
{
n = 256 + n;
}
int d1 = n / HEX_NUM;
int d2 = n % HEX_NUM;
return HEX_DIGITS[d1] + HEX_DIGITS[d2];
}


最后将Byte数组转化为String保存到DB中 

比较两个加密byte数组是否相同 

/** 将数据转化为BASE64码进行保存
* <功能详细描述>
* @param encryptMD5
* @return [参数说明]
*
* @return String [返回类型说明]
* @exception throws [违例类型] [违例说明]
* @see [类、类#方法、类#成员]
*/
public String transform(byte[] encryptMD5)
{
//对数据进行base64转发,有可能某些byte是不可打印的字符。
BASE64Encoder basEncoder = new BASE64Encoder();
return basEncoder.encode(encryptMD5);
}


测试函数 

public static void main(String[] args)
throws NoSuchAlgorithmException, IOException
{
SecurityTest t = new SecurityTest();
//得到加密数组
byte[] data = t.encrypt("我的测试信息!");
//转化为字符串,采用base编码方式
String baseResult = SecurityTest.transform(data);

String hexResult = SecurityTest.byte2hex(data);

System.out.println("baseResult"+baseResult);
System.out.println("hexResult"+hexResult);

}
结果:
baseResult:+HwBij+rZkL3Ihsxx2xFsA==
hexResult:F87C018A3FAB6642F7221B31C76C45B0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: