PHP
如何将JAVA的aesKey加密转换成PHP
本篇博文为大家讲诉如何将JAVA的aesKey加密转换成PHP
之前博主遇到了一个需求,需要调用银行的一个接口,其中涉及到aesKey加密跟解密的问题,虽然有DEMO,但无奈是JAVA版本的,而博主的环境的PHP,所以碰壁了好久终于调通,所以将为大家分享如何实现转换。
首先看下JAVA版本的:
加密方法: public class TenpayEncryptionUtils { public static String encrypt(String data,String type) { String md5Key = CommUtils.getConfig(type+"_MD5KEY"); String aesKey = CommUtils.getConfig(type+"_AESKEY"); try { data = URLEncoder.encode(data,"utf-8"); } catch (UnsupportedEncodingException e) { data = null; } if(data != null){ String aesStr = EncryptionUtils.encrypt(data, aesKey); String md5Str = EncryptionMD5.encodeByMd5(aesStr+md5Key); JSONObject json=new JSONObject(); json.put("data", aesStr); json.put("sign", md5Str); return json.toString(); } return null; } public static String decrypt(String data, String sign,String type) { String md5Key = CommUtils.getConfig(type+"_MD5KEY"); String aesKey = CommUtils.getConfig(type+"_AESKEY"); if(!EncryptionMD5.encodeByMd5(data + md5Key).equals(sign)){ return null; } String decodedData = EncryptionUtils.decrypt(data, aesKey); try { decodedData = URLDecoder.decode(decodedData, "UTF-8"); } catch (UnsupportedEncodingException e) { decodedData = null; } return decodedData; } 字符转成字节,然后加密: public class EncryptionUtils { public static String encrypt(String content, String key) { if(!checkKey(key)){ return null; } try { Cipher aesECB = Cipher.getInstance("AES/ECB/PKCS5Padding"); SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES"); aesECB.init(Cipher.ENCRYPT_MODE, secretKey); byte[] result = aesECB.doFinal(content.getBytes("UTF-8")); return Hex.encodeHexString(result); } 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 (UnsupportedEncodingException e) { e.printStackTrace(); } return null; } public static String decrypt(String content, String key) { if(!checkKey(key)){ return null; } try { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// 创建密码器 SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES"); cipher.init(Cipher.DECRYPT_MODE, secretKey);// 初始化 byte[] contentBytes = Hex.decodeHex(content.toCharArray()); return new String(cipher.doFinal(contentBytes)); // 解密 } 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 (DecoderException e) { e.printStackTrace(); } return null; } private static boolean checkKey(String key){ if(key == "" || key.length() < 6){ return false; } return true; } }
下面是PHP版本的:
/////////////////// 根据data进行aesKey加密后获得sign /////////////////// /** * aesKey加密 * @param array $dataParams * @return array|string */ static function orderEncryption($dataParams = array()) { //数组转成Json方法【防止中文乱码】 $dataParams = self::__jsonToUtf8($dataParams); //URLEncode进行转换防止中文乱码 $dataParams = urlencode($dataParams); //AES加密 $dataParams = self::encrypt($dataParams, 'jfdshfjdsfhjytgb'); //将AES加密后的字符串再加上MD5KEY后再MD5加密 $md5Key = "123456"; $dataParams = md5($dataParams . $md5Key); //返回json return $dataParams; } /** * 对数组进行json编码,防止中文乱码 * @param $testJSON * @return string */ static function __jsonToUtf8($testJSON) { foreach ($testJSON as $key => $value) { $testJSON[$key] = urlencode($value); } return urldecode(json_encode($testJSON)); } /** * AES加密 * @param $input * @param $key * @return string */ static function encrypt($input, $key) { //@用于兼容高版本PHP $size = @mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB); //调取字符串加密方法 $input = self::pkcs5_pad($input, $size); $td = @mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, ''); $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); //进行hex编码 $data = bin2hex($data); //返回加密后的字符串 return $data; } /** * 字符串加密 * @param $text * @param $blockSize * @return string */ static function pkcs5_pad($text, $blockSize) { $pad = $blockSize - (strlen($text) % $blockSize); return $text . str_repeat(chr($pad), $pad); } ///////////////////// 数据AES解密 ///////////////////// /** * 数据解密 * @param $sStr * @param $sKey * @return bool|string */ static function decrypt($sStr, $sKey) { //转换格式 $sStr = hex2bin($sStr); //使用AES/ECB/PKCS5Padding模式进行解密 $decrypted = @mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $sKey, $sStr, MCRYPT_MODE_ECB); //截取掉末尾 $dec_s = strlen($decrypted); $padding = ord($decrypted[$dec_s - 1]); $decrypted = substr($decrypted, 0, -$padding); //得到解密体再进行还原成解码前的json数据 $jsonData = urldecode($decrypted); //将json数据转换成数组格式 $dataArray = json_decode($jsonData, true); //返回解密处理后的数据 return $dataArray; }
0条评论