小编Joh*_*ijn的帖子

有没有办法在 PHP 中使用 Sodium Encrypt 而不使用随机数?

我正在对新库“Libsodium”进行一些实验。基于https://www.zimuel.it/slides/zendcon2018/sodium#/21幻灯片。这张幻灯片中有一个有关钠加密和解密的示例。

 $msg = 'This is a super secret message!';

// Generating an encryption key and a nonce
$key   = random_bytes(SODIUM_CRYPTO_SECRETBOX_KEYBYTES); // 256 bit
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); // 24 bytes

// Encrypt
$ciphertext = sodium_crypto_secretbox($msg, $nonce, $key);
// Decrypt
$plaintext = sodium_crypto_secretbox_open($ciphertext, $nonce, $key);

echo $plaintext === $msg ? 'Success' : 'Error';
Run Code Online (Sandbox Code Playgroud)

我在 PHP 类方法中使用了它,如下所示:

public function sodium_encrypt($p_sPlaintext)            
{
    try{
        if(!empty($p_sPlaintext) && is_string($p_sPlaintext)){
            // Generating an encryption key and a nonce
            $key   = random_bytes(SODIUM_CRYPTO_SECRETBOX_KEYBYTES); // 256 bit                        
            $nonce = …
Run Code Online (Sandbox Code Playgroud)

php encryption sodium

3
推荐指数
1
解决办法
4384
查看次数

标签 统计

encryption ×1

php ×1

sodium ×1