MessageDigest的功能及⽤法(加密解密)SHA_MD5 MessageDigest的功能及⽤法
MessageDigest 类为应⽤程序提供信息摘要算法的功能,如 MD5 或 SHA 算法。信息摘要是安全的单向哈希函数,它接收任意⼤⼩的数据,并输出固定长度的哈希值。MessageDigest 对象开始被初始化。该对象通过使⽤ update()⽅法处理数据。任何时候都可以调⽤ reset()⽅法重置摘要。⼀旦所有需要更新的数据都已经被更新了,应该调⽤digest() ⽅法之⼀完成哈希计算。
对于给定数量的更新数据,digest ⽅法只能被调⽤⼀次。在调⽤ digest 之后,MessageDigest 对象被重新设置成其初始状态。
1、public static MessageDigest getInstance(String algorithm)
throws NoSuchAlgorithmException
返回实现指定摘要算法的 MessageDigest 对象。
algorithm - 所请求算法的名称
2、public static MessageDigest getInstance(String algorithm,
String provider)
throws NoSuchAlgorithmException,
NoSuchProviderException
返回实现指定摘要算法的 MessageDigest 对象。
algorithm - 所请求算法的名称
provider - 提供者的名称。
3、public void update(byte[] input)
使⽤指定的 byte数组更新摘要。
4、public byte[] digest()
通过执⾏诸如填充之类的最终操作完成哈希计算。在调⽤此⽅法之后,摘要被重置。
5、public static boolean isEqual(byte[] digesta,
byte[] digestb)
⽐较两个摘要的相等性。做简单的字节⽐较。
注意:Provider可以通过 Java.Providers() ⽅法获取已注册提供者列表。⽐较常⽤的有“SUN”
SUN提供的常⽤的算法名称有:MD2
MD5
SHA-1
SHA-256
SHA-384
SHA-512
Code举例:
import java.security.*;
public class myDigest {
public static void main(String[] args)  {
myDigest my=new myDigest();
}
public void testDigest()
{
try{
String myinfo="我的测试信息";
//java.security.MessageDigest alg=java.Instance("MD5");
java.security.MessageDigest alga=java.Instance("SHA-1");
alga.Bytes());
byte[] digesta=alga.digest();
System.out.println("本信息摘要是:"+byte2hex(digesta));
//通过某中⽅式传给其他⼈你的信息(myinfo)和摘要(digesta) 对⽅可以判断是否更改或传输正常
java.security.MessageDigest algb=java.Instance("SHA-1");
algb.Bytes());
if(algb.isEqual(digesta,algb.digest())) {
System.out.println("信息检查正常");
}
else
{
System.out.println("摘要不相同");
}
}
catch(java.security.NoSuchAlgorithmException ex) {
System.out.println("⾮法摘要算法");
}
}
public String byte2hex(byte[] b) //⼆⾏制转字符串
{
String hs="";
String stmp="";
for(int n=0;n<b.length;n++)
{
stmp=(java.HexString(b[n] & 0XFF));
if(stmp.length()==1) hs=hs+"0"+stmp;
else hs=hs+stmp;
if(n<b.length-1)  hs=hs+":";
}
UpperCase();
}
}
上⼀篇:
下⼀篇:
________________________________________________________________________________________________________⽬录
Java MD5Utils
1
2
3
4
5
6
7
8
9
10
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33/**
* <html>
* <body>
*  <P> Copyright 1994 JsonInternational</p>
*  <p> All rights reserved.</p>
*  <p> Created on 19941115</p>
*  <p> Created by Jason</p>
*  </body>
* </html>
*/
package cn.ucaner.alpaca.pt; import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import org.apachemons.lang3.StringUtils;
/**
* @Package:cn.ucaner.alpaca.pt  * @ClassName:MD5Utils
* @Description:  <p> MD5加密⼯具    By Jason </p> * @Author: - Jason
* @CreatTime:2018年5⽉24⽇下午9:40:31
* @Modify By:
* @ModifyTime:  2018年5⽉24⽇
16进制字符串转16进制数组
* @Modify marker:
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 6
5 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101* @Modify marker:
* @version    V1.0
*/
class MD5Utils {
protected final static String MD5_KEY = "MD5";
protected final static String SHA_KEY = "SHA1";
/**
* @param value
* @param key
* @return
*/
protected static String encrypt(String value,String key) {
try{
// 拿到⼀个MD5转换器(如果想要SHA1参数换成”SHA1”)
MessageDigest messageDigest = Instance(key);
// 输⼊的字符串转换成字节数组
byte[] inputByteArray = Bytes();
// inputByteArray是输⼊字符串转换得到的字节数组
messageDigest.update(inputByteArray);
// 转换并返回结果,也是字节数组,包含16个元素
byte[] resultByteArray = messageDigest.digest();
// 字符数组转换成字符串返回
return byteArrayToHex(resultByteArray);
} catch(NoSuchAlgorithmException e) {
return null;
}
}
/**
* 字节数组转换为hex
* @param byteArray
* @return
*/
private static String byteArrayToHex(byte[] byteArray) {
/
/ ⾸先初始化⼀个字符数组,⽤来存放每个16进制字符
char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9','A', 'B', 'C', 'D', 'E', 'F'};
// new⼀个字符数组,这个就是⽤来组成结果字符串的(解释⼀下:⼀个byte是⼋位⼆进制,也就是2位⼗六进制字符(2的8次⽅等于16的2次⽅))        char[] resultCharArray = new char[byteArray.length * 2];
// 遍历字节数组,通过位运算(位运算效率⾼),转换成字符放到字符数组中去
int index = 0;
for(byte b : byteArray) {
resultCharArray[index++] = hexDigits[b >>> 4& 0xf];
resultCharArray[index++] = hexDigits[b & 0xf];
}
// 字符数组组合成字符串返回
return new String(resultCharArray);
}
/**
* 获得16位的加密字符
* @param str
* @return
* @throws NoSuchAlgorithmException
*/
public static String getMd5String16(String str) throws NoSuchAlgorithmException {
String md5str = getMd5String32(str).substring(8);
return md5str.substring(0, md5str.length() - 8);
}
/**
* 获得24位的MD5加密字符
* @param str
* @return
* @throws NoSuchAlgorithmException
*/
public static String getMd5String24(String str) throws NoSuchAlgorithmException {
String md5str = getMd5String32(str).substring(4);
return md5str.substring(0, md5str.length() - 4);
}
/
**
* 获得32位的MD5加密算法
* @param str
* @return
* @throws NoSuchAlgorithmException
*/
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169    */
public static String getMd5String32(String str) throws NoSuchAlgorithmException {
MessageDigest md = Instance("MD5");
md.Bytes());
byte b[] = md.digest();
int i;
StringBuffer 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");
buf.HexString(i));
}
String();
}
/**
* 获取MD5密码
* @param password
* @param salt
* @return
* @throws NoSuchAlgorithmException
*/
public static String getMD5Pwd(String password, String salt) throws NoSuchAlgorithmException {        String result = null;
if(StringUtils.isNotBlank(salt)) {
result = getMD5(getMD5(password) + salt);
} else{
result = getMD5(password);
}
return result;
}
/**
* 获取MD5加密数据
* @param input
* @return
* @throws NoSuchAlgorithmException
*/
public static String getMD5(String input) throws NoSuchAlgorithmException {
String result = input;
if(input != null) {
MessageDigest md = Instance("MD5"); //or "SHA-1"
md.Bytes());
BigInteger hash = new BigInteger(1, md.digest());
result = String(16);
while(result.length() < 32) {//40 for SHA-1
result = "0"+ result;
}
}
return result;
}
/**
* For test by Jason
*/
public static void main(String[] args) {
try{
System.out.println(getMd5String16("Jason")); //829018f9dbd65fb8
} catch(NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
}
169 170 171 172
分类:
上⼀篇:下⼀篇:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。