俊星的BLOG
導航
BlogJava
首頁
新隨筆
聯系
聚合
管理
<
2009年6月
>
日
一
二
三
四
五
六
31
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
1
2
3
4
5
6
7
8
9
10
11
統計
隨筆 - 50
文章 - 0
評論 - 6
引用 - 0
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(1)
給我留言
查看公開留言
查看私人留言
隨筆分類
代碼庫(7)
(rss)
隨筆檔案
2010年11月 (3)
2010年10月 (12)
2009年6月 (5)
2009年5月 (22)
2009年4月 (8)
最新隨筆
1.?實現變更統計
2.?切換IE8中碰到的問題
3.?發布FWMS
4.?ActiveMQ初探
5.?CAS初步配置
搜索
最新評論
1.?re: JAVA圖像縮放處理[未登錄]
不錯的方法
--test
2.?re: freetts初探
學習了! 就是不清楚為啥在eclipse下跑不起來...
--apiao
3.?re: freetts初探
System property "mbrola.base" is undefined. Will not use MBROLA voices.
這個問題怎么解決啊?
--瓏兒
4.?re: ActiveMQ初探
測試通過。多謝
--71569973
5.?登錄網站
評論內容較長,點擊標題查看
--ahome
閱讀排行榜
1.?ftp的port和pasv模式(轉)(8036)
2.?JAVA圖像縮放處理(4186)
3.?Nutz初步試用(4110)
4.?Window下FTP命令使用(3523)
5.?freetts初探(3434)
6.?ActiveMQ初探(2910)
7.?MYSQL Access denied 問題的解決(2347)
8.?HTTP Client及HTTPS初探(1993)
9.?JAVA單向加密(MD5,SHA,MAC)(1269)
10.?CAS認證初探(1158)
評論排行榜
1.?freetts初探(2)
2.?HTTP Client及HTTPS初探(2)
3.?ActiveMQ初探(1)
4.?JAVA圖像縮放處理(1)
5.?我的DWR之DefaultContainer(0)
JAVA單向加密(MD5,SHA,MAC)
具體代碼如下:
import
java.security.MessageDigest;
import
javax.crypto.KeyGenerator;
import
javax.crypto.Mac;
import
javax.crypto.SecretKey;
import
javax.crypto.spec.SecretKeySpec;
import
sun.misc.BASE64Decoder;
import
sun.misc.BASE64Encoder;
/** */
/**
* 加密(主要有:MD4,SHA,MAC)
*
*
@author
kinkding
* @history 2009-6-3
*/
public
class
MyEncrypt
{
/** */
/**
MD5 加密
*/
public
static
byte
[] encryptMD5(
byte
[] data)
throws
Exception
{
MessageDigest md5
=
MessageDigest.getInstance(
"
MD5
"
);
md5.update(data);
return
md5.digest();
}
/** */
/**
SHA 加密
*/
public
static
byte
[] encryptSHA(
byte
[] data)
throws
Exception
{
MessageDigest sha
=
MessageDigest.getInstance(
"
SHA
"
);
sha.update(data);
return
sha.digest();
}
/** */
/**
取得HMAC密鑰
*/
public
static
String getMacKey()
throws
Exception
{
KeyGenerator keyGenerator
=
KeyGenerator.getInstance(
"
HmacMD5
"
);
SecretKey secretKey
=
keyGenerator.generateKey();
return
new
BASE64Encoder().encode(secretKey.getEncoded());
}
/** */
/**
執行加密
*/
public
static
byte
[] encryptHMAC(
byte
[] data, String key)
throws
Exception
{
byte
[] bkey
=
new
BASE64Decoder().decodeBuffer(key);
SecretKey secretKey
=
new
SecretKeySpec(bkey,
"
HmacMD5
"
);
Mac mac
=
Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
return
mac.doFinal(data);
}
private
static
String toHex(
byte
[] buffer)
{
StringBuffer sb
=
new
StringBuffer(buffer.length
*
3
);
for
(
int
i
=
0
; i
<
buffer.length; i
++
)
{
sb.append(Character.forDigit((buffer[i]
&
0xf0
)
>>
4
,
16
));
sb.append(Character.forDigit(buffer[i]
&
0x0f
,
16
));
}
return
sb.toString();
}
public
static
void
main(String[] args)
{
String msg
=
"
生活真好^_^
"
;
byte
[] data
=
msg.getBytes();
try
{
System.out.println(
"
msg:
"
+
msg);
System.out.println(
"
md5:
"
+
toHex(encryptMD5(data)));
System.out.println(
"
sha:
"
+
toHex(encryptSHA(data)));
String key
=
getMacKey();
System.out.println(
"
mac key:
"
+
key);
System.out.println(
"
mac:
"
+
toHex(encryptHMAC(data, key)));
}
catch
(Exception e)
{
e.printStackTrace();
}
}
}
運行效果如下:
msg:生活真好^_^
md5:e0649dfaef57789734e920c7ecb9c4ea
sha:a4bd855836de26b2323778b797629fed4416f12f
mac key:zBPe28oho2H84+Mg8mF4abpd0MQvdjgqgFdX4hmUQQbOGnX1aFq/oQnogsHVIczgx1AZ1s2/ncPz
tBQIGLZUnw
==
mac:87f4140161ad43797059e85dd9962897
posted on 2009-06-03 23:20
俊星
閱讀(1269)
評論(0)
編輯
收藏
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
Powered by:
BlogJava
Copyright © 俊星
主站蜘蛛池模板:
逊克县
|
温州市
|
武宣县
|
扎赉特旗
|
西畴县
|
康定县
|
天气
|
兴宁市
|
渝北区
|
巨鹿县
|
赣州市
|
桂林市
|
汉阴县
|
喀什市
|
大兴区
|
张家口市
|
新巴尔虎左旗
|
阜新市
|
乌兰察布市
|
新余市
|
阿拉善盟
|
明星
|
长治县
|
石渠县
|
荔波县
|
忻州市
|
高陵县
|
惠东县
|
大安市
|
竹溪县
|
介休市
|
孟津县
|
咸阳市
|
鄂尔多斯市
|
井陉县
|
常州市
|
泰安市
|
泗水县
|
马鞍山市
|
兴文县
|
雅江县
|