- 1
- /**
- 2 * 加密
- 3 * @param content
- 4 * @param keyBytes
- 5 * @param iv
- 6 * @return
- 7 * @throws Exception
- 8 */
- 9 public String AES_CBC_Encrypt(byte[] content, byte[] keyBytes, byte[] iv) throws Exception {
- 10
- try {
- 11 KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); //返回生成指定算法的秘密密钥的 KeyGenerator 对象
- 12 keyGenerator.init(128, new SecureRandom(keyBytes)); //使用用户提供的随机源初始化此密钥生成器,使其具有确定的密钥大小
- 13 SecretKey key = keyGenerator.generateKey(); // 生成一个密钥。
- 14 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
- 15 cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
- 16 byte[] result = cipher.doFinal(content);
- 17
- return Base64.encodeBase64String(result);
- 18
- } catch(Exception e) {
- 19 e.printStackTrace();
- 20
- throw e;
- 21
- }
- 22
- }
来源: http://www.bubuko.com/infodetail-1980459.html