簡單的java加密算法有:
- BASE64 嚴(yán)格地說,屬于編碼格式,而非加密算法
- MD5(Message Digest algorithm 5,信息摘要算法)
- SHA(Secure Hash Algorithm,安全散列算法)
- HMAC(Hash Message Authentication Code,散列消息鑒別碼)
1. BASE64
Base64是網(wǎng)絡(luò)上最常見的用于傳輸8Bit字節(jié)代碼的編碼方式之一,大家可以查看RFC2045~RFC2049,上面有MIME的詳細(xì)規(guī)范。Base64編碼可用于在HTTP環(huán)境下傳遞較長的標(biāo)識信息。例如,在Java Persistence系統(tǒng)hibernate中,就采用了Base64來將一個(gè)較長的唯一標(biāo)識符(一般為128-bit的UUID)編碼為一個(gè)字符串,用作HTTP表單和HTTP GET URL中的參數(shù)。在其他應(yīng)用程序中,也常常需要把二進(jìn)制數(shù)據(jù)編碼為適合放在URL(包括隱藏表單域)中的形式。此時(shí),采用Base64編碼具有不可讀性,即所編碼的數(shù)據(jù)不會被人用肉眼所直接看到。(來源百度百科)
java實(shí)現(xiàn)代碼:
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
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
|
package com.cn.單向加密; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; /* BASE64的加密解密是雙向的,可以求反解. BASE64Encoder和BASE64Decoder是非官方JDK實(shí)現(xiàn)類。雖然可以在JDK里能找到并使用,但是在API里查不到。 JRE 中 sun 和 com.sun 開頭包的類都是未被文檔化的,他們屬于 java, javax 類庫的基礎(chǔ),其中的實(shí)現(xiàn)大多數(shù)與底層平臺有關(guān), 一般來說是不推薦使用的。 BASE64 嚴(yán)格地說,屬于編碼格式,而非加密算法 主要就是BASE64Encoder、BASE64Decoder兩個(gè)類,我們只需要知道使用對應(yīng)的方法即可。 另,BASE加密后產(chǎn)生的字節(jié)位數(shù)是8的倍數(shù),如果不夠位數(shù)以=符號填充。 BASE64 按照RFC2045的定義,Base64被定義為:Base64內(nèi)容傳送編碼被設(shè)計(jì)用來把任意序列的8位字節(jié)描述為一種不易被人直接識別的形式。 (The Base64 Content-Transfer-Encoding is designed to represent arbitrary sequences of octets in a form that need not be humanly readable.) 常見于郵件、http加密,截取http信息,你就會發(fā)現(xiàn)登錄操作的用戶名、密碼字段通過BASE64加密的。 */ public class BASE64 { /** * BASE64解密 * * @param key * @return * @throws Exception */ public static byte[] decryptBASE64(String key) throws Exception { return (new BASE64Decoder()).decodeBuffer(key); } /** * BASE64加密 * * @param key * @return * @throws Exception */ public static String encryptBASE64( byte [] key) throws Exception { return ( new BASE64Encoder()).encodeBuffer(key); } public static void main(String[] args) { String str= "12345678" ; try { String result1= BASE64.encryptBASE64(str.getBytes()); System.out.println( "result1=====加密數(shù)據(jù)==========" +result1); byte result2[]= BASE64.decryptBASE64(result1); String str2= new String(result2); System.out.println( "str2========解密數(shù)據(jù)========" +str2); } catch (Exception e) { e.printStackTrace(); } } } |
2. MD5
MD5即Message-Digest Algorithm 5(信息-摘要算法5),用于確保信息傳輸完整一致。是計(jì)算機(jī)廣泛使用的雜湊算法之一(又譯摘要算法、哈希算法),主流編程語言普遍已有MD5實(shí)現(xiàn)。將數(shù)據(jù)(如漢字)運(yùn)算為另一固定長度值,是雜湊算法的基礎(chǔ)原理,MD5的前身有MD2、MD3和MD4。廣泛用于加密和解密技術(shù),常用于文件校驗(yàn)。校驗(yàn)?不管文件多大,經(jīng)過MD5后都能生成唯一的MD5值。好比現(xiàn)在的ISO校驗(yàn),都是MD5校驗(yàn)。怎么用?當(dāng)然是把ISO經(jīng)過MD5后產(chǎn)生MD5的值。一般下載Linux-ISO的朋友都見過下載鏈接旁邊放著MD5的串。就是用來驗(yàn)證文件是否一致的。
java實(shí)現(xiàn):
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
34
35
36
37
38
39
|
package com.cn.單向加密; import java.math.BigInteger; import java.security.MessageDigest; /* MD5(Message Digest algorithm 5,信息摘要算法) 通常我們不直接使用上述MD5加密。通常將MD5產(chǎn)生的字節(jié)數(shù)組交給BASE64再加密一把,得到相應(yīng)的字符串 Digest:匯編 */ public class MD5 { public static final String KEY_MD5 = "MD5" ; public static String getResult(String inputStr) { System.out.println( "=======加密前的數(shù)據(jù):" +inputStr); BigInteger bigInteger= null ; try { MessageDigest md = MessageDigest.getInstance(KEY_MD5); byte [] inputData = inputStr.getBytes(); md.update(inputData); bigInteger = new BigInteger(md.digest()); } catch (Exception e) {e.printStackTrace();} System.out.println( "MD5加密后:" + bigInteger.toString( 16 )); return bigInteger.toString( 16 ); } public static void main(String args[]) { try { String inputStr = "簡單加密8888888888888888888" ; getResult(inputStr); } catch (Exception e) { e.printStackTrace(); } } } |
MD5算法具有以下特點(diǎn):
1、壓縮性:任意長度的數(shù)據(jù),算出的MD5值長度都是固定的。
2、容易計(jì)算:從原數(shù)據(jù)計(jì)算出MD5值很容易。
3、抗修改性:對原數(shù)據(jù)進(jìn)行任何改動,哪怕只修改1個(gè)字節(jié),所得到的MD5值都有很大區(qū)別。
4、弱抗碰撞:已知原數(shù)據(jù)和其MD5值,想找到一個(gè)具有相同MD5值的數(shù)據(jù)(即偽造數(shù)據(jù))是非常困難的。
5、強(qiáng)抗碰撞:想找到兩個(gè)不同的數(shù)據(jù),使它們具有相同的MD5值,是非常困難的。
MD5的作用是讓大容量信息在用數(shù)字簽名軟件簽署私人密鑰前被”壓縮”成一種保密的格式(就是把一個(gè)任意長度的字節(jié)串變換成一定長的十六進(jìn)制數(shù)字串)。除了MD5以外,其中比較有名的還有sha-1、RIPEMD以及Haval等。
3.SHA
安全哈希算法(Secure Hash Algorithm)主要適用于數(shù)字簽名標(biāo)準(zhǔn)(Digital Signature Standard DSS)里面定義的數(shù)字簽名算法(Digital Signature Algorithm DSA)。對于長度小于2^64位的消息,SHA1會產(chǎn)生一個(gè)160位的消息摘要。該算法經(jīng)過加密專家多年來的發(fā)展和改進(jìn)已日益完善,并被廣泛使用。該算法的思想是接收一段明文,然后以一種不可逆的方式將它轉(zhuǎn)換成一段(通常更小)密文,也可以簡單的理解為取一串輸入碼(稱為預(yù)映射或信息),并把它們轉(zhuǎn)化為長度較短、位數(shù)固定的輸出序列即散列值(也稱為信息摘要或信息認(rèn)證代碼)的過程。散列函數(shù)值可以說是對明文的一種“指紋”或是“摘要”所以對散列值的數(shù)字簽名就可以視為對此明文的數(shù)字簽名。
java實(shí)現(xiàn):
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
34
35
36
37
38
|
package com.cn.單向加密; import java.math.BigInteger; import java.security.MessageDigest; /* SHA(Secure Hash Algorithm,安全散列算法),數(shù)字簽名等密碼學(xué)應(yīng)用中重要的工具, 被廣泛地應(yīng)用于電子商務(wù)等信息安全領(lǐng)域。雖然,SHA與MD5通過碰撞法都被破解了, 但是SHA仍然是公認(rèn)的安全加密算法,較之MD5更為安全*/ public class SHA { public static final String KEY_SHA = "SHA" ; public static String getResult(String inputStr) { BigInteger sha = null ; System.out.println( "=======加密前的數(shù)據(jù):" +inputStr); byte [] inputData = inputStr.getBytes(); try { MessageDigest messageDigest = MessageDigest.getInstance(KEY_SHA); messageDigest.update(inputData); sha = new BigInteger(messageDigest.digest()); System.out.println( "SHA加密后:" + sha.toString( 32 )); } catch (Exception e) {e.printStackTrace();} return sha.toString( 32 ); } public static void main(String args[]) { try { String inputStr = "簡單加密" ; getResult(inputStr); } catch (Exception e) { e.printStackTrace(); } } } |
SHA-1與MD5的比較
因?yàn)槎呔蒑D4導(dǎo)出,SHA-1和MD5彼此很相似。相應(yīng)的,他們的強(qiáng)度和其他特性也是相似,但還有以下幾點(diǎn)不同:
l 對強(qiáng)行攻擊的安全性:最顯著和最重要的區(qū)別是SHA-1摘要比MD5摘要長32 位。使用強(qiáng)行技術(shù),產(chǎn)生任何一個(gè)報(bào)文使其摘要等于給定報(bào)摘要的難度對MD5是2^128數(shù)量級的操作,而對SHA-1則是2^160數(shù)量級的操作。這樣,SHA-1對強(qiáng)行攻擊有更大的強(qiáng)度。
l 對密碼分析的安全性:由于MD5的設(shè)計(jì),易受密碼分析的攻擊,SHA-1顯得不易受這樣的攻擊。
l 速度:在相同的硬件上,SHA-1的運(yùn)行速度比MD5慢。
4.HMAC
HMAC(Hash Message Authentication Code,散列消息鑒別碼,基于密鑰的Hash算法的認(rèn)證協(xié)議。消息鑒別碼實(shí)現(xiàn)鑒別的原理是,用公開函數(shù)和密鑰產(chǎn)生一個(gè)固定長度的值作為認(rèn)證標(biāo)識,用這個(gè)標(biāo)識鑒別消息的完整性。使用一個(gè)密鑰生成一個(gè)固定大小的小數(shù)據(jù)塊,即MAC,并將其加入到消息中,然后傳輸。接收方利用與發(fā)送方共享的密鑰進(jìn)行鑒別認(rèn)證等。
java實(shí)現(xiàn)代碼:
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
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
65
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
102
103
104
|
package com.cn.單向加密; /* HMAC HMAC(Hash Message Authentication Code,散列消息鑒別碼,基于密鑰的Hash算法的認(rèn)證協(xié)議。 消息鑒別碼實(shí)現(xiàn)鑒別的原理是,用公開函數(shù)和密鑰產(chǎn)生一個(gè)固定長度的值作為認(rèn)證標(biāo)識,用這個(gè)標(biāo)識鑒別消息的完整性。 使用一個(gè)密鑰生成一個(gè)固定大小的小數(shù)據(jù)塊, 即MAC,并將其加入到消息中,然后傳輸。接收方利用與發(fā)送方共享的密鑰進(jìn)行鑒別認(rèn)證等。*/ import javax.crypto.KeyGenerator; import javax.crypto.Mac; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import com.cn.comm.Tools; /** * 基礎(chǔ)加密組件 */ public abstract class HMAC { public static final String KEY_MAC = "HmacMD5"; /** * 初始化HMAC密鑰 * * @return * @throws Exception */ public static String initMacKey() throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC); SecretKey secretKey = keyGenerator.generateKey(); return BASE64.encryptBASE64(secretKey.getEncoded()); } /** * HMAC加密 :主要方法 * * @param data * @param key * @return * @throws Exception */ public static String encryptHMAC(byte[] data, String key) throws Exception { SecretKey secretKey = new SecretKeySpec(BASE64.decryptBASE64(key), KEY_MAC); Mac mac = Mac.getInstance(secretKey.getAlgorithm()); mac.init(secretKey); return new String(mac.doFinal(data)); } public static String getResult1(String inputStr) { String path=Tools.getClassPath(); String fileSource=path+"/file/HMAC_key.txt"; System.out.println("=======加密前的數(shù)據(jù):"+inputStr); String result=null; try { byte[] inputData = inputStr.getBytes(); String key = HMAC.initMacKey(); /*產(chǎn)生密鑰*/ System.out.println("Mac密鑰:===" + key); /*將密鑰寫文件*/ Tools.WriteMyFile(fileSource,key); result= HMAC.encryptHMAC(inputData, key); System.out.println("HMAC加密后:===" + result); } catch (Exception e) {e.printStackTrace();} return result.toString(); } public static String getResult2(String inputStr) { System.out.println("=======加密前的數(shù)據(jù):"+inputStr); String path=Tools.getClassPath(); String fileSource=path+"/file/HMAC_key.txt"; String key=null;; try { /*將密鑰從文件中讀取*/ key=Tools.ReadMyFile(fileSource); System.out.println("getResult2密鑰:===" + key); } catch (Exception e1) { e1.printStackTrace();} String result=null; try { byte[] inputData = inputStr.getBytes(); /*對數(shù)據(jù)進(jìn)行加密*/ result= HMAC.encryptHMAC(inputData, key); System.out.println("HMAC加密后:===" + result); } catch (Exception e) {e.printStackTrace();} return result.toString(); } public static void main(String args[]) { try { String inputStr = "簡單加密"; /*使用同一密鑰:對數(shù)據(jù)進(jìn)行加密:查看兩次加密的結(jié)果是否一樣*/ getResult1(inputStr); getResult2(inputStr); } catch (Exception e) { e.printStackTrace(); } } } |
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://blog.csdn.net/qq_35101189/article/details/55044955