java加密解密工具类

来源:互联网 发布:执行云计算的服务器 编辑:程序博客网 时间:2024/04/30 09:58


[java] view plain copy
  1. package com.xy6.moneykeeper.syn;  
  2.   
  3. import java.security.MessageDigest;  
  4.   
  5. import javax.crypto.KeyGenerator;  
  6. import javax.crypto.Mac;  
  7. import javax.crypto.SecretKey;  
  8. import javax.crypto.spec.SecretKeySpec;  
  9.   
  10. import sun.misc.BASE64Decoder;  
  11. import sun.misc.BASE64Encoder;  
  12.   
  13. /** 
  14.  * data encrypt utls 
  15.  * 
  16.  * @author skyline 
  17.  * 
  18.  */  
  19. public class DataEncryptUtil {  
  20.     public static final String KEY_SHA = "SHA";  
  21.   
  22.     public static final String KEY_MD5 = "MD5";  
  23.   
  24.     /** 
  25.      * MAC算法可选以下多种算法 
  26.      * 
  27.      * <pre> 
  28.      * HmacMD5 
  29.      * HmacSHA1 
  30.      * HmacSHA256 
  31.      * HmacSHA384 
  32.      * HmacSHA512 
  33.      * </pre> 
  34.      */  
  35.     public static final String KEY_MAC = "HmacMD5";  
  36.   
  37.     /** 
  38.      * BASE64解密 
  39.      * 
  40.      * @param key = 需要解密的密码字符串 
  41.      * @return 
  42.      * @throws Exception 
  43.      */  
  44.     public static byte[] decryptBASE64(String key) throws Exception {  
  45.         return (new BASE64Decoder()).decodeBuffer(key);  
  46.     }  
  47.   
  48.     /** 
  49.      * BASE64加密 
  50.      * 
  51.      * @param key = 需要加密的字符数组 
  52.      * @return 
  53.      * @throws Exception 
  54.      */  
  55.     public static String encryptBASE64(byte[] key) throws Exception {  
  56.         return (new BASE64Encoder()).encodeBuffer(key);  
  57.     }  
  58.   
  59.     /** 
  60.      * MD5加密 
  61.      * 
  62.      * @param data = 需要加密的字符数组 
  63.      * @return 
  64.      * @throws Exception 
  65.      */  
  66.     public static byte[] encryptMD5(byte[] data) throws Exception {  
  67.   
  68.         MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);  
  69.         md5.update(data);  
  70.   
  71.         return md5.digest();  
  72.   
  73.     }  
  74.   
  75.     /** 
  76.      * SHA加密 
  77.      * 
  78.      * @param data = 需要加密的字符数组 
  79.      * @return 
  80.      * @throws Exception 
  81.      */  
  82.     public static byte[] encryptSHA(byte[] data) throws Exception {  
  83.   
  84.         MessageDigest sha = MessageDigest.getInstance(KEY_SHA);  
  85.         sha.update(data);  
  86.   
  87.         return sha.digest();  
  88.   
  89.     }  
  90.   
  91.     /** 
  92.      * 初始化HMAC密钥 
  93.      * 
  94.      * @return 
  95.      * @throws Exception 
  96.      */  
  97.     public static String initMacKey() throws Exception {  
  98.         KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);  
  99.   
  100.         SecretKey secretKey = keyGenerator.generateKey();  
  101.         return encryptBASE64(secretKey.getEncoded());  
  102.     }  
  103.   
  104.     /** 
  105.      * HMAC加密 
  106.      * 
  107.      * @param data = 密匙加密过的字符数组 
  108.      * @param key = 需要加密的字符串 
  109.      * @return 
  110.      * @throws Exception 
  111.      */  
  112.     public static byte[] encryptHMAC(byte[] data, String key) throws Exception {  
  113.   
  114.         SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);  
  115.         Mac mac = Mac.getInstance(secretKey.getAlgorithm());  
  116.         mac.init(secretKey);  
  117.   
  118.         return mac.doFinal(data);  
  119.   
  120.     }  
  121.   
  122. }  


用法:

加密:

str2 = DataEncryptUtil.encryptBASE64(str1.getBytes());

解密:
str1 = new String(DataEncryptUtil.decryptBASE64(str2));

0 0
原创粉丝点击