Java 算法需求
image.png
使用openssl进行AES/CBC/PKCS5Padding加密
问题解决了。
@openssl_encrypt($data, 'AES-128-CBC', $Key,OPENSSL_RAW_DATA);
iv不填写直接加密,这样子就行了
不过php会出现warning,在前面加一个@就可以了
php7 版 示例
ase.php
<?php
namespace app\admin\model;
class Ase
{
/**
* [encrypt aes加密]
* @param [type] $input [要加密的数据]
* @param [type] $key [加密key]
* @return [type] [加密后的数据]
*/
public static function encrypt($input, $key)
{
$key = self::_sha1prng($key);
$iv = '';
$data = openssl_encrypt($input, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
$data = self::url_safe_b64encode($data);
return $data;
}
/**
* [decrypt aes解密]
* @param [type] $sStr [要解密的数据]
* @param [type] $sKey [加密key]
* @return [type] [解密后的数据]
*/
public static function decrypt($sStr, $sKey)
{
$sKey = self::_sha1prng($sKey);
$decrypted = openssl_decrypt(base64_decode($sStr), 'AES-128-ECB', $sKey, OPENSSL_RAW_DATA);
return $decrypted;
}
/**
* SHA1PRNG算法
* @param [type] $key [description]
* @return [type] [description]
*/
private static function _sha1prng($key)
{
return substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
}
/**
*url base64编码
* @param $string
* @return string|string[]
*/
private static function url_safe_b64encode($string) {
$data = base64_encode($string);
$data = str_replace(array('+','/','='),array('-','_',''),$data);
return $data;
}
/**
* url base64解码
* @param $string
* @return false|string
*/
private static function url_safe_b64decode($string) {
$data = str_replace(array('-','_'),array('+','/'),$string);
$mod4 = strlen($data) % 4;
if ($mod4) {
$data .= substr('====', $mod4);
}
return base64_decode($data);
}
}
使用文件
$jsonData = json_encode($data, JSON_UNESCAPED_UNICODE); //数组转json Utf-8
$appKeyHash = md5(self::$appKey);
$checkCodeHash = md5($jsonData . self::$apiCode . self::$appKey);
$jsonDataAES = Ase::encrypt(urlencode($jsonData), self::$appKey);