为了账号安全,请及时绑定邮箱和手机立即绑定

php与java对接中aes加密解密乱码问题。

php与java对接中aes加密解密乱码问题。

小晓韩 2016-10-27 17:08:39
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.util;import java.io.UnsupportedEncodingException;import java.net.URLDecoder;import java.net.URLEncoder;import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.NoSuchPaddingException;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;public class AESUtilsCallBack {    public static String encrypt4AES(String source, String key) {        try {            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 encryptResultStr;            // 加密        } 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) {        }        return null;    }    public static String decrypt4AES(String content, String key) {        try {            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) {        }        return null;    }    public static void main(String[] args) throws UnsupportedEncodingException {        String info = decrypt4AES("w80LXFCISYdUuLjwSMldpg==", "1234567812345678");        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) % $size;        // 添加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");    }};
查看完整描述

1 回答

?
chosen1cwp

TA贡献1条经验 获得超0个赞

同样问题  ,我这里也是解析不出俩?不知道  这个问题你最终解决了没有?

查看完整回答
反对 回复 2018-02-06
  • 慕粉4117966
    慕粉4117966
    private function decrypt($data, $key) { return openssl_decrypt(base64_decode($data), "AES-128-CBC", $key, OPENSSL_RAW_DATA,substr($key, 0, 16)); } 问题出现在iv值上,你要把秘钥截成16位作为iv值,希望能帮到你
  • 1 回答
  • 1 关注
  • 6559 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信