一、定义
高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在中又称Rijndael加密法,是采用的一种区块加密标准。这个标准用来替代原先的,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由(NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一。
二、AES加密方式的CBC模式
’
三、实现
1 package com.slp; 2 import javax.crypto.Cipher; 3 import javax.crypto.spec.IvParameterSpec; 4 import javax.crypto.spec.SecretKeySpec; 5 import sun.misc.BASE64Decoder; 6 7 public class Encryption 8 { 9 public static void main(String args[]) throws Exception { 10 System.out.println(encrypt()); 11 System.out.println(desEncrypt()); 12 } 13 14 /**15 * 加密16 * @return 17 * @throws Exception18 */19 public static String encrypt() throws Exception { 20 try { 21 String data = "plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize))"; 22 String key = "1234567812345678"; 23 String iv = "1234567812345678"; 24 25 Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); 26 int blockSize = cipher.getBlockSize(); 27 28 byte[] dataBytes = data.getBytes(); 29 int plaintextLength = dataBytes.length; 30 if (plaintextLength % blockSize != 0) { 31 plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize)); 32 } 33 34 byte[] plaintext = new byte[plaintextLength]; 35 System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length); 36 SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES"); 37 IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes()); 38 39 cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec); 40 byte[] encrypted = cipher.doFinal(plaintext); 41 42 return new sun.misc.BASE64Encoder().encode(encrypted); 43 44 } catch (Exception e) { 45 e.printStackTrace(); 46 return null; 47 } 48 } 49 50 /**51 * 解密52 * @return53 * @throws Exception54 */55 public static String desEncrypt() throws Exception { 56 try 57 { 58 // String data = "2fbwW9+8vPId2/foafZq6Q=="; 59 String data = "s3EhlkBgcltfMAW13/K/rNMgqnymdSYNuF5pcHdO1jOVSh/jdZRrWlgQwKv76yO/VAhCU8FLa8M+ivLFEWwIdplTuP/posnYTgldCXDo53s=";60 String key = "1234567812345678"; 61 String iv = "1234567812345678"; 62 63 byte[] encrypted1 = new BASE64Decoder().decodeBuffer(data); 64 65 Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); 66 SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES"); 67 IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes()); 68 69 cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec); 70 71 byte[] original = cipher.doFinal(encrypted1); 72 String originalString = new String(original); 73 return originalString.trim(); 74 } 75 catch (Exception e) { 76 e.printStackTrace(); 77 return null; 78 } 79 } 80 }