AES加解密过程
Posted wings-ff
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AES加解密过程相关的知识,希望对你有一定的参考价值。
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加解密过程的主要内容,如果未能解决你的问题,请参考以下文章