AES對稱加密

public class AES {
   
   private static final String KEY = "99ssavv";
         
       /** 
        * base 64 encode 
        * @param bytes 待編碼的byte[] 
        * @return 編碼後的base 64 code 
        */  
       public static String base64Encode(byte[] bytes){  
           return new BASE64Encoder().encode(bytes);  
       }  
         
       /** 
        * base 64 decode 
        * @param base64Code 待解碼的base 64 code 
        * @return 解碼後的byte[] 
        * @throws Exception 
        */  
       public static byte[] base64Decode(String base64Code) throws Exception{  
           return StringUtils.isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code);  
       }  

       /** 
        * AES加密 
        * @param content 待加密的內容 
        * @return 加密後的byte[]
        * @throws Exception 
        */  
       public static byte[] aesEncryptToBytes(String content) throws Exception {  
          SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
           random.setSeed(KEY.getBytes());
           KeyGenerator kgen = KeyGenerator.getInstance("AES");  
           kgen.init(128, random);   
     
           Cipher cipher = Cipher.getInstance("AES");  
           cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));  
             
           return cipher.doFinal(content.getBytes("utf-8"));  
       }  
         
       /** 
        * AES加密爲base 64 code 
        * @param content 待加密的內容 
        * @return 加密後的base 64 code 
        * @throws Exception 
        */  
       public static String aesEncrypt(String content) throws Exception {  
           return base64Encode(aesEncryptToBytes(content));  
       }  
         
       /** 
        * AES解密 
        * @param encryptBytes 待解密的byte[] 
        * @return 解密後的String 
        * @throws Exception 
        */  
       public static String aesDecryptByBytes(byte[] encryptBytes) throws Exception {  
           
           SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
           random.setSeed(KEY.getBytes());
           KeyGenerator kgen = KeyGenerator.getInstance("AES");  
           kgen.init(128, random);  
           
           Cipher cipher = Cipher.getInstance("AES");  
           cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));  
           byte[] decryptBytes = cipher.doFinal(encryptBytes);  
             
           return new String(decryptBytes);  
       }  
         
       /** 
        * base 64 code AES解密 
        * @param encryptStr 待解密的base 64 code 
        * @return 解密後的string 
        * @throws Exception 
        */  
       public static String aesDecrypt(String encryptStr) throws Exception {  
           return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr));  
       }  
         
       
	public static void main(String[] args) throws Exception {
   		String content = "oQ906uPAbia-uuIH3u4vBS9t_05U";
   		System.out.println("加密前:"+content);
   		String encryptStr =aesEncrypt(content);
       		System.out.println("加密後:"+encryptStr);
       		System.out.println("解密後:"+aesDecrypt(encryptStr));
	}
}

執行main方法:

加密前:oQ906uPAbia-uuIH3u4vBS9t_05U
加密後:L2iC6aItPTAMvfGTeY9EF3Va8EUWoLlqGUrgBvZWLaE=
解密後:oQ906uPAbia-uuIH3u4vBS9t_05U



發佈了45 篇原創文章 · 獲贊 65 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章