PHP AES加密/解密

我找到了在PHP中对字符串进行编码/解码的示例。起初它看起来非常好,但是不会起作用:-(

有人知道问题出在哪里吗?

$Pass = "Passwort";

$Clear = "Klartext";

$crypted = fnEncrypt($Clear, $Pass);

echo "Encrypted: ".$crypted."</br>";

$newClear = fnDecrypt($crypted, $Pass);

echo "Decrypted: ".$newClear."</br>";

function fnEncrypt($sValue, $sSecretKey) {

return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $sSecretKey, $sDecrypted, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));

}

function fnDecrypt($sValue, $sSecretKey) {

return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $sSecretKey, base64_decode($sEncrypted), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));

}

结果是:

加密: boKRNTYYNp7AiOvY1CidqsAn9wX4ufz/D9XrpjAOPk8=

解密: —‚(ÑÁ ^ yË~F'¸®Ó–í œð2Á_B‰Â—

回答:

$sDecrypted并且$sEncrypted 在您的代码中未定义。查看有效的解决方案( ):


回答:

这个例子是 不要使用它!


$Pass = "Passwort";

$Clear = "Klartext";

$crypted = fnEncrypt($Clear, $Pass);

echo "Encrypred: ".$crypted."</br>";

$newClear = fnDecrypt($crypted, $Pass);

echo "Decrypred: ".$newClear."</br>";

function fnEncrypt($sValue, $sSecretKey)

{

return rtrim(

base64_encode(

mcrypt_encrypt(

MCRYPT_RIJNDAEL_256,

$sSecretKey, $sValue,

MCRYPT_MODE_ECB,

mcrypt_create_iv(

mcrypt_get_iv_size(

MCRYPT_RIJNDAEL_256,

MCRYPT_MODE_ECB

),

MCRYPT_RAND)

)

), "\0"

);

}

function fnDecrypt($sValue, $sSecretKey)

{

return rtrim(

mcrypt_decrypt(

MCRYPT_RIJNDAEL_256,

$sSecretKey,

base64_decode($sValue),

MCRYPT_MODE_ECB,

mcrypt_create_iv(

mcrypt_get_iv_size(

MCRYPT_RIJNDAEL_256,

MCRYPT_MODE_ECB

),

MCRYPT_RAND

)

), "\0"

);

}

**但是此代码中还有其他问题,使其变得不安全,尤其是使用ECB(这不是_加密_模式,只能在其上定义加密模式的构造块)。

以上是 PHP AES加密/解密 的全部内容, 来源链接: utcz.com/qa/436234.html

回到顶部