·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[java]java加密解密算法(基于org.apache.commons)源代码

时间:2015-01-14 17:17jueyue
[java]java加密解密算法使用的org.apache.commons

[java]java加密解密算法(基于org.apache.commons)源代码

  1. package com.jueyue.crypto; 
  2.  
  3. import javax.crypto.Cipher;   
  4. import javax.crypto.spec.IvParameterSpec;   
  5. import javax.crypto.spec.SecretKeySpec;   
  6.  
  7. import org.apache.commons.codec.binary.BAES64; 
  8.    
  9.    
  10. /*******************************************************************************  
  11.  * AES加解密算法  
  12.  *   
  13.  * @author jueyue  
  14.  *   
  15.   
  16.   加密用的Key 可以用26个字母和数字组成,最好不要用保留字符,虽然不会错,至于怎么裁决,个人看情况而定  
  17.   此处使用AES-128-CBC加密模式,key需要为16位。  
  18.    也是使用<span style="font-size: 1em; line-height: 1.5;">0102030405060708</span> 
  19.  */   
  20.    
  21. public class AES {   
  22.    
  23.     // 加密   
  24.     public static String Encrypt(String sSrc, String sKey) throws Exception {   
  25.         if (sKey == null) {   
  26.             System.out.print("Key为空null");   
  27.             return null;   
  28.         }   
  29.         // 判断Key是否为16位   
  30.         if (sKey.length() != 16) {   
  31.             System.out.print("Key长度不是16位");   
  32.             return null;   
  33.         }   
  34.         byte[] raw = sKey.getBytes();   
  35.         SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");   
  36.         Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//"算法/模式/补码方式"   
  37.         IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());//使用CBC模式,需要一个向量iv,可增加加密算法的强度   
  38.         cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);   
  39.         byte[] encrypted = cipher.doFinal(sSrc.getBytes());   
  40.    
  41.         return BAES64.encodeBAES64String(encrypted);//此处使用BAES64做转码功能,同时能起到2次加密的作用。   
  42.     }   
  43.    
  44.     // 解密   
  45.     public static String Decrypt(String sSrc, String sKey) throws Exception {   
  46.         try {   
  47.             // 判断Key是否正确   
  48.             if (sKey == null) {   
  49.                 System.out.print("Key为空null");   
  50.                 return null;   
  51.             }   
  52.             // 判断Key是否为16位   
  53.             if (sKey.length() != 16) {   
  54.                 System.out.print("Key长度不是16位");   
  55.                 return null;   
  56.             }   
  57.             byte[] raw = sKey.getBytes("ASCII");   
  58.             SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");   
  59.             Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");   
  60.             IvParameterSpec iv = new IvParameterSpec("0102030405060708"   
  61.                     .getBytes());   
  62.             cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);   
  63.             byte[] encrypted1 = BAES64.decodeBAES64(sSrc);//先用bAES64解密   
  64.             try {   
  65.                 byte[] original = cipher.doFinal(encrypted1);   
  66.                 String originalString = new String(original);   
  67.                 return originalString;   
  68.             } catch (Exception e) {   
  69.                 System.out.println(e.toString());   
  70.                 return null;   
  71.             }   
  72.         } catch (Exception ex) {   
  73.             System.out.println(ex.toString());   
  74.             return null;   
  75.         }   
  76.     }   
  77.    
  78.  
  79. }  

 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器