一段常用的加解密代码demo

来源:互联网 发布:mac的ppt怎么调16:9 编辑:程序博客网 时间:2024/05/21 14:56

贴一段常用的加解密代码 

用途: 

App和后端通信时,可对参数加密,防止请求伪造或被劫持后拿到一些敏感数据。 

App提交加密后的参数值,同时带上kid;服务端通过kid,拿到对应的秘钥,通过AES算法解密参数值。 

当然了,不同的kid可以对应不同的秘钥和加密算法。 

Java代码下载   收藏代码
  1. package xx.demo.decrypt;  
  2.   
  3. import org.apache.commons.codec.binary.Base64;  
  4.   
  5. import javax.crypto.Cipher;  
  6. import javax.crypto.SecretKey;  
  7. import javax.crypto.spec.SecretKeySpec;  
  8.   
  9. public class Main {  
  10.   
  11.     //这里测试发现,加密时必须使用PKCS5Padding填充模式, 解密时,则PKCS5Padding和NoPadding 都可以  
  12.     //至于原理,待研究  
  13.     static String[] conf = new String[] {"1.0.0""ab3d2e93fc3ff8bd2d48e3fa39750be7""AES""AES/ECB/PKCS5Padding""AES/ECB/PKCS5Padding"};  
  14.     static String _source ="{this is json}";  
  15.     static String _kid = "1.0.0";  
  16.   
  17.     public static void main(String[] args) throws Exception {  
  18.   
  19.         //加密过程: 与解密过程相反  
  20.         String encrypt = encrypt(_source, _kid);  
  21.         System.out.println(encrypt);  
  22.   
  23.         //解密过程: 加密字符串->Base64解码字节数组->Crypt解密取得字节数组->utf8编码取得字符串  
  24.         String src = decrypt(encrypt, _kid);  
  25.         System.out.println(src);  
  26.     }  
  27.   
  28.     /** 
  29.      * 加密 
  30.      * @param source 原文 
  31.      * @param kid 秘钥对应的key 
  32.      * @return 
  33.      * @throws Exception 
  34.      */  
  35.     public static String encrypt(String source,String kid) throws Exception {  
  36.   
  37.         byte[] bytes = source.getBytes("UTF-8");  
  38.   
  39.         SecretKey keytmp = new SecretKeySpec(hexString2Byte(conf[1]), conf[2]);  
  40.         Cipher cp_en = Cipher.getInstance(conf[3]);  //加密  
  41.         cp_en.init(Cipher.ENCRYPT_MODE, keytmp);  
  42.   
  43.         byte[] encrypt = cp_en.doFinal(bytes);  
  44.   
  45.         byte[] base64Encrypt = Base64.encodeBase64(encrypt);  
  46.         String edata = new String(base64Encrypt, "UTF-8");  
  47.   
  48.         return edata;  
  49.     }  
  50.   
  51.     /** 
  52.      * 解密 
  53.      * @param data 密文 
  54.      * @param kid 秘钥对应的key 
  55.      * @return 
  56.      * @throws Exception 
  57.      */  
  58.     public static String decrypt(String data, String kid) throws Exception {  
  59.   
  60.         byte[] original_bytes = Base64.decodeBase64(data);  
  61.   
  62.         SecretKey keytmp = new SecretKeySpec(hexString2Byte(conf[1]), conf[2]);  
  63.         Cipher cp_de = Cipher.getInstance(conf[4]);  //解密  
  64.         cp_de.init(Cipher.DECRYPT_MODE, keytmp);  
  65.   
  66.         byte[] zipped = cp_de.doFinal(original_bytes);  
  67.   
  68.         String content =  new String(zipped, "UTF-8");  
  69.         return content;  
  70.     }  
  71.   
  72.     /** 
  73.      * 16进制字符串转为byte[] 
  74.      * @param str (字符范围:0-9 A-F) 
  75.      * @return 
  76.      */  
  77.     public static byte[] hexString2Byte(String str) {  
  78.         byte[] result = new byte[str.length() / 2];  
  79.         for (int i = 0; i < result.length; i++) {  
  80.             byte b = (byte) ((((getStrIndex(str.charAt(2 * i))) & 0x0f) << 4) | ((getStrIndex(str.charAt(2 * i + 1))) & 0x0f));  
  81.             result[i] = b;  
  82.         }  
  83.         return result;  
  84.     }  
  85.   
  86.     public static int getStrIndex(char c) {  
  87.         if (c > '9') {  
  88.             return 10 + (c - 'a');  
  89.         } else {  
  90.             return c - '0';  
  91.         }  
  92.     }  
  93. }  


0 0
原创粉丝点击