码迷,mamicode.com
首页 > 其他好文 > 详细

AES加密解密

时间:2017-09-20 12:16:23      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:generate   vax   and   code   amp   log   初始化   img   substr   

  1 import java.security.SecureRandom;
  2 
  3 import javax.crypto.Cipher;
  4 import javax.crypto.KeyGenerator;
  5 import javax.crypto.SecretKey;
  6 import javax.crypto.spec.SecretKeySpec;
  7 
  8 /**
  9  * AES加密
 10  *
 11  */
 12 public class AESUtils {
 13 
 14   /**
 15    * 解密
 16    *
 17    * @param content 待解密内容
 18    * @param password 解密密钥
 19    * @return
 20    */
 21   public static byte[] decrypt(byte[] content, String password) {
 22     try {
 23       KeyGenerator kgen = KeyGenerator.getInstance("AES");
 24       kgen.init(128, new SecureRandom(password.getBytes()));
 25       SecretKey secretKey = kgen.generateKey();
 26       byte[] enCodeFormat = secretKey.getEncoded();
 27       SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
 28       Cipher cipher = Cipher.getInstance("AES");// 创建密码器
 29       cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
 30       byte[] result = cipher.doFinal(content);
 31       return result; // 加密
 32     }
 33     catch (Exception e) {
 34       e.printStackTrace();
 35     }
 36     return null;
 37   }
 38 
 39   public static byte[] encrypt(String content, String password) {
 40     try {
 41       KeyGenerator kgen = KeyGenerator.getInstance("AES");
 42       kgen.init(128, new SecureRandom(password.getBytes()));
 43       SecretKey secretKey = kgen.generateKey();
 44       byte[] enCodeFormat = secretKey.getEncoded();
 45       SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
 46       Cipher cipher = Cipher.getInstance("AES");// 创建密码器
 47       byte[] byteContent = content.getBytes("utf-8");
 48       cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
 49       byte[] result = cipher.doFinal(byteContent);
 50       return result; // 加密
 51     }
 52     catch (Exception e) {
 53       e.printStackTrace();
 54     }
 55     return null;
 56   }
 57 
 58   public static void main(String[] args) {
 59     String content = "给我加密把";
 60     String password = "这是加密密钥";
 61     // 加密
 62     System.out.println("加密前:" + content);
 63     byte[] encryptResult = AESUtils.encrypt(content, password);
 64     String encryptResultStr = AESUtils.parseByte2HexStr(encryptResult);
 65     System.out.println("加密后:" + encryptResultStr);
 66     // 解密
 67     byte[] decryptFrom = AESUtils.parseHexStr2Byte(encryptResultStr);
 68     byte[] decryptResult = AESUtils.decrypt(decryptFrom, password);
 69     System.out.println("解密后:" + new String(decryptResult));
 70   }
 71 
 72   /**
 73    * 将二进制转换成16进制
 74    *
 75    * @param buf
 76    * @return
 77    */
 78   public static String parseByte2HexStr(byte buf[]) {
 79     StringBuffer sb = new StringBuffer();
 80     for (int i = 0; i < buf.length; i++) {
 81       String hex = Integer.toHexString(buf[i] & 0xFF);
 82       if (hex.length() == 1) {
 83         hex = ‘0‘ + hex;
 84       }
 85       sb.append(hex.toUpperCase());
 86     }
 87     return sb.toString();
 88   }
 89 
 90   /**
 91    * 将16进制转换为二进制
 92    *
 93    * @param hexStr
 94    * @return
 95    */
 96   public static byte[] parseHexStr2Byte(String hexStr) {
 97     if (hexStr.length() < 1) {
 98       return null;
 99     }
100     byte[] result = new byte[hexStr.length() / 2];
101     for (int i = 0; i < hexStr.length() / 2; i++) {
102       int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
103       int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
104       result[i] = (byte) (high * 16 + low);
105     }
106     return result;
107   }
108 }

运行结果:

技术分享

 

AES加密解密

标签:generate   vax   and   code   amp   log   初始化   img   substr   

原文地址:http://www.cnblogs.com/zanderblogs/p/7560296.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!