怎么把php AES128的php java 代码转换成java

1305人阅读
php 和 java 使用 AES128/ECB/PKCS5Padding对称加解密,签名使用 HmacSHA256,附带 php 和 java 代码,均为 DEMO 版本,测试通过,实际使用请根据自己项目修改。
最近做项目涉及到一丢丢的安全问题,需要 PHP 加密,Java 解密,对方使用的 AES加密与解密,填充模式使用AES/ECB/PKCS5Padding,编码为UTF-8,发现PHP的padding与Java的padding不一样,so 需要自己对加密字符串padding后在加密就能解决,php 加密和 java 不一样的问题,至于详细问题请google 不想误人子弟 。闲话不说了,直接上截图和 github 地址,如果帮助到你请给一个”星波波”奥(我真是厚脸皮+虚荣心的人)。注意 java 和 php的代码均为demo版本,切勿拿到生产环境使用,需要修改,我是做PHP 的,java 内功差,部分代码没有catch 异常,哈哈!!!
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:35928次
排名:千里之外
原创:19篇
(2)(7)(10)(1)(1)java用aes加密后的加密串7Ysbf3IeDXCtvW5GjAO47wOWJX9PhdvCKKXZWQthAuAlPZsSQiPRvtNV9f+N cl9u9XMY9ptKJV6Ow3jDx4pBTHuZybs5VjwmMEtYVtaGMt9mWHshO+mHmEU/ 8c4/lyKh7a721jOnJcBOvBcJB2xOEg== php用aes解密后的结果V+3&{k^8A[^;Ll,"custId":"","msg":"URL地址获取失败.","password":"","result":"","status":"","url":""} 前面一部分有乱码请问大神这个问题怎么解决java AES加密代码package com.import java.io.UnsupportedEncodingEimport java.net.URLDimport java.net.URLEimport java.security.InvalidKeyEimport java.security.NoSuchAlgorithmEimport javax.crypto.BadPaddingEimport javax.crypto.Cimport javax.crypto.IllegalBlockSizeEimport javax.crypto.NoSuchPaddingEimport javax.crypto.spec.IvParameterSimport javax.crypto.spec.SecretKeySpublic class AESUtilsCallBack {
public static String encrypt4AES(String source, String key) {
IvParameterSpec zeroIv = new IvParameterSpec(key.getBytes());
SecretKeySpec key1 = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key1, zeroIv);
byte[] encryptedData = cipher.doFinal(source.getBytes());
String encryptResultStr = Base64.encode(encryptedData);
return encryptResultS
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (Exception e) {
public static String decrypt4AES(String content, String key) {
byte[] decryptFrom = Base64.decode(content);
IvParameterSpec zeroIv = new IvParameterSpec(key.getBytes());
SecretKeySpec key1 = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key1, zeroIv);
byte decryptedData[] = cipher.doFinal(decryptFrom);
return new String(decryptedData); // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (Exception e) {
public static void main(String[] args) throws UnsupportedEncodingException {
String info = decrypt4AES("w80LXFCISYdUuLjwSMldpg==", "5678");
System.out.print(info);
}}php EAS加解密代码&?phpclass Tcaes{
* This was AES-128 / CBC / PKCS5Padding
* return base64_encode string
* @author Terry
* @param string $plaintext
* @param string $key
* @return string
public static function AesEncrypt($plaintext,$key = null)
$plaintext = trim($plaintext);
if ($plaintext == '') return '';
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
//PKCS5Padding
$padding = $size - strlen($plaintext) % $
// 添加Padding
$plaintext .= str_repeat(chr($padding), $padding);
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
$key=self::substr($key, 0, mcrypt_enc_get_key_size($module));
$iv = str_repeat("\0", $size);
/* Intialize encryption */
mcrypt_generic_init($module, $key, $iv);
/* Encrypt data */
$encrypted = mcrypt_generic($module, $plaintext);
/* Terminate encryption handler */
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
return base64_encode($encrypted);
* Returns the length of the given string.
* If available uses the multibyte string function mb_strlen.
* @param string $string the string being measured for length
* @return integer the length of the string
private static function strlen($string)
return extension_loaded('mbstring') ? mb_strlen($string,'8bit') : strlen($string);
* Returns the portion of string specified by the start and length parameters.
* If available uses the multibyte string function mb_substr
* @param string $string the input string. Must be one character or longer.
* @param integer $start the starting position
* @param integer $length the desired portion length
* @return string the extracted part of string, or FALSE on failure or an empty string.
private static function substr($string,$start,$length)
return extension_loaded('mbstring') ? mb_substr($string,$start,$length,'8bit') : substr($string,$start,$length);
* This was AES-128 / CBC / PKCS5Padding
* @author Terry
* @param string $encrypted
base64_encode encrypted string
* @param string $key
* @throws CException
* @return string
public static function AesDecrypt($encrypted, $key = null)
if ($encrypted == '') return '';
$ciphertext_dec = base64_decode($encrypted);
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
$key=self::substr($key, 0, mcrypt_enc_get_key_size($module));
$iv = str_repeat("\0", 16);
//解密的初始化向量要和加密时一样。
/* Initialize encryption module for decryption */
mcrypt_generic_init($module, $key, $iv);
/* Decrypt encrypted string */
$decrypted = mdecrypt_generic($module, $ciphertext_dec);
var_dump($module);
/* Terminate decryption handle and close module */
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
$a = rtrim($decrypted,"\0");
return rtrim($decrypted,"\0");
暂无任何回答
69393人关注
33339人关注
Copyright (C)
All Rights Reserved | 京ICP备 号-22012年1月 Java大版内专家分月排行榜第三
2010年2月 Java大版内专家分月排行榜第二
2011年7月 Java大版内专家分月排行榜第三2010年1月 Java大版内专家分月排行榜第三2009年12月 Java大版内专家分月排行榜第三
2011年3月 Java大版内专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。

我要回帖

更多关于 php转java难吗 的文章

 

随机推荐