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

AES加解密过程

时间:2018-06-26 16:10:44      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:div   port   mod   and   decrypt   过程   com   ring   vax   

 1 package com.pff.pff;
 2 
 3 import java.security.SecureRandom;
 4 
 5 import javax.crypto.Cipher;
 6 import javax.crypto.KeyGenerator;
 7 import javax.crypto.SecretKey;
 8 import javax.crypto.spec.SecretKeySpec;
 9 
10 import org.apache.commons.codec.binary.Hex;
11 
12 public class Aes {
13 
14     private static String test = "pff";
15 
16     public static void main(String[] args) {
17         testDes();
18     }
19 
20     public static void testDes() {
21         try {
22             // 生成秘钥
23             KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
24             keyGenerator.init(new SecureRandom());
25             SecretKey generateKey = keyGenerator.generateKey();
26             byte[] encoded = generateKey.getEncoded();
27             // 秘钥转换
28             SecretKeySpec secretKeySpec = new SecretKeySpec(encoded, "AES");
29             // DES加密过程
30             Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5padding");
31             cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
32             byte[] result = cipher.doFinal(test.getBytes());
33             System.out.println("AES加密过程结果= " + Hex.encodeHexString(result));
34             // 解密过程
35             cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
36             byte[] result2 = cipher.doFinal(result);
37             System.out.println("AES解密过程结果= " + new String(result2));
38         } catch (Exception e) {
39             e.printStackTrace();
40         }
41     }
42 }

 

AES加解密过程

标签:div   port   mod   and   decrypt   过程   com   ring   vax   

原文地址:https://www.cnblogs.com/wings-ff/p/9229219.html

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