JAVA_AesCBC纯净例子

时间:2021-11-29 01:37:34
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException; import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec; import com.utils.CHexConver; /**
* @author kelin.ll
* @date on 2018/6/21
*/
public class TestPKCS5Demo {
// key
private static String sKey="****************";
// IV
private static String ivParameter="****************"; // 正常加解密逻辑中使用
private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
// 本地测试 hex值得解密时使用
//private static final String ALGORITHM = "AES/CBC/NoPadding";
//加密
public static byte[] AES_cbc_encrypt(byte[] srcData,byte[] key,byte[] iv) throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException
{
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv));
byte[] encData = cipher.doFinal(srcData);
return encData;
} //解密
public static byte[] AES_cbc_decrypt(byte[] encData,byte[] key,byte[] iv) throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException
{
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv));
byte[] decbbdt = cipher.doFinal(encData);
return decbbdt;
} public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { byte[] key = sKey.getBytes();
byte[] iv = ivParameter.getBytes();
String test = "96 4d a0 c3 96 6e 48 c7 49 e4 43 79 cd 75 91 69"
+ "42 7f 0e bb e2 be 60 e7 40 e2 f0 5b 32 bf 27 da"
+ "93 86 bb 78 1f bc 0c 46 7c 73 4e 72 d5 0c 9e 7c"
+ "67 3d 08 39 33 32 54 ad e1 7f b6 f9 14 d5 61 e8"
+ "bc 1f 58 79 2a a8 d6 9a 9d 15 82 f7 26 00 8b 9d"
+ "9d 21 e5 55 11 f0 54 93 d6 30 34 20 a2 20 22 23"
+ "fe cd f2 83 cf ab d0 87 db f5 b8 b4 0e 59 6e ec"
+ "fc d7 6a 96 2a ea c8 7c 73 2c c6 64 10 c8 a3 77"
+ "14 94 c6 97 4c 2f 37 db 55 d3 12 c0 6c 4c 73 ce"
+ "00 b5 43 d8 f2 e3 e4 7d c4 a3 ba 8e 9b 22 75 e1"
+ "7b ea ce 16 4c 4f 04 20 f5 30 f7 41 de 2a 89 cf"
+ "9d 4b 21 9c d2 1a 0f ab ac 00 15 01 1c 11 af 80"
+ "a2 da f3 6f 1e df 6c 4d 1e 04 71 82 a0 3c 2e 8d"
+ "38 1b a0 66 37 c8 45 a2 33 b4 d6 7d 39 32 05 d5"
+ "1a a1 b7 20 0b f6 dc 2e f6 35 c0 81 88 3f 75 1d"
+ "a4 bc 31 7d 9e b8 82 a3 28 0d 34 d0 93 82 b9 12"
+ "2b 07 9f b4 9a a2 24 b9 9a a1 6e 70 27 e1 d7 21"
+ "c6 6b 5c 7b 3e 6b 1f 23 27 29 b5 cc cf 0a ab 05"
+ "fa 02 20 c3 02 ee 0d 00 61 a0 b7 54 a5 5f 5a 90";
byte[] contentByte = CHexConver.hexStr2Bytes(test); byte[] encbt = AES_cbc_encrypt(test.getBytes(),key,iv);
String encStr = new String(encbt);
System.out.println(encStr); byte[] decbt = AES_cbc_decrypt(encbt,key,iv);
//byte[] decbt = AES_cbc_decrypt(contentByte,key,iv);
String decStr = new String(decbt);
System.out.println(decStr); }
}