MD5、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加密的。 

 

 


  1. /**
  2. * BASE64解密
  3. *
  4. * @param key
  5. * @return
  6. * @throws Exception
  7. */
  8. public static byte[] decryptBASE64(String key) throws Exception {
  9. return (new BASE64Decoder()).decodeBuffer(key);
  10. }

  11. /**
  12. * BASE64加密
  13. *
  14. * @param key
  15. * @return
  16. * @throws Exception
  17. */
  18. public static String encryptBASE64(byte[] key) throws Exception {
  19. return (new BASE64Encoder()).encodeBuffer(key);
  20. }


主要就是BASE64Encoder、BASE64Decoder两个类,我们只需要知道使用对应的方法即可。另,BASE加密后产生的字节位数是8的倍数,如果不够位数以=符号填充。



MD5

MD5 -- message-digest algorithm 5 (信息-摘要算法)缩写,广泛用于加密和解密技术,常用于文件校验。校验?不管文件多大,经过MD5后都能生成唯一的MD5值。好比现在的ISO校验,都是MD5校验。怎么用?当然是把ISO经过MD5后产生MD5的值。一般下载linux-ISO的朋友都见过下载链接旁边放着MD5的串。就是用来验证文件是否一致的。



通过java代码实现如下:


 

 


  1. /**
  2. * MD5加密
  3. *
  4. * @param data
  5. * @return
  6. * @throws Exception
  7. */
  8. public static byte[] encryptMD5(byte[] data) throws Exception {

  9. MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
  10. md5.update(data);

  11. return md5.digest();

  12. }


通常我们不直接使用上述MD5加密。通常将MD5产生的字节数组交给BASE64再加密一把,得到相应的字符串。



SHA

SHA(Secure Hash Algorithm,安全散列算法),数字签名等密码学应用中重要的工具,被广泛地应用于电子商务等信息安全领域。虽然,SHA与MD5通过碰撞法都被破解了, 但是SHA仍然是公认的安全加密算法,较之MD5更为安全。



通过java代码实现如下:



 


  1. /**
  2. * SHA加密
  3. *
  4. * @param data
  5. * @return
  6. * @throws Exception
  7. */
  8. public static byte[] encryptSHA(byte[] data) throws Exception {

  9. MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
  10. sha.update(data);

  11. return sha.digest();

  12. }
  13. }


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);
}




给出一个完整类,如下:

 ​


  1. import java.security.MessageDigest;

  2. import javax.crypto.KeyGenerator;
  3. import javax.crypto.Mac;
  4. import javax.crypto.SecretKey;

  5. import sun.misc.BASE64Decoder;
  6. import sun.misc.BASE64Encoder;

  7. /**
  8. * 基础加密组件
  9. *
  10. * @author 梁栋
  11. * @version 1.0
  12. * @since 1.0
  13. */
  14. public abstract class Coder {
  15. public static final String KEY_SHA = "SHA";
  16. public static final String KEY_MD5 = "MD5";

  17. /**
  18. * MAC算法可选以下多种算法
  19. *
  20. * <pre>
  21. * HmacMD5
  22. * HmacSHA1
  23. * HmacSHA256
  24. * HmacSHA384
  25. * HmacSHA512
  26. * </pre>
  27. */
  28. public static final String KEY_MAC = "HmacMD5";

  29. /**
  30. * BASE64解密
  31. *
  32. * @param key
  33. * @return
  34. * @throws Exception
  35. */
  36. public static byte[] decryptBASE64(String key) throws Exception {
  37. return (new BASE64Decoder()).decodeBuffer(key);
  38. }

  39. /**
  40. * BASE64加密
  41. *
  42. * @param key
  43. * @return
  44. * @throws Exception
  45. */
  46. public static String encryptBASE64(byte[] key) throws Exception {
  47. return (new BASE64Encoder()).encodeBuffer(key);
  48. }

  49. /**
  50. * MD5加密
  51. *
  52. * @param data
  53. * @return
  54. * @throws Exception
  55. */
  56. public static byte[] encryptMD5(byte[] data) throws Exception {

  57. MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
  58. md5.update(data);

  59. return md5.digest();

  60. }

  61. /**
  62. * SHA加密
  63. *
  64. * @param data
  65. * @return
  66. * @throws Exception
  67. */
  68. public static byte[] encryptSHA(byte[] data) throws Exception {

  69. MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
  70. sha.update(data);

  71. return sha.digest();

  72. }

  73. /**
  74. * 初始化HMAC密钥
  75. *
  76. * @return
  77. * @throws Exception
  78. */
  79. public static String initMacKey() throws Exception {
  80. KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);

  81. SecretKey secretKey = keyGenerator.generateKey();
  82. return encryptBASE64(secretKey.getEncoded());
  83. }

  84. /**
  85. * HMAC加密
  86. *
  87. * @param data
  88. * @param key
  89. * @return
  90. * @throws Exception
  91. */
  92. public static byte[] encryptHMAC(byte[] data, String key) throws Exception {

  93. SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);
  94. Mac mac = Mac.getInstance(secretKey.getAlgorithm());
  95. mac.init(secretKey);

  96. return mac.doFinal(data);

  97. }
  98. }




再给出一个测试类:


 


  1. import static org.junit.Assert.*;

  2. import org.junit.Test;

  3. /**
  4. *
  5. * @author 梁栋
  6. * @version 1.0
  7. * @since 1.0
  8. */
  9. public class CoderTest {

  10. @Test
  11. public void test() throws Exception {
  12. String inputStr = "简单加密";
  13. System.err.println("原文:/n" + inputStr);

  14. byte[] inputData = inputStr.getBytes();
  15. String code = Coder.encryptBASE64(inputData);

  16. System.err.println("BASE64加密后:/n" + code);

  17. byte[] output = Coder.decryptBASE64(code);

  18. String outputStr = new String(output);

  19. System.err.println("BASE64解密后:/n" + outputStr);

  20. // 验证BASE64加密解密一致性
  21. assertEquals(inputStr, outputStr);

  22. // 验证MD5对于同一内容加密是否一致
  23. assertArrayEquals(Coder.encryptMD5(inputData), Coder
  24. .encryptMD5(inputData));

  25. // 验证SHA对于同一内容加密是否一致
  26. assertArrayEquals(Coder.encryptSHA(inputData), Coder
  27. .encryptSHA(inputData));

  28. String key = Coder.initMacKey();
  29. System.err.println("Mac密钥:/n" + key);

  30. // 验证HMAC对于同一内容,同一密钥加密是否一致
  31. assertArrayEquals(Coder.encryptHMAC(inputData, key), Coder.encryptHMAC(
  32. inputData, key));

  33. BigInteger md5 = new BigInteger(Coder.encryptMD5(inputData));
  34. System.err.println("MD5:/n" + md5.toString(16));

  35. BigInteger sha = new BigInteger(Coder.encryptSHA(inputData));
  36. System.err.println("SHA:/n" + sha.toString(32));

  37. BigInteger mac = new BigInteger(Coder.encryptHMAC(inputData, inputStr));
  38. System.err.println("HMAC:/n" + mac.toString(16));
  39. }
  40. }



控制台输出: