1 importjava.io.IOException;2 importjava.io.UnsupportedEncodingException;3 importjava.security.InvalidAlgorithmParameterException;4 importjava.security.InvalidKeyException;5 importjava.security.NoSuchAlgorithmException;6
7 importjavax.crypto.BadPaddingException;8 importjavax.crypto.Cipher;9 importjavax.crypto.IllegalBlockSizeException;10 importjavax.crypto.NoSuchPaddingException;11 importjavax.crypto.spec.IvParameterSpec;12 importjavax.crypto.spec.SecretKeySpec;13
14 importorg.apache.commons.lang.StringUtils;15
16 importsun.misc.BASE64Decoder;17 importsun.misc.BASE64Encoder;18
19 /**
20 *
Title: TestAES3
21 *Description:
22 *Copyright: Copyright (c) 2012 All rights reserved.
23 *Company: Neusoft Corporation, China, Ltd.
24 *@authorchendch25 *26 */27 public classTestAES328 {29 private String ALGO = "AES";30 private String ALGO_MODE = "AES/CBC/NoPadding";31 private String akey = "keyskeyskeyskeys";32 private String aiv = "keyskeyskeyskeys";33 private String padding = " ";34 private int blockSize = 16;35
36 /**
37 *@paramcontent38 *@returnpassword39 *@throwsNoSuchAlgorithmException40 *@throwsNoSuchPaddingException41 *@throwsUnsupportedEncodingException42 *@throwsInvalidAlgorithmParameterException43 *@throwsInvalidKeyException44 *@throwsBadPaddingException45 *@throwsIllegalBlockSizeException46 */
47 publicString encrypt(String content)48 throwsNoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException,49 BadPaddingException50 {51 String encryptContent =content;52 if (encryptContent.length()
58 Cipher cipher =Cipher.getInstance(ALGO_MODE);59 SecretKeySpec keyspec = new SecretKeySpec(akey.getBytes("utf-8"), ALGO);60 IvParameterSpec ivspec = new IvParameterSpec(aiv.getBytes("utf-8"));61 cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);62 byte[] byteEncode = encryptContent.getBytes("utf-8");63 byte[] byteAES =cipher.doFinal(byteEncode);64
65 String AESEncode = new String(newBASE64Encoder().encode(byteAES));66
67 returnAESEncode;68 }69
70 /**
71 *@paramcontent72 *@returncontent73 *@throwsNoSuchAlgorithmException74 *@throwsNoSuchPaddingException75 *@throwsInvalidKeyException76 *@throwsInvalidAlgorithmParameterException77 *@throwsIllegalBlockSizeException78 *@throwsBadPaddingException79 *@throwsIOException80 */
81 publicString decrypt(String content)82 throwsNoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException,83 BadPaddingException, IOException84 {85 byte[] byte_content = newBASE64Decoder().decodeBuffer(content);86
87 Cipher cipher =Cipher.getInstance(ALGO_MODE);88 SecretKeySpec keyspec = new SecretKeySpec(akey.getBytes("utf-8"), ALGO);89 IvParameterSpec ivspec = new IvParameterSpec(aiv.getBytes("utf-8"));90
91 cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);92
93 byte[] byte_decode =cipher.doFinal(byte_content);94 String AES_decode = new String(byte_decode, "utf-8");95 returnAES_decode.trim();96 }97
98 /**
99 *@paramargs100 *@throwsNoSuchAlgorithmException101 *@throwsNoSuchPaddingException102 *@throwsBadPaddingException103 *@throwsIllegalBlockSizeException104 *@throwsInvalidAlgorithmParameterException105 *@throwsInvalidKeyException106 *@throwsIOException107 */
108 public static voidmain(String[] args)109 throwsNoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException,110 BadPaddingException, IOException111 {112 TestAES3 testAES3 = newTestAES3();113 String content = "chenduoc";114 System.out.println("加密前:" +content);115
116 String encryptContent =testAES3.encrypt(content);117 System.out.println("加密后:" +encryptContent);118
119 String decryptContent =testAES3.decrypt(encryptContent);120 System.out.println("解密后:" +decryptContent);121 }122 }