本篇內容簡要介紹BASE6四、MD五、SHA、HMAC幾種加密算法。
BASE64編碼算法不算是真正的加密算法。
MD五、SHA、HMAC這三種加密算法,可謂是非可逆加密,就是不可解密的加密方法,咱們稱之爲單向加密算法。咱們一般只把他們做爲加密的基礎。單純的以上三種的加密並不可靠。
BASE64
按照RFC2045的定義,Base64被定義爲:Base64內容傳送編碼被設計用來把任意序列的8位字節描述爲一種不易被人直接識別的形式。(The Base64 Content-Transfer-Encoding is designed to represent arbitrary sequences of octets in a form that need not be humanly readable.)
常見於郵件、http加密,截取http信息,你就會發現登陸操做的用戶名、密碼字段經過BASE64加密的。 java
如基本的單向加密算法: linux
-
BASE64 嚴格地說,屬於編碼格式,而非加密算法git
-
MD5(Message Digest algorithm 5,信息摘要算法)算法
-
SHA(Secure Hash Algorithm,安全散列算法)數組
-
HMAC(Hash Message Authentication Code,散列消息鑑別碼)安全
複雜的對稱加密(DES、PBE)、非對稱加密算法: 函數
-
DES(Data Encryption Standard,數據加密算法)工具
-
PBE(Password-based encryption,基於密碼驗證)測試
-
RSA(算法的名字以發明者的名字命名:Ron Rivest, AdiShamir 和Leonard Adleman)編碼
-
DH(Diffie-Hellman算法,密鑰一致協議)
-
DSA(Digital Signature Algorithm,數字簽名)
-
ECC(Elliptic Curves Cryptography,橢圓曲線密碼編碼學)
本篇內容簡要介紹BASE6四、MD五、SHA、HMAC、DES、PBE、RSA幾種方法。
MD五、SHA、HMAC這三種加密算法,可謂是非可逆加密,就是不可解密的加密方法。咱們一般只把他們做爲加密的基礎。單純的以上三種的加密並不可靠。
經過java代碼實現以下:
- /**
- * 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);
- }
主要就是BASE64Encoder、BASE64Decoder兩個類,咱們只須要知道使用對應的方法便可。另,BASE加密後產生的字節位數是8的倍數,若是不夠位數以=符號填充。
MD5
MD5 -- message-digest algorithm 5 (信息-摘要算法)縮寫,普遍用於加密和解密技術,經常使用於文件校驗。校驗?無論文件多大,通過MD5後都能生成惟一的MD5值。比如如今的ISO校驗,都是MD5校驗。怎麼用?固然是把ISO通過MD5後產生MD5的值。通常下載linux-ISO的朋友都見過下載連接旁邊放着MD5的串。就是用來驗證文件是否一致的。
經過java代碼實現以下:
- /**
- * MD5加密
- *
- * @param data
- * @return
- * @throws Exception
- */
- public static byte[] encryptMD5(byte[] data) throws Exception {
- MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
- md5.update(data);
- return md5.digest();
- }
一般咱們不直接使用上述MD5加密。一般將MD5產生的字節數組交給BASE64再加密一把,獲得相應的字符串。
SHA
SHA(Secure Hash Algorithm,安全散列算法),數字簽名等密碼學應用中重要的工具,被普遍地應用於電子商務等信息安全領域。雖然,SHA與MD5經過碰撞法都被破解了, 可是SHA仍然是公認的安全加密算法,較之MD5更爲安全。
經過java代碼實現以下:
- /**
- * SHA加密
- *
- * @param data
- * @return
- * @throws Exception
- */
- public static byte[] encryptSHA(byte[] data) throws Exception {
- MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
- sha.update(data);
- return sha.digest();
- }
- }
HMAC
HMAC(Hash Message Authentication Code,散列消息鑑別碼,基於密鑰的Hash算法的認證協議。消息鑑別碼實現鑑別的原理是,用公開函數和密鑰產生一個固定長度的值做爲認證標識,用這個標識鑑別消息的完整性。使用一個密鑰生成一個固定大小的小數據塊,即MAC,並將其加入到消息中,而後傳輸。接收方利用與發送方共享的密鑰進行鑑別認證等。
經過java代碼實現以下:
- /**
- * 初始化HMAC密鑰
- *
- * @return
- * @throws Exception
- */
- public static String initMacKey() throws Exception {
- KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);
- SecretKey secretKey = keyGenerator.generateKey();
- return encryptBASE64(secretKey.getEncoded());
- }
- /**
- * HMAC加密
- *
- * @param data
- * @param key
- * @return
- * @throws Exception
- */
- public static byte[] encryptHMAC(byte[] data, String key) throws Exception {
- SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);
- Mac mac = Mac.getInstance(secretKey.getAlgorithm());
- mac.init(secretKey);
- return mac.doFinal(data);
- }
給出一個完整類,以下:
- import java.security.MessageDigest;
- import javax.crypto.KeyGenerator;
- import javax.crypto.Mac;
- import javax.crypto.SecretKey;
- import sun.misc.BASE64Decoder;
- import sun.misc.BASE64Encoder;
- /**
- * 基礎加密組件
- *
- * @author 樑棟
- * @version 1.0
- * @since 1.0
- */
- public abstract class Coder {
- public static final String KEY_SHA = "SHA";
- public static final String KEY_MD5 = "MD5";
- /**
- * MAC算法可選如下多種算法
- *
- * <pre>
- * HmacMD5
- * HmacSHA1
- * HmacSHA256
- * HmacSHA384
- * HmacSHA512
- * </pre>
- */
- public static final String KEY_MAC = "HmacMD5";
- /**
- * 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);
- }
- /**
- * MD5加密
- *
- * @param data
- * @return
- * @throws Exception
- */
- public static byte[] encryptMD5(byte[] data) throws Exception {
- MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
- md5.update(data);
- return md5.digest();
- }
- /**
- * SHA加密
- *
- * @param data
- * @return
- * @throws Exception
- */
- public static byte[] encryptSHA(byte[] data) throws Exception {
- MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
- sha.update(data);
- return sha.digest();
- }
- /**
- * 初始化HMAC密鑰
- *
- * @return
- * @throws Exception
- */
- public static String initMacKey() throws Exception {
- KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);
- SecretKey secretKey = keyGenerator.generateKey();
- return encryptBASE64(secretKey.getEncoded());
- }
- /**
- * HMAC加密
- *
- * @param data
- * @param key
- * @return
- * @throws Exception
- */
- public static byte[] encryptHMAC(byte[] data, String key) throws Exception {
- SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);
- Mac mac = Mac.getInstance(secretKey.getAlgorithm());
- mac.init(secretKey);
- return mac.doFinal(data);
- }
- }
再給出一個測試類:
- import static org.junit.Assert.*;
- import org.junit.Test;
- /**
- *
- * @author 樑棟
- * @version 1.0
- * @since 1.0
- */
- public class CoderTest {
- @Test
- public void test() throws Exception {
- String inputStr = "簡單加密";
- System.err.println("原文:/n" + inputStr);
- byte[] inputData = inputStr.getBytes();
- String code = Coder.encryptBASE64(inputData);
- System.err.println("BASE64加密後:/n" + code);
- byte[] output = Coder.decryptBASE64(code);
- String outputStr = new String(output);
- System.err.println("BASE64解密後:/n" + outputStr);
- // 驗證BASE64加密解密一致性
- assertEquals(inputStr, outputStr);
- // 驗證MD5對於同一內容加密是否一致
- assertArrayEquals(Coder.encryptMD5(inputData), Coder
- .encryptMD5(inputData));
- // 驗證SHA對於同一內容加密是否一致
- assertArrayEquals(Coder.encryptSHA(inputData), Coder
- .encryptSHA(inputData));
- String key = Coder.initMacKey();
- System.err.println("Mac密鑰:/n" + key);
- // 驗證HMAC對於同一內容,同一密鑰加密是否一致
- assertArrayEquals(Coder.encryptHMAC(inputData, key), Coder.encryptHMAC(
- inputData, key));
- BigInteger md5 = new BigInteger(Coder.encryptMD5(inputData));
- System.err.println("MD5:/n" + md5.toString(16));
- BigInteger sha = new BigInteger(Coder.encryptSHA(inputData));
- System.err.println("SHA:/n" + sha.toString(32));
- BigInteger mac = new BigInteger(Coder.encryptHMAC(inputData, inputStr));
- System.err.println("HMAC:/n" + mac.toString(16));
- }
- }
控制檯輸出:
- 原文:
- 簡單加密
- BASE64加密後:
- 566A5Y2V5Yqg5a+G
- BASE64解密後:
- 簡單加密
- Mac密鑰:
- uGxdHC+6ylRDaik++leFtGwiMbuYUJ6mqHWyhSgF4trVkVBBSQvY/a22xU8XT1RUemdCWW155Bke
- pBIpkd7QHg==
- MD5:
- -550b4d90349ad4629462113e7934de56
- SHA:
- 91k9vo7p400cjkgfhjh0ia9qthsjagfn
- HMAC:
- 2287d192387e95694bdbba2fa941009a
BASE64的加密解密是雙向的,能夠求反解。
MD五、SHA以及HMAC是單向加密,任何數據加密後只會產生惟一的一個加密串,一般用來校驗數據在傳輸過程當中是否被修改。其中HMAC算法有一個密鑰,加強了數據傳輸過程當中的安全性,強化了算法外的不可控因素。 單向加密的用途主要是爲了校驗數據在傳輸過程當中是否被修改。