网上搜了N多代码,都是你抄我的,我抄你的,着实让人无语对苍天。经过多番资料的查找,php与java的cbc加密、解密结果终于一致了,代码如下:

Java加密解密类:

package main;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; public class AESUtil {
// 加密
public static String Encrypt(String sSrc, String sKey) throws Exception {
if (sKey == null) {
System.out.print("Key为空null");
return null;
}
// 判断Key是否为16位
if (sKey.length() != 16) {
System.out.print("Key长度不是16位");
return null;
}
byte[] raw = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//"算法/模式/补码方式"
//Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
IvParameterSpec iv = new IvParameterSpec("1234567890123456".getBytes());//使用CBC模式,需要一个向量iv,可增加加密算法的强度 cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
//cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(sSrc.getBytes("UTF-8")); return new String(Base64.encodeBase64(encrypted));
} // 解密
public static String Decrypt(String sSrc, String sKey) throws Exception {
try {
// 判断Key是否正确
if (sKey == null) {
System.out.print("Key为空null");
return null;
}
// 判断Key是否为16位
if (sKey.length() != 16) {
System.out.print("Key长度不是16位");
return null;
}
byte[] raw = sKey.getBytes("UTF-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec("1234567890123456"
.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
//cipher.init(Cipher.DECRYPT_MODE, skeySpec);
Base64.decodeBase64(sSrc.getBytes());
byte[] encrypted1 = Base64.decodeBase64(sSrc.getBytes());
try {
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original);
return originalString;
} catch (Exception e) {
System.out.println(e.toString());
return null;
}
} catch (Exception ex) {
System.out.println(ex.toString());
return null;
}
}
}

Java调用:

public static void main(String[] args) {
// TODO Auto-generated method stub String cKey = "1234567890123456";
// 需要加密的字串
String cSrc = "test string";
System.out.println(cSrc); String enString;
String deString;
try {
enString = AESUtil.Encrypt(cSrc, cKey);
System.out.println("加密后的字串是:" + enString); deString=AESUtil.Decrypt(enString, cKey);
System.out.println("解密后的字串是:" + deString);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }

要注意,AESUtil类中,有个iv变量,要和php中的iv对应起来。

PHP加密解密类:

class MyAES {
public function encryptString($input, $key,$iv) {
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$input = MyAES::pkcs5_pad($input, $size);
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
//$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$data = base64_encode($data);
return $data;
} private static function pkcs5_pad ($text, $blocksize) {
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
} public function decryptString($sStr, $sKey,$iv) {
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
//$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
$decrypted= @mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $sKey, base64_decode($sStr), MCRYPT_MODE_CBC, $iv);
$dec_s = strlen($decrypted);
$padding = ord($decrypted[$dec_s-1]);
$decrypted = substr($decrypted, 0, -$padding);
return $decrypted;
}
}

PHP调用:

$msg = 'test string';
$key='1234567890123456';
$iv=$key;
$x=new MyAES(); $en=$x->encryptString($msg, $key,$iv);
echo $en,'<br />'; echo $x->decryptString($en,$key,$iv);

参考资料:http://blog.163.com/fan_xy_qingyuan/blog/static/188987748201391124728740/

最新文章

  1. Android开发自学笔记(Android Studio)—4.3ImageView及其子类
  2. AngularJS 控制器 ng-controller
  3. 使用Gradle运行集成测试
  4. Spring第一天
  5. BED format
  6. php 根据指定的键对多维数组进行排序
  7. 怒刷DP之 HDU 1069
  8. IOS 通知 alarm 记录
  9. default parameter value for ‘color’ must be a compile-time constant
  10. (转载)完美解决PHP中文乱码问题
  11. BOM 之 window
  12. oStrictHostKeyChecking=no 参数
  13. LeetCode 674. Longest Continuous Increasing Subsequence (最长连续递增序列)
  14. Centos7查看IP
  15. .NET controller传给view的bool类型
  16. faiss索引基于数量级和内存限制的选择
  17. python nose 自写插件支持用例带进度
  18. H5开发:横屏适配
  19. html5移动端查找
  20. 关于ORA-00979 不是 GROUP BY 表达式错误的解释

热门文章

  1. Excel单元格所在的行和列变色
  2. Oracle-11g 中两库间物化视图的同步
  3. sharepoint 2010版本 图文安装
  4. bzoj1588: [HNOI2002]营业额统计 splay瞎写
  5. 个人PE流程备忘
  6. Windows/Linux 生成iOS证书及p12文件
  7. c# PictureBox 的图像上使用鼠标画矩形框
  8. linux 学习-用户&amp;群组&amp;权限
  9. C# Volatile 类
  10. perl的列表(List)和数组(Array)