Java实现 Base64、MD5、MAC、HMAC加密

开始对那些基本的加密还不怎么熟练,然后总结了些,写了一个测试:支持 Base64、MD5、MAC、HMAC加密,长话短说,我们都比较喜欢自己理解,看代码吧!

采用的输UTF-8的格式...

  1 package visec;
  2 import java.security.MessageDigest;
  3 import javax.crypto.KeyGenerator;
  4 import javax.crypto.Mac;
  5 import javax.crypto.SecretKey;
  6 import javax.crypto.spec.SecretKeySpec;
  7 
  8 import Decoder.BASE64Decoder;
  9 import Decoder.BASE64Encoder;
 10 /**
 11  * 基础加密组件
 12  * @author Visec·Dana
 13  * @version 1.0
 14  * @since 1.0
 15  */
 16 public abstract class Coder {
 17     public static final String KEY_SHA = "SHA";
 18     public static final String KEY_MD5 = "MD5";
 19 
 20     /**
 21      * MAC算法可选以下多种算法
 22      * <pre>
 23      * HmacMD5 
 24      * HmacSHA1 
 25      * HmacSHA256 
 26      * HmacSHA384 
 27      * HmacSHA512
 28      * </pre>
 29      */
 30     public static final String KEY_MAC = "HmacMD5";
 31 
 32     /**
 33      * BASE64解密
 34      * @param key
 35      * @return
 36      * @throws Exception
 37      */
 38     public static byte[] decryptBASE64(String key) throws Exception {
 39         return (new BASE64Decoder()).decodeBuffer(key);
 40     }
 41 
 42     /**
 43      * BASE64加密
 44      * @param key
 45      * @return
 46      * @throws Exception
 47      */
 48     public static String encryptBASE64(byte[] key) throws Exception {
 49         return (new BASE64Encoder()).encodeBuffer(key);
 50     }
 51     /**
 52      * MD5加密
 53      * @param data
 54      * @return
 55      * @throws Exception
 56      */
 57     public static byte[] encryptMD5(byte[] data) throws Exception {
 58 
 59         MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
 60         md5.update(data);
 61         return md5.digest();
 62 
 63     }
 64     /**
 65      * SHA加密
 66      * @param data
 67      * @return
 68      * @throws Exception
 69      */
 70     public static byte[] encryptSHA(byte[] data) throws Exception {
 71         MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
 72         sha.update(data);
 73         return sha.digest();
 74 
 75     }
 76     /**
 77      * 初始化HMAC密钥
 78      * @return
 79      * @throws Exception
 80      */
 81     public static String initMacKey() throws Exception {
 82         KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);
 83         SecretKey secretKey = keyGenerator.generateKey();
 84         return encryptBASE64(secretKey.getEncoded());
 85     }
 86     /**
 87      * MAC加密
 88      * @param data
 89      * @param key
 90      * @return
 91      * @throws Exception
 92      */
 93     public static byte[] encryptHMAC(byte[] data, String key) throws Exception {
 94         SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);
 95         Mac mac = Mac.getInstance(secretKey.getAlgorithm());
 96         mac.init(secretKey);
 97         return mac.doFinal(data);
 98 
 99     }
100 }

下面是一个测试类,及输入命令

 1 package visec;
 2 
 3 import java.math.BigInteger;
 4 
 5 /**
 6  * 加密测试
 7  * @author Visec·Dana
 8  * @version 1.0
 9  * @since 1.0
10  */
11 public class CoderTest {
12     public static void main(String[] args) throws Exception {
13         CoderTest.test(); 
14     }
15 
16     public static void test() throws Exception {
17         String inputStr = "这是一些简单的加密测试";
18         System.err.println("原文:" + inputStr);
19 
20         byte[] inputData = inputStr.getBytes();
21         String code = Coder.encryptBASE64(inputData);
22 
23         System.err.println("BASE64加密后:" + code);
24 
25         byte[] output = Coder.decryptBASE64(code);
26 
27         String outputStr = new String(output);
28 
29         System.err.println("BASE64解密后:" + outputStr);
30 
31         //单元测试 Junit4[暂时不用...]
32         /*// 验证BASE64加密解密一致性
33         assertEquals(inputStr, outputStr);
34 
35         // 验证MD5对于同一内容加密是否一致
36         assertArrayEquals(Coder.encryptMD5(inputData), Coder
37                 .encryptMD5(inputData));
38 
39         // 验证SHA对于同一内容加密是否一致
40         assertArrayEquals(Coder.encryptSHA(inputData), Coder
41                 .encryptSHA(inputData));
42 
43         String key = Coder.initMacKey();
44         System.err.println("Mac密钥:/n" + key);
45 
46         // 验证HMAC对于同一内容,同一密钥加密是否一致
47         assertArrayEquals(Coder.encryptHMAC(inputData, key), Coder.encryptHMAC(
48                 inputData, key));
49         */
50         BigInteger md5 = new BigInteger(Coder.encryptMD5(inputData));
51         System.err.println("MD5:" + md5.toString(16));
52 
53         BigInteger sha = new BigInteger(Coder.encryptSHA(inputData));
54         System.err.println("SHA:" + sha.toString(32));
55 
56         BigInteger mac = new BigInteger(Coder.encryptHMAC(inputData, inputStr));
57         System.err.println("HMAC:" + mac.toString(16));
58     }
59 }

下面呢是控制台输出:

原文:这是一些简单的加密测试
BASE64加密后:6L+Z5piv5LiA5Lqb566A5Y2V55qE5Yqg5a+G5rWL6K+V

BASE64解密后:这是一些简单的加密测试
MD5:76757e30d128e82b14488b115794d959
SHA:6f7afslor1oev1k7k40um57cscuqkjtn
HMAC:782313e944a28a55fc20507e50a9d470

posted @ 2014-06-10 16:52  小码哥、iByte  阅读(2382)  评论(0编辑  收藏  举报