java加密算法

 
  1.   
  1. Base64可以解密、MD5、SHA、不行、也算不上是加密算法!  
  1. package com.boxun.crm.test;  
  2.   
  3. import java.security.MessageDigest;  
  4.   
  5.   
  6. import javax.crypto.KeyGenerator;  
  7. import javax.crypto.Mac;  
  8. import javax.crypto.SecretKey;  
  9. import javax.crypto.spec.SecretKeySpec;  
  10.   
  11. import sun.misc.BASE64Decoder;  
  12. import sun.misc.BASE64Encoder;  
  13.   
  14. public abstract class Base64Test {  
  15.   
  16.     private static final String KEY_MD5 = "MD5";  
  17.     private static final String KEY_SHA = "SHA";  
  18.     /**   
  19.      * MAC算法可選以下多種算法   
  20.      *    
  21.      * <pre>   
  22.      * HmacMD5    
  23.      * HmacSHA1    
  24.      * HmacSHA256    
  25.      * HmacSHA384    
  26.      * HmacSHA512   
  27.      * </pre>   
  28.      */    
  29.     public static final String KEY_MAC = "HmacMD5";     
  30.   
  31.     /**   
  32.      * BASE64解密   
  33.      *    
  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.      *    
  45.      * @param key   
  46.      * @return   
  47.      * @throws Exception   
  48.      */    
  49.     public static String encryptBASE64(byte[] key) throws Exception {     
  50.         return (new BASE64Encoder()).encodeBuffer(key);     
  51.     }  
  52.   
  53.   
  54.     /**   
  55.      * MD5加密   
  56.      *    
  57.      * @param data   
  58.      * @return   
  59.      * @throws Exception   
  60.      */    
  61.     public static byte[] encryptMD5(byte[] data) throws Exception {    
  62.         /**-- getInstance(String algorithm)返回實現指定摘要算法的 MessageDigest 對象  --*/  
  63.         MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);     
  64.         md5.update(data);     
  65.         return md5.digest();     
  66.     }   
  67.   
  68.   
  69.     /**   
  70.      * SHA加密   
  71.      *    
  72.      * @param data   
  73.      * @return   
  74.      * @throws Exception   
  75.      */    
  76.     public static byte[] encryptSHA(byte[] data) throws Exception {     
  77.         MessageDigest sha = MessageDigest.getInstance(KEY_SHA);     
  78.         sha.update(data);     
  79.         return sha.digest();     
  80.     }   
  81.   
  82.     /**   
  83.      * 初始化HMAC密鑰   
  84.      *    
  85.      * @return   
  86.      * @throws Exception   
  87.      */    
  88.     public static String initMacKey() throws Exception {   
  89.         /**-- getInstance(String algorithm)返回生成指定算法的祕密密鑰的 KeyGenerator 對象  --*/  
  90.         KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);  
  91.         /**-- generateKey()生成一個密鑰  --*/  
  92.         SecretKey secretKey = keyGenerator.generateKey();   
  93.         /**-- secretKey.getEncoded()返回基本編碼格式的密鑰,如果此密鑰不支持編碼,則返回 null -- 
  94.          * encryptBASE64調用Base64加密方法 
  95.          */  
  96.         return encryptBASE64(secretKey.getEncoded());     
  97.     }     
  98.   
  99.     /**   
  100.      * HMAC加密   
  101.      *    
  102.      * @param data   
  103.      * @param key   
  104.      * @return   
  105.      * @throws Exception   
  106.      */    
  107.     public static byte[] encryptHMAC(byte[] data, String key) throws Exception {     
  108.         SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);     
  109.         Mac mac = Mac.getInstance(secretKey.getAlgorithm());     
  110.         mac.init(secretKey);     
  111.         return mac.doFinal(data);     
  112.     }  
  113.   
  114. }  
package com.boxun.crm.test;

import java.security.MessageDigest;


import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public abstract class Base64Test {

	private static final String KEY_MD5 = "MD5";
	private static final String KEY_SHA = "SHA";
	/**  
	 * 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 {  
		/**-- getInstance(String algorithm)返回實現指定摘要算法的 MessageDigest 對象  --*/
		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 { 
		/**-- getInstance(String algorithm)返回生成指定算法的祕密密鑰的 KeyGenerator 對象  --*/
		KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);
		/**-- generateKey()生成一個密鑰  --*/
		SecretKey secretKey = keyGenerator.generateKey(); 
		/**-- secretKey.getEncoded()返回基本編碼格式的密鑰,如果此密鑰不支持編碼,則返回 null --
		 * encryptBASE64調用Base64加密方法
		 */
		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. 下面是main方法類:  
  1. <PRE class=java name="code">package com.boxun.crm.test;  
  2.   
  3. import java.math.BigInteger;  
  4.   
  5.   
  6. /** 
  7.  *  
  8.  * @author 旦旦而學 
  9.  * @version 1.0 
  10.  * @since 1.0 
  11.  */  
  12. public class CoderTest {  
  13.       
  14.     public void test() throws Exception {  
  15.         String inputStr = "單項加密";  
  16.         System.err.println("原值:\t" + inputStr);  
  17.   
  18.         byte[] inputData = inputStr.getBytes();  
  19.         String code = Base64Test.encryptBASE64(inputData);  
  20.   
  21.         System.err.println("BASE64加密後:\t" + code);  
  22.   
  23.         byte[] output = Base64Test.decryptBASE64(code);  
  24.   
  25.         String outputStr = new String(output);  
  26.   
  27.         System.err.println("BASE64解密後:\t" + outputStr);  
  28.   
  29.         // 驗證BASE64加密解密一致性   
  30.         assertEquals(inputStr, outputStr);  
  31.   
  32.         // 驗證MD5對於同一內容加密是否一致   
  33.         assertArrayEquals(Base64Test.encryptMD5(inputData), Base64Test  
  34.                 .encryptMD5(inputData),"MD5");  
  35.   
  36.         // 驗證SHA對於同一內容加密是否一致   
  37.         assertArrayEquals(Base64Test.encryptSHA(inputData), Base64Test  
  38.                 .encryptSHA(inputData),"SHA");  
  39.   
  40.         String key = Base64Test.initMacKey();  
  41.         System.err.println("Mac密鑰:\t" + key);  
  42.   
  43.         // 驗證HMAC對於同一內容,同一密鑰加密是否一致   
  44.         assertArrayEquals(Base64Test.encryptHMAC(inputData, key), Base64Test.encryptHMAC(  
  45.                 inputData, key),"HMAC");  
  46.   
  47.         BigInteger md5 = new BigInteger(Base64Test.encryptMD5(inputData));  
  48.         System.err.println("MD5:\t" + md5.toString(16));  
  49.   
  50.         BigInteger sha = new BigInteger(Base64Test.encryptSHA(inputData));  
  51.         System.err.println("SHA:\t" + sha.toString(32));  
  52.   
  53.         BigInteger mac = new BigInteger(Base64Test.encryptHMAC(inputData, inputStr));  
  54.         System.err.println("HMAC:\t" + mac.toString(16));  
  55.     }  
  56.   
  57.     private void assertArrayEquals(byte[] encryptMD5, byte[] encryptMD52,String type) {  
  58.         String out = "不一致" ;  
  59.         byte[] by = encryptMD5 ;  
  60.         byte[] by2 = encryptMD52 ;  
  61.         String byt = "" ;  
  62.         String byt2 = "" ;  
  63.         //轉換成16進制數據   
  64.         for (int i = 0; i < by.length; i++) {  
  65.             byt += Integer.toHexString((by[i] & 0xFF) | 0x100).substring(1,3);  
  66.         }  
  67.         for (int i = 0; i < by2.length; i++) {  
  68.             byt2 += Integer.toHexString((by2[i] & 0xFF) | 0x100).substring(1,3);  
  69.         }  
  70.         if(byt.equals(byt2)){  
  71.             out = "一致" ;  
  72.         }  
  73.         System.out.println("驗證"+type+"對於同一內容,同一密鑰加密是否一致:"+out);  
  74.         System.out.println(byt + "  "+type+"  " + byt2);  
  75.     }  
  76.   
  77.     private void assertEquals(String inputStr, String outputStr) {  
  78.         String out = "不一致" ;  
  79.         if(inputStr.equals(outputStr)){  
  80.             out = "一致" ;  
  81.         }  
  82.         System.out.println("驗證BASE64加密解密一致性:"+out);  
  83.     }  
  84.     /** 
  85.      * 2011-7-5上午10:43:51 
  86.      * @param args 
  87.      * @throws Exception  
  88.      */  
  89.     public static void main(String[] args) throws Exception {  
  90.         CoderTest te = new CoderTest();  
  91.         te.test();  
  92.     }  
  93.   
  94.   
  95.   
  96. }
發佈了45 篇原創文章 · 獲贊 10 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章