Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中用libnail加密/解密url参数_Php_Security_Libsodium - Fatal编程技术网

如何在PHP中用libnail加密/解密url参数

如何在PHP中用libnail加密/解密url参数,php,security,libsodium,Php,Security,Libsodium,我对这个话题完全陌生,我了解到加密敏感信息并在URL中传递它不是一个好的做法,但有人建议如果我们还想继续使用强大的库进行加密和解密,所以我决定使用libsom来加密我的数据,但我面临一个问题,因为我实际上使用的是MVC架构,我认为密钥不一样,因为GET to decrypt在我的controller.php中,cat_标题在我的function.php中,下面是不同的文件 functions.php function safeEncrypt($message, $key) { $nonc

我对这个话题完全陌生,我了解到加密敏感信息并在URL中传递它不是一个好的做法,但有人建议如果我们还想继续使用强大的库进行加密和解密,所以我决定使用libsom来加密我的数据,但我面临一个问题,因为我实际上使用的是MVC架构,我认为密钥不一样,因为GET to decrypt在我的controller.php中,cat_标题在我的function.php中,下面是不同的文件

functions.php

function safeEncrypt($message, $key)
{
    $nonce = random_bytes(
        SODIUM_CRYPTO_SECRETBOX_NONCEBYTES
    );

    $cipher = base64_encode(
        $nonce.
        sodium_crypto_secretbox(
            $message,
            $nonce,
            $key
        )
    );
    sodium_memzero($message);
    sodium_memzero($key);
    return $cipher;
}


function safeDecrypt($encrypted, $key)
{
    $decoded = base64_decode($encrypted);
    if ($decoded === false) {
        throw new Exception('Scream bloody murder, the encoding failed');
    }
    if (mb_strlen($decoded, '8bit') < (SODIUM_CRYPTO_SECRETBOX_NONCEBYTES + SODIUM_CRYPTO_SECRETBOX_MACBYTES)) {
        throw new Exception('Scream bloody murder, the message was truncated');
    }
    $nonce = mb_substr($decoded, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, '8bit');
    $ciphertext = mb_substr($decoded, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit');

    $plain = sodium_crypto_secretbox_open(
        $ciphertext,
        $nonce,
        $key
    );
    if ($plain === false) {
         throw new Exception('the message was tampered with in transit');
    }
    sodium_memzero($ciphertext);
    sodium_memzero($key);
    return $plain;
}
//Encrypt & Decrypt your message
$key = random_bytes(SODIUM_CRYPTO_SECRETBOX_KEYBYTES);

<a href='/my_site/product/?action=category&cat_title=".safeEncrypt($category['cat_title'], $key)."'> link_Title </a>
下面是safeDecrypt函数引发的错误

Fatal error: Uncaught Exception: the message was tampered with in transit in

如果你测试这两个函数,它们的加密和解密都很好。因此,这个错误很可能是愚蠢的,因为在解密过程中,您似乎正在重新创建密钥。您必须对加密和加密使用相同的密钥decryption@RiggsFolly,是的,这就是重点,正如我前面提到的,我认为密钥是问题所在,所以有没有办法解决这个问题,请在加密和解密之间将密钥保存在哪里?k,刚刚在解密中使用不同的密钥进行了测试。它给出了这个错误。重要的是,您必须使用相同的密钥进行加密和解密。如果这不是显而易见的,你可能应该玩一些更简单、更不危险的游戏
Fatal error: Uncaught Exception: the message was tampered with in transit in