javaaes_cbc_256加密解密
在之前我们在openssl上和ios上分别测试了 AES256位cbc模式的加密和解密
今天⽤java提供的api来测试⼀下;进⽽确定⼀下在PC,iOS,安卓上三个平台下的加密解密数据;
1. ⾸先通过查询资料了解到:
java的jdk 不⽀持256位AES密钥,需要安装扩展包,以下会提到
java 中默认不⽀持PKCS7 填充⽅式,⽀持PKCS5,经测试PKCS5和pkcs7没多⼤区别(在多平台数据互测上) java中如果⾮要指定PKCS7 需要借助 BouncyCastle(java⽤的密码学类库)
2. 关于安装 ⽀持AES 256位密钥的扩展包
下载之后得到 local_policy.jar ,US_export_policy.jar 两个jar包,把这两个jar包放到 jdk/lib/security ⽬录下  替换原来的两个jar包即可
3.  使⽤java api 进⾏ AES_cbc_256位密钥 PKCS5 填充⽅式的加密解密
如下封装
private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
//加密
public static byte[] AES_cbc_encrypt(byte[] srcData,byte[] key,byte[] iv)
{
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Instance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv));
byte[] encData = cipher.doFinal(srcData);
return encData;
}
//解密
java加密方式有哪些
public static byte[] AES_cbc_decrypt(byte[] encData,byte[] key,byte[] iv)
{
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Instance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv));
byte[] decbbdt = cipher.doFinal(encData);
return decbbdt;
}
测试调⽤
public static void main(String[] args) {
byte[] key= new byte[32];
byte[] iv = new byte[16];
String srcStr = "This is java default pkcs5padding PKCS5 TEST";
System.out.println(srcStr);
//设置key 全8,iv,全1,这⾥测试⽤
for (int i = 0; i <32; i++) {
key[i] = 8;
if (i < 16) {iv[i] = 1;}
}
byte[] encbt = AES_cbc_Bytes(),key,iv);
byte[] decbt = AES_cbc_decrypt(encbt,key,iv);
String decStr = new String(decbt);
System.out.println(decStr);
if(srcStr.equals(decStr))
{
System.out.println("TEST PASS");
}else
{
System.out.println("TEST NO PASS");
}
}
输出:
This is java default pkcs5padding PKCS5 TEST
This is java default pkcs5padding PKCS5 TEST
TEST PASS
View Code
总结:此处使⽤的是java提供的PKCS5填充⽅式,其实和PKCS7是⼀样的,所以在ios上使⽤pkcs7,安卓上使⽤pkcs5,只要key和iv保持⼀致,相互加解密是没有问题的
完整的代码如下
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.util.Base64;
pto.BadPaddingException;
pto.Cipher;
pto.IllegalBlockSizeException;
pto.NoSuchPaddingException;
pto.spec.IvParameterSpec;
pto.spec.SecretKeySpec;
public class TestPKCS5 {
private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
//加密
public static byte[] AES_cbc_encrypt(byte[] srcData,byte[] key,byte[] iv) throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingExceptio    {
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Instance(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, NoSuchPaddingExceptio    {
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Instance(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, InvalidAlgori
byte[] key= new byte[32];
byte[] iv = new byte[16];
String srcStr = "This is java default pkcs5padding PKCS5 TEST";
System.out.println(srcStr);
//设置key 全8,iv,全1,这⾥测试⽤
for (int i = 0; i <32; i++) {
key[i] = 8;
if (i < 16) {iv[i] = 1;}
}
byte[] encbt = AES_cbc_Bytes(),key,iv);
byte[] decbt = AES_cbc_decrypt(encbt,key,iv);
String decStr = new String(decbt);
System.out.println(decStr);
if(srcStr.equals(decStr))
{
System.out.println("TEST PASS");
}else
{
System.out.println("TEST NO PASS");
}
}
}
4.  使⽤ BouncyCastle库 进⾏ AES_cbc_256 指定PKCS7⽅式加密解密
如下封装
//AES_256_cbc pkcs7
private static final String ALGORITHM = "AES/CBC/PKCS7Padding";
//加密
public static byte[] AES_cbc_encrypt(byte[] srcData,byte[] key,byte[] iv)
{
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
Security.addProvider(new BouncyCastleProvider());
Cipher cipher = Instance(ALGORITHM,"BC");
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)
{
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
Security.addProvider(new BouncyCastleProvider());
Cipher cipher = Instance(ALGORITHM,"BC");
cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv));        byte[] decbbdt = cipher.doFinal(encData);
return decbbdt;
}
  调⽤测试代码
public static void main(String[] args)  {
// TODO Auto-generated method stub
byte[] key= new byte[32];
byte[] iv = new byte[16];
String srcStr = "This is java bcprovlib pkcs7padding PKCS7 TEST";
System.out.println(srcStr);
//设置key 全8,iv,全1,这⾥测试⽤
for (int i = 0; i <32; i++) {
key[i] = 8;
if (i < 16) {iv[i] = 1;}
}
byte[] encbt = AES_cbc_Bytes(),key,iv);
byte[] decbt = AES_cbc_decrypt(encbt,key,iv);
String decStr = new String(decbt);
System.out.println(decStr);
if(srcStr.equals(decStr))
{
System.out.println("TEST PASS");
}else
{
System.out.println("TEST NO PASS");
}
}
  输出结果:
This is java bcprovlib pkcs7padding PKCS7 TEST
This is java bcprovlib pkcs7padding PKCS7 TEST
TEST PASS
View Code
总结:此处使⽤BC库的⽅式指定PKCS7⽅式,这样就和ios pc保持⼀致了;不过使⽤上⾯第⼀种⽅式也是可⾏的
完整的代码如下
import java.util.Base64;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.util.Arrays;
pto.BadPaddingException;
pto.Cipher;
pto.IllegalBlockSizeException;
pto.KeyGenerator;
pto.NoSuchPaddingException;
pto.SecretKey;
pto.spec.IvParameterSpec;
pto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class TestAES {
//AES_256_cbc pkcs7
private static final String ALGORITHM = "AES/CBC/PKCS7Padding";
//加密
public static byte[] AES_cbc_encrypt(byte[] srcData,byte[] key,byte[] iv) throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingExceptio    {
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
Security.addProvider(new BouncyCastleProvider());
Cipher cipher = Instance(ALGORITHM,"BC");
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, NoSuchPaddingExceptio    {
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
Security.addProvider(new BouncyCastleProvider());
Cipher cipher = Instance(ALGORITHM,"BC");
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, InvalidAlgori  // TODO Auto-generated method stub
byte[] key= new byte[32];
byte[] iv = new byte[16];
String srcStr = "This is java bcprovlib pkcs7padding PKCS7 TEST";
System.out.println(srcStr);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。