当前位置: 首页 > 工具软件 > MCrypt > 使用案例 >

PHP mcrypt_encrypt加密,使用java解密

西门振
2023-12-01

PHP mcrypt_encrypt使用给定的 cipher 和 mode 加密的数据,没有使用pkcs5_pad()函数填充的情况下,如果数据长度不是n*分组大小,则在其后使用“0”补齐。

Java 不能使用AES/ECB/PKCS5Padding,因为填充方式与php不同,不能正常的解密

在java中应该使用AES/ECB/NoPadding方式,手动使用"0",填充补齐

一、PHP加密解密类示例:

<?php
/**
 * AES
 * AES加密解密算法
 * Created by PhpStorm.
 * Date: 2015/4/28
 * Time: 15:41
 */

namespace Fin\App\Library\Util;
class AES {

    /**
     * 算法,另外还有192和256两种长度
     */
    const CIPHER = MCRYPT_RIJNDAEL_128;

    /**
     * 模式
     */
    const MODE = MCRYPT_MODE_ECB;

    /**
     * 加密
     * @param string $key 密钥
     * @param string $str 需加密的字符串
     *
     * @return string
     */
    static public function encode($key, $str)
    {
        if(empty($key)){
            $key = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
        }
        $size = mcrypt_get_iv_size(self::CIPHER, self::MODE);
        $iv = mcrypt_create_iv($size, MCRYPT_RAND);
        $string = mcrypt_encrypt(self::CIPHER, $key, $str, self::MODE, $iv);
        $string = base64_encode($string);
        return $string;
    }

    /**
     * 解密
     * @param type $key
     * @param type $str
     *
     * @return string
     */
    static public function decode($key, $str)
    {
        if(empty($key)){
            $key = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
        }
        $size = mcrypt_get_iv_size(self::CIPHER, self::MODE);
        $iv = mcrypt_create_iv($size, MCRYPT_RAND);
        $string = base64_decode($str);
        $string = mcrypt_decrypt(self::CIPHER, $key, $string, self::MODE, $iv);

        /**
         * 解决下边问题
         * the given cipher and mode. If the size of the data is not n * blocksize,
         * the data will be padded with '\0'.
         */
        $string = trim($string);

        return $string;
    }
}

二、 JAVA加密解密类示例:

import javax.crypto.Cipher;

import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.*;

public class Security {

    public static String encrypt(String input, String key) {

        byte[] crypted = null;

        try {

            input = StringUtils.rightPad(input, 16, "\0");

            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");

            Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");

            cipher.init(Cipher.ENCRYPT_MODE, skey);
            crypted = cipher.doFinal(input.getBytes());

        } catch (Exception e) {

            System.out.println(e.toString());

        }

        return new String(Base64.encodeBase64(crypted));

    }

    public static String decrypt(String input, String key) {

        byte[] output = null;

        try {

            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");

            Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");

            cipher.init(Cipher.DECRYPT_MODE, skey);

            output = cipher.doFinal(Base64.decodeBase64(input));

        } catch (Exception e) {

            System.out.println(e.toString());

        }

        return new String(output);

    }

    public static void main(String[] args) {

        String key = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";

        String data = "412016278912497";

        System.out.println(Security.encrypt(data, key));

        System.out.println(Security.decrypt(Security.encrypt(data, key), key));

    }

}
 类似资料: