在线 AES 128 加密 | 在线 AES 256 加密

AES 128 解密 | AES 256 解密

当选择加密且 iv 填写为空时,请仔细阅读:
1、如果 iv 填写为空,服务端会自动填充一个 16 位伪随机字节串。因为自动生成的 iv 值是二进制数据,所以前端显示时转换为十六进制字符串。
2、因此,当您采取的是自动生成 iv 时,真正的值需要把十六进制字符串转换为二进制字符串。比如通过 PHP 函数 hex2bin 进行转换。
3、自动去除 内容、iv 初始化向量、秘钥 左右多余的空格。

知识科普

这个标准用来替代原先的 DES(Data Encryption Standard),已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院 (NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一 。

该算法为比利时密码学家Joan Daemen和Vincent Rijmen所设计,结合两位作者的名字,以Rijdael之名命之,投稿高级加密标准的甄选流程。(Rijdael的发音近于 "Rhine doll"。)

Example

示例 #1 PHP 7.1+ AES 128 加密、解密之 GCM 模式的示例
<?php
// $key should have been previously generated in a cryptographically safe way, like openssl_random_pseudo_bytes
$plaintext = "message to be encrypted";
$cipher = "aes-128-gcm";
$key = "secret";
$tag = null;
if (in_array($cipher, openssl_get_cipher_methods()))
{
    $ivlen = openssl_cipher_iv_length($cipher);
    // Generate a pseudo-random string of bytes
    $iv = openssl_random_pseudo_bytes($ivlen);
    $ciphertext = openssl_encrypt($plaintext, $cipher, $key, $options=0, $iv, $tag);
    //store $cipher, $iv, and $tag for decryption later
    $original_plaintext = openssl_decrypt($ciphertext, $cipher, $key, $options=0, $iv, $tag);
    echo $original_plaintext."\n";
}

输出的结果($original_plaintext 变量的值):message to be encrypted

示例 #2 PHP 7.1+ AES 256 加密、解密之 CBC 模式的示例
<?php
$plaintext = "message to be encrypted";
$cipher = "aes-256-cbc";
$key = "secret";
if (in_array($cipher, openssl_get_cipher_methods()))
{
    $ivlen = openssl_cipher_iv_length($cipher);
    $iv = '1111112222220000';
    // 加密
    $ciphertext = openssl_encrypt($plaintext, $cipher, $key, $options=0, $iv);
    echo 'ciphertext: ', $ciphertext, "\n";
    // 解密
    $original_plaintext = openssl_decrypt($ciphertext, $cipher, $key, $options=0, $iv);
    echo 'original_plaintext: ', $original_plaintext,"\n";
}

加密的结果(变量 $ciphertext 的值): eTIQTDLVTkYp1FQ6fh6yn3fMOnHKoqWQA+BOi/QX4FM=
解密的结果(变量$original_plaintext 的值): message to be encrypted