php与java对接 AES/CBC/PKCS5Padding 加密

Java AES/CBC/PKCS5Padding 加密

import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class EncryptUtil
{

    public static String encodeAESAndBase64(String content, String key)
    {
        byte[] bs1 = encodeAESForb(content, key);
        return encodeBase64(bs1);
    }

    public static byte[] encodeAESForb(String content, String password)
    {
        try
        {
            Cipher cipher = initCipher(Cipher.ENCRYPT_MODE, password);
            return cipher.doFinal(content.getBytes("UTF-8"));
        }
        catch (Exception e)
        {
            return null;
        }
    }

    private static Cipher initCipher(int mode, String password) throws Exception
    {
        // 默认为128位加密
        SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES");
        // 创建密码器
        Cipher cipher = Cipher.getInstance("AES");
        // 初始化
        cipher.init(mode, key);
        return cipher;
    }

    public static String encodeBase64(byte[] bs)
    {
        if (bs == null)
            return null;
        return (new BASE64Encoder()).encode(bs);
    }

}


String key="xxx";
String origin=encodeAESAndBase64(str, key);
return origin;

对应的PHP AES/CBC/PKCS5Padding 加密

 /**
     * This was AES-128 / CBC / PKCS5Padding
     * return base64_encode string
     * @author Terry
     * @param string $plaintext
     * @param string $key
     * @return string
     */
    public 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);      //java里面的16个空数组对应的是\0.
        /* 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 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 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 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);


        /* Terminate decryption handle and close module */
        mcrypt_generic_deinit($module);
        mcrypt_module_close($module);
        $a = rtrim($decrypted,"\0");


        return rtrim($decrypted,"\0");
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值