AES加密解密

Posted zblogs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AES加密解密相关的知识,希望对你有一定的参考价值。

  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加密解密的主要内容,如果未能解决你的问题,请参考以下文章

更新实现代码对称加密与解密剖析:AES,高级加密标准(Advanced Encryption Standard,缩写AES)

AES加密解密代码

QT:AES-256-CBC 根据 PHP 代码在 C++ 中加密/解密

Java aes加密C#解密的取巧方法

Android通过AES128加密解密字符串的代码

基于AES对称加密解密的代码块