真爱趁现在为什么下线OPENSSL在C++/PHP下AES加密解密结果不一致

用户名:xtceetg
文章数:35
访问量:4760
注册日期:
阅读量:1297
阅读量:3317
阅读量:582911
阅读量:467871
51CTO推荐博文
AES加密解密就不说了,网上百度一堆。需要注意的是java与php互通要选择AES/CBC/NoPadding下面还是只接上代码吧
package&com.zns.
import&mons.codec.binary.Base64;
import&javax.crypto.BadPaddingE
import&javax.crypto.C
import&javax.crypto.IllegalBlockSizeE
import&javax.crypto.NoSuchPaddingE
import&javax.crypto.spec.IvParameterS
import&javax.crypto.spec.SecretKeyS
import&java.security.InvalidAlgorithmParameterE
import&java.security.InvalidKeyE
import&java.security.NoSuchAlgorithmE
&*&Created&by&Administrator&on&&0018.
public&class&AES&{
&&&&&*&AES加密
&&&&&*&@param&key&加密需要的KEY
&&&&&*&@param&iv&加密需要的向量
&&&&&*&@param&data&需要加密的数据
&&&&&*&@return
&&&&public&static&String&encrypt(String&key,&String&iv,&String&data)&{
&&&&&&&&byte[]&encrypted&=&{};
&&&&&&&&byte[]&enCodeFormat&=&key.getBytes();
&&&&&&&&SecretKeySpec&secretKeySpec&=&new&SecretKeySpec(enCodeFormat,"AES");
&&&&&&&&try&{
&&&&&&&&&&&&Cipher&cipher&=&Cipher.getInstance("AES/CBC/NoPadding");
&&&&&&&&&&&&cipher.init(Cipher.ENCRYPT_MODE,secretKeySpec,new&IvParameterSpec(iv.getBytes()));
&&&&&&&&&&&&int&blockSize&=&cipher.getBlockSize();
&&&&&&&&&&&&System.out.println(data.length());
&&&&&&&&&&&&byte[]&dataBytes&=&data.getBytes();
&&&&&&&&&&&&int&plaintextLength&=&dataBytes.
&&&&&&&&&&&&if&(plaintextLength&%&blockSize&!=&0)&{
&&&&&&&&&&&&&&&&plaintextLength&=&plaintextLength&+&(blockSize&-&(plaintextLength&%&blockSize));
&&&&&&&&&&&&}
&&&&&&&&&&&&System.out.println(plaintextLength);
&&&&&&&&&&&&byte[]&plaintext&=&new&byte[plaintextLength];
&&&&&&&&&&&&System.arraycopy(dataBytes,&0,&plaintext,&0,&dataBytes.length);
&&&&&&&&&&&&encrypted&=&cipher.doFinal(plaintext);
&&&&&&&&}&catch&(InvalidKeyException&e)&{
&&&&&&&&&&&&e.printStackTrace();
&&&&&&&&}&catch&(InvalidAlgorithmParameterException&e)&{
&&&&&&&&&&&&e.printStackTrace();
&&&&&&&&}&catch&(NoSuchAlgorithmException&e)&{
&&&&&&&&&&&&e.printStackTrace();
&&&&&&&&}&catch&(NoSuchPaddingException&e)&{
&&&&&&&&&&&&e.printStackTrace();
&&&&&&&&}&catch&(BadPaddingException&e)&{
&&&&&&&&&&&&e.printStackTrace();
&&&&&&&&}&catch&(IllegalBlockSizeException&e)&{
&&&&&&&&&&&&e.printStackTrace();
&&&&&&&&return&new&String(Base64.encodeBase64(encrypted));
&&&&&*&@param&key&解密需要的KEY&同加密
&&&&&*&@param&iv&解密需要的向量&同加密
&&&&&*&@param&data&需要解密的数据
&&&&&*&@return
&&&&public&static&String&decrypt(String&key,String&iv,byte[]&data){
&&&&&&&&String&content&=&"";
&&&&&&&&byte[]&enCodeFormat&=&key.getBytes();
&&&&&&&&SecretKeySpec&secretKeySpec&=&new&SecretKeySpec(enCodeFormat,&"AES");
&&&&&&&&Cipher&cipher&=&//&创建密码器
&&&&&&&&try&{
&&&&&&&&&&&&cipher&=&Cipher.getInstance("AES/CBC/NoPadding");
&&&&&&&&&&&&cipher.init(Cipher.DECRYPT_MODE,&secretKeySpec,new&IvParameterSpec(iv.getBytes()));//&初始化
&&&&&&&&&&&&byte[]&result&=&cipher.doFinal(data);
&&&&&&&&&&&&content&=&new&String(result);
&&&&&&&&}&catch&(NoSuchAlgorithmException&e)&{
&&&&&&&&&&&&e.printStackTrace();
&&&&&&&&}&catch&(NoSuchPaddingException&e)&{
&&&&&&&&&&&&e.printStackTrace();
&&&&&&&&}&catch&(BadPaddingException&e)&{
&&&&&&&&&&&&e.printStackTrace();
&&&&&&&&}&catch&(IllegalBlockSizeException&e)&{
&&&&&&&&&&&&e.printStackTrace();
&&&&&&&&}&catch&(InvalidKeyException&e)&{
&&&&&&&&&&&&e.printStackTrace();
&&&&&&&&}&catch&(InvalidAlgorithmParameterException&e)&{
&&&&&&&&&&&&e.printStackTrace();
&&&&&&&&return&&//&加密
}测试用例:public&class&Main&{
&&&&public&static&void&main(String[]&args)&{
&&&&&&&&String&key&=&DigestUtils.sha1Hex("123456").substring(0,16);
&&&&&&&&String&iv&=&"3456";
&&&&&&&&String&data&=&"steven11111";
&&&&&&&&String&encryptData&=&AES.encrypt(key,iv&,data&);
&&&&&&&&System.out.println(encryptData);
&&&&&&&&String&decryptData&=&AES.decrypt(key,iv,Base64.decodeBase64(encryptData));
&&&&&&&&System.out.println(decryptData);
}PHP的代码:crypt方式:&?php
&*&Created&by&PhpStorm.
&*&User:&Administrator
&*&Date:&&0019
&*&Time:&上午&10:40
&&&&&*&@param&$key&加密KEY
&&&&&*&@param&$iv&加密向量
&&&&&*&@param&$data&需要加密的数据
&&&&&*&@return&string
&&&&public&static&function&encrypt($key,&$iv,&$data)
&&&&&&&&/**
&&&&&&&&&*&打开加密
&&&&&&&&&*/
&&&&&&&&$td&=&mcrypt_module_open(MCRYPT_RIJNDAEL_128,&"",&MCRYPT_MODE_CBC,&"");
&&&&&&&&/**
&&&&&&&&&*&初始化加密
&&&&&&&&&*/
&&&&&&&&mcrypt_generic_init($td,&$key,&$iv);
&&&&&&&&/**
&&&&&&&&&*&加密
&&&&&&&&&*/
&&&&&&&&$encrypted&=&mcrypt_generic($td,&$data);
&&&&&&&&/**
&&&&&&&&&*&清理加密
&&&&&&&&&*/
&&&&&&&&mcrypt_generic_deinit($td);
&&&&&&&&/**
&&&&&&&&&*&关闭
&&&&&&&&&*/
&&&&&&&&mcrypt_module_close($td);
&&&&&&&&return&base64_encode($encrypted);
&&&&&*&@param&$key
&&&&&*&@param&$iv
&&&&&*&@param&$data
&&&&&*&@return&string
&&&&public&static&function&decrypt($key,&$iv,&$data)
&&&&&&&&/**
&&&&&&&&&*&打开加密
&&&&&&&&&*/
&&&&&&&&$td&=&mcrypt_module_open(MCRYPT_RIJNDAEL_128,"",MCRYPT_MODE_CBC,"");
&&&&&&&&/**
&&&&&&&&&*&初始化加密
&&&&&&&&&*/
&&&&&&&&mcrypt_generic_init($td,&$key,&$iv);
&&&&&&&&$decode&=&base64_decode($data);
&&&&&&&&/**
&&&&&&&&&*&解密
&&&&&&&&&*/
&&&&&&&&$dencrypted&=&mdecrypt_generic($td,&$decode);
&&&&&&&&/**
&&&&&&&&&*&清理加密
&&&&&&&&&*/
&&&&&&&&mcrypt_generic_deinit($td);
&&&&&&&&/**
&&&&&&&&&*&关闭
&&&&&&&&&*/
&&&&&&&&mcrypt_module_close($td);
&&&&&&&&return&$
}测试用例:include&'AES.class.php';
$key&=&substr(sha1("123456"),&0,&16);
$iv&=&"3456";
$data&=&"steven1121314";
$code&=&AES::encrypt($key,$iv,$data);
echo&PHP_EOL;
echo&AES::decrypt($key,$iv,"0IYawS+QIqyPnWvBAwXW7w==");openssl方式:$open=openssl_encrypt($data,'aes-128-cbc',$key,false,$iv);
echo&PHP_EOL;
echo&openssl_decrypt($open,'aes-128-cbc',$key,false,$iv);代码都测试通过了的。大家有什么不同的意见可以给我留言啊。本文出自 “” 博客,请务必保留此出处
了这篇文章
类别:┆阅读(0)┆评论(0)为什么OPENSSL在C++/PHP下AES加密解密结果不一致? - 知乎9被浏览2166分享邀请回答0添加评论分享收藏感谢收起

我要回帖

更多关于 为什么下城区在上面 的文章

 

随机推荐