PHP AES/CBC/128/PKCS7Padding

标签: none

PHP 7.2 加密实例 PKCS7Padding PKCS5Padding

PKCS5Padding 需要要安装扩展 mcrypt

// CentOS 7 PHP 7.2
yum install php72-mcrypt
// Debian/Ubuntu PHP 7.0
sudo apt-get install php7-mcrypt
<?php
class Aes
{

    private static $key = 'YourKeyxxxxxxxxx';
    private static $iv = 'YourIvxxxxxxxxxx';

    public static function encrypt($data)
    {
        return base64_encode(mcrypt_encrypt(
            MCRYPT_RIJNDAEL_128,
            self::$key,
            self::pkcs5_pad($data),
            MCRYPT_MODE_CBC,
            self::$iv
        ));
    }

    public static function decrypt($data)
    {
        $str = mcrypt_decrypt(
            MCRYPT_RIJNDAEL_128,
            self::$key,
            base64_decode($data),
            MCRYPT_MODE_CBC,
            self::$iv
        );
        return self::pkcs5_unpad($str);
    }

    private static function pkcs5_pad($text)
    {
        $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
        $pad = $blocksize - (strlen($text) % $blocksize);
        return $text . str_repeat(chr($pad), $pad);
    }

    private static function pkcs5_unpad($text)
    {
        $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
        $pad = ord($text[($len = strlen($text)) - 1]);
        $len = strlen($text);
        $pad = ord($text[$len-1]);
        return substr($text, 0, strlen($text) - $pad);
    }

}
<?php
$privateKey = "7854156156611111";
$iv     = "0000000000000000";
$data   = "test";
echo($privateKey."\n");
//加密
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $privateKey, $data, MCRYPT_MODE_CBC, $iv);
echo(base64_encode($encrypted)."\n");

//解密
$encryptedData = base64_decode("L7AswKt5/t1gND4ct22Odw==");
$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $privateKey, $encryptedData, MCRYPT_MODE_CBC, $iv);
echo($decrypted);
?>

PKCS7Padding 使用 openssl 返回 hex

class AES {
    /**
     *
     * @param string $string 需要加密的字符串
     * @param string $key 密钥
     * @return string
     */
    public static function encrypt($string, $key) {
        // 对接java,服务商做的AES加密通过SHA1PRNG算法(只要password一样,每次生成的数组都是一样的),Java的加密源码翻译php如下:
        $key = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
        // openssl_encrypt 加密不同Mcrypt,对秘钥长度要求,超出16加密结果不变
        $data = openssl_encrypt($string, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
        $data = strtoupper(bin2hex($data));
        return $data;
    }
    /**
     * @param string $string 需要解密的字符串
     * @param string $key 密钥
     * @return string
     */
    public static function decrypt($string, $key) {
        // 对接java,服务商做的AES加密通过SHA1PRNG算法(只要password一样,每次生成的数组都是一样的),Java的加密源码翻译php如下:
        $key = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
        $decrypted = openssl_decrypt(hex2bin($string), 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
        return $decrypted;
    }
}
$encrypt = AES::encrypt('test', 'chinaexpressairosjj');
$decrypt = AES::decrypt($encrypt, 'chinaexpressairosjj');
echo "加密后:" . $encrypt . "\n";
echo "解密:" . $decrypt;

AES/CBC/128/PKCS5Padding加密解密算法(iOS、Android、JavaScript、PHP)
PHP AES Encryption/Decryption Class with PKCS5 padding
PHP AES加解密(AES-128-ECB|sha1|bin2hex)
md5 Hash Generator
在线AES加密解密、AES在线加密解密、AES encryption and decryption


扫描二维码,在手机上阅读!

添加新评论