溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PHP的AES(高級(jí)加密標(biāo)準(zhǔn)Advanced Encryption Standard)加密

發(fā)布時(shí)間:2020-04-07 16:34:58 來源:網(wǎng)絡(luò) 閱讀:763 作者:hgditren 欄目:軟件技術(shù)
AES介紹
高級(jí)加密標(biāo)準(zhǔn)(英語:Advanced Encryption Standard,縮寫:AES),在密碼學(xué)中又稱Rijndael加密法,是美國(guó)聯(lián)邦政府采用的一種區(qū)塊加密標(biāo)準(zhǔn)。

這個(gè)標(biāo)準(zhǔn)用來替代原先的DES,已經(jīng)被多方分析且廣為全世界所使用。
經(jīng)過五年的甄選流程,高級(jí)加密標(biāo)準(zhǔn)由美國(guó)國(guó)家標(biāo)準(zhǔn)與技術(shù)研究院(NIST)于2001年11月26日發(fā)布于FIPS PUB 197,并在2002年5月26日成為有效的標(biāo)準(zhǔn)。
2006年,高級(jí)加密標(biāo)準(zhǔn)已然成為對(duì)稱密鑰加密中最流行的算法之一。

class AES
{
    public $method = '';
    public $key = '';
    public $iv = '';

    public function __construct(string $method, string $key, string $iv)
    {
        if (!in_array($method, openssl_get_cipher_methods())) {
            throw new \Exception($method . ' encryption method is not support.');
        }
        $this->method = $method;
        $this->key = $key;
        $this->iv = $iv;
    }

    //AES加密
    public function aesEncryption(string $data): string
    {
        $result = openssl_encrypt($data, $this->method, $this->key, OPENSSL_RAW_DATA, $this->iv);
        return base64_encode($result);
    }

    //AES解密
    public function aesDecryption(string $data): string
    {
        return openssl_decrypt(base64_decode($data), $this->method, $this->key, OPENSSL_RAW_DATA, $this->iv);
    }
}

$config = [
    'AES-128-CBC1', //method加密方式  # AES-256-CBC等
    'helloworld', //key加密key
    md5(time() . uniqid(), true), //iv保證偏移量為16位
];

try{
    $obj = new AES(...$config);
    echo $encryptionResult = $obj->aesEncryption('Jack') . PHP_EOL;
    echo $decryptionResult = $obj->aesDecryption($encryptionResult) . PHP_EOL;
}catch (\Exception $e){
    exit($e->getMessage().PHP_EOL);
}
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI