5.sha1加密//sha1加密
public static string getsha1(string str){
if(str==null||str.length()==0){
return null;
}
char hexdigits[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
try {
messagedigest mdtemp = messagedigest.getinstance("sha1");
mdtemp.update(str.getbytes("utf-8"));
byte[] md = mdtemp.digest();
int j = md.length;
char buf[] = new char[j*2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
buf[k++] = hexdigits[byte0 >>> 4 & 0xf];
buf[k++] = hexdigits[byte0 & 0xf];
} return new string(buf);
} catch (exception e) {
return null;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
6.md5加密工具类:package com.huihui.util;
import java.security.messagedigest;
/**
* md5加密工具类
* @author administrator
*
*/public class md5util {
public final static string md5(string s){
char hexdigits[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
try { byte[] strtemp = s.getbytes();
messagedigest mdtemp = messagedigest.getinstance("md5");
mdtemp.update(strtemp);
byte[] md = mdtemp.digest();
int j = md.length;
char str[] = new char[j*2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexdigits[byte0>>>4&0xf];
str[k++] = hexdigits[byte0 & 0xf];
} return new string(str);
} catch (exception e) {
return null;
}
} public static void main(string[] args) {
system.out.println(md5util.md5("b"));
}
}
以上就是c# 加密类工具实例分析的详细内容。