用于在cookie上存储数据的AES加密

时间:2012-10-06 23:13:27

标签: php cookies encryption aes formula

我需要在具有char字符串的cookie中尽可能存储最大字符,以便用AES加密存储。

cookie有大约4kb的内存。

所以我需要计算我可以在AES加密的所有这些字符中存储多少字符。

要存储的字符是一个字母数字字符串。

编辑:我可以在纯文本和明文中使用所有UTF-8字母数字字符

这是我的代码

 <?php


$Pass = "132ksjcngpt04938idjsk39urtokg";
$Clear = "123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123456789012345678901234567890123456789o";


$crypted = fnEncrypt($Clear, $Pass);
echo "Encrypred: ".$crypted."<br></br>";

$newClear = fnDecrypt($crypted, $Pass);
echo "Decrypred: ".$newClear."<br></br>";



function fnEncrypt($sValue, $sSecretKey)
{
    return trim(
        base64_encode(
            mcrypt_encrypt(
                MCRYPT_RIJNDAEL_128,
                $sSecretKey, $sValue, 
                MCRYPT_MODE_ECB, 
                mcrypt_create_iv(
                    mcrypt_get_iv_size(
                        MCRYPT_RIJNDAEL_128, 
                        MCRYPT_MODE_ECB
                    ), 
                    MCRYPT_RAND)
                )
            )
        );
}

function fnDecrypt($sValue, $sSecretKey)
{
    return trim(
        mcrypt_decrypt(
            MCRYPT_RIJNDAEL_128, 
            $sSecretKey, 
            base64_decode($sValue), 
            MCRYPT_MODE_ECB,
            mcrypt_create_iv(
                mcrypt_get_iv_size(
                    MCRYPT_RIJNDAEL_128,
                    MCRYPT_MODE_ECB
                ), 
                MCRYPT_RAND
            )
        )
    );
}
?>

我测试过,这是结果:

Encrypred: xINH9LnDsiwJjxrxAE7WYKTGBqw2tkKDx/04vryevRfkgKIqO0sRVurGW1Qu267CBuU9CwgS9YIXFs6zhM6y/limz3P8rST6phe5xP9zsw1tX42ngl7SgmK6apoVTjkXtxzY2XQtsuhBv6gPqBG1uoNqhU9QbhQXnbrqQUL7jG0UxGxqbAgb49atHjkTap4H55yzd4dJROlJ9G98rKe1znEU17XDUWjgDkebgG2puew804Sxh+seRMyV4MqmA4bt+uSKGASPQYONyHmY/3pz+yY0HFb/6GERHMOopJGGr17z9ZaQ7R6EeuGko1tiKYxfOX7Ep1hzJqGZ97TWtL+gyF/WaTpFedgLoKTLYe/CcGH3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CbGeml1EtinVN82aTwmrPplqmx+nVQP25S7xEG9VaGtCm9d0y2Ntlc/JvzSSG9cKU+bXl6O871X1vQgkhVvulw7zX5T7MH3N7kVRIawieTzq4aLXT/8cSDR/S5w7IsCY8a8QBO1mCkxgasNrZCg8f9OL68nr0X5ICiKjtLEVbqxltULtuuwgblPQsIEvWCFxbOs4TOsv5Yps9z/K0k+qYXucT/c7MNbV+Np4Je0oJiumqaFU45F7cc2Nl0LbLoQb+oD6gRtbqDaoVPUG4UF5266kFC+4xtFMRsamwIG+PWrR45E2qeB+ecs3eHSUTpSfRvfKyntc5xFNe1w1Fo4A5Hm4BtqbnsPNOEsYfrHkTMleDKpgOG7fcgwEO8b4hbdZM893s5T4W65w7kB3E9LFMARJATuxEONiJHm3YcAFDdl04v/hLNoA==

Decrypred: 123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123hryd{}ls.-''ksllasllallallallallal:{}::{}jdhhfn123456789012345678901234567890123456789o

chiperText由3040 utf-8字符组成

加密最多4056 utf-8字符

2 个答案:

答案 0 :(得分:1)

如果我错了,请更正我,但假设您使用UTF-8作为纯文本的字母数字字符,因为AES使用16字节长度的块(在此上下文中可以被视为16个字母数字字符),纯文本至少填充15字节(最糟糕的情况)。

因此,给定n纯文本的长度,可以推断出生成的密文长度:

(n + 16) - (n % 16)

只需2美分。 达里奥。

答案 1 :(得分:1)

如果使用经过身份验证的流模式(例如AES-GCM),字节中的原始密文大小将比明文大小大32个字节。其中16个用于随机IV,16个用于MAC。

现在您遇到的问题是任意字节无效UTF-8,因此您需要一些编码来处理它。 Base64是一种流行的变体,但它的大小增加到原始大小的4/3 这意味着如果您的大小限制为4KiB,您将能够支持最多3040字节的明文。

还有其他更有效的编码,但这会变得更复杂,您需要确定哪些字符有效(例如\0)。除非绝对必要,否则我不建议这样做。


您的代码有很多错误:

  1. 你正在使用ECB,几乎是所有模式中最差的
  2. 您需要相同的IV进行加密和解密。所以将它与密文一起存储。您没有注意到,因为ECB不使用IV。
  3. 您没有身份验证。
  4. 你的IV代不好。使用MCRYPT_DEV_URANDOM代替MCRYPT_RAND