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

Joh*_*ijn 3 php encryption sodium

我正在对新库“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 = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); // 24 bytes                

            // Encrypt Note: the encryption is always authenticated, you need to store also nonce + ciphertext
            $eCiphertext = sodium_crypto_secretbox($p_sPlaintext, $nonce, $key);
            $eCryptotext = $nonce.$eCiphertext;
            if(!empty($eCiphertext)){
                return($eCiphertext);
            } else{
                throw new Exception('clse004');                             // Error trigger
            }
        } else{
            throw new Exception('clse005');                                 // Error trigger
        }
    } catch (Exception $e){
        echo("<br>Errormessage, code: ".$e->getMessage());
    }
}
Run Code Online (Sandbox Code Playgroud)

它的工作原理应该是这样,没有任何问题。但是......:-) 总有一个“但是”。

例如,如果我使用此方法来加密电子邮件地址并将其作为用户登录凭据存储在数据库中,那么它们就是其凭据的唯一加密。下次用户输入他的凭据并对这些凭据进行加密时,我会在数据库中找到他,因为 $key 和 $nonce 是随机生成的。

如果我生成自己的密钥,而不是随机的密钥:( https://www.allkeysgenerator.com/Random/Security-Encryption-Key-Generator.aspx )

 $key   = "4D6251655468576D597133743677397A24432646294A404E635266556A586E32"; // 256 bit hex
Run Code Online (Sandbox Code Playgroud)

然后我收到消息:“密钥大小应为 SODIUM_CRYPTO_SECRETBOX_KEYBYTES 字节”。

用钥匙:

 $key   = "E)H@McQfTjWnZr4u7w!z%C*F-JaNdRgU"; // 256 bit non hex
Run Code Online (Sandbox Code Playgroud)

这个问题也解决了吗?

那么 $ciphertext 仍然是随机的,因为随机数是随机的。用非随机随机数替换随机数发生器,例如:( https://www.random.org/bytes/ )

$nonce = "d80ac8385bb52cef7920ded5bda4de50697efc95ea53d81b" ; // 24 bytes hex
Run Code Online (Sandbox Code Playgroud)

显示此消息:“随机数大小应为 SODIUM_CRYPTO_SECRETBOX_NONCEBYTES 字节”。

与以下相同:

$nonce = "249206220193104991548714284109130186236311295118249161302345616 " ; // 24 bytes decimal
$nonce = "056073032127157034374115050245203151150054323272014260311360377272100266" ; // 24 bytes octal
Run Code Online (Sandbox Code Playgroud)

目前我当前的方法如下:

public function sodium_encrypt($p_sPlaintext)            
{
    try{
        if(!empty($p_sPlaintext) && is_string($p_sPlaintext)){
            // Generating an encryption key and a nonce
            $key   = "E)H@McQfTjWnZr4u7w!z%C*F-JaNdRgU"; // 256 bit                                        
            $nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); // 24 bytes
            // Encrypt Note: the encryption is always authenticated, you need to store also nonce + ciphertext
            $eCiphertext = sodium_crypto_secretbox($p_sPlaintext, $nonce, $key);
            //$eCryptoText = $nonce.$eCiphertext;                
            echo $eCiphertext;
            if(!empty($eCiphertext)){
                return($eCiphertext);
            } else{
                throw new Exception('clse004');                             // Error trigger
            }
            exit();
        } else{
            throw new Exception('clse005');                                 // Error trigger
        }
    } catch (Exception $e){
        echo("<br>Errormessage, code: ".$e->getMessage());
    }
}
Run Code Online (Sandbox Code Playgroud)

我查看了https://download.libsodium.org/doc/secret-key_cryptography但无法找到创建非随机(秘密)随机数以用于此加密实例的解决方案。有人有想法吗?是否有更多人在 PHP 中使用 Sodium?欢迎任何意见。

小智 6

我知道这条消息很旧并且可能已解决,但我想这可能会有所帮助。

NONCE 的长度为24 个字符。-> 这几乎就是答案。但是,如果您尝试使随机数(包括密钥)不是随机的,那么输出将不再是单个数据的一系列无尽的随机字符。这很容易说成一对一。无论如何,KEY是32个字符。让我们试试这个例子。

<?php
class justEncrypt {
     private $key = 'vDIa5JdknBqfrKOu8d7UpddnBMCH1vza'; //32 characters
     private $nonce = 'Ra5LeH7ntW2rvkz3dmqI5Stx'; //24 characters

     public function sodiumStaticEncrypt(string $data) {
           $encrypted = base64_encode(
                 sodium_crypto_secretbox(
                       $data,
                       $this->nonce,
                       $this->key
                 )
           );
           return $encrypted;
    }
}
Run Code Online (Sandbox Code Playgroud)

测试脚本:

<?php
$site = new justEncrypt; // I feel like I should not have done this
    echo $site->sodiumStaticEncrypt("It's better late than never!");
Run Code Online (Sandbox Code Playgroud)

会有一个输出5Was2l9V0RgsgEm5csEbXnCaIxLkWwWiOxnHBgrej9+Ipyqn4ehn8VImFa8=,就是这样,因为你不希望它变得随机,对吗?

检索数据相对容易。只需添加这段代码:

     public function sodiumStaticDecrypt(string $data) {
           // decode the base64 on $data first
           $decrypt = base64_decode($data);
           $decrypted = sodium_crypto_secretbox_open($data,$this->nonce,$this->key);
           return $decrypted;
     }
Run Code Online (Sandbox Code Playgroud)

所以:

<?php

echo $site->sodiumStaticDecrypt('5Was2l9V0RgsgEm5csEbXnCaIxLkWwWiOxnHBgrej9+Ipyqn4ehn8VImFa8=');
Run Code Online (Sandbox Code Playgroud)

它将返回纯文本:It's better late than never!

无论你做什么,只要你不改变密钥和随机数,加密就永远不会改变。这就像一对一的加密。只要确保没有人知道密钥和随机数,因为一切都会变得毫无用处。不要忘记NONCE 的长度应该是24 个字符,不能多也不能少;并且KEY 的长度应该是32 个字符,不能多也不能少。

也可以使用常规钠随机加密(用于保存电子邮件、用户名等),无论每次随机数和密钥都是随机的。但这不是问题。无论如何,我建议不要将其设为静态,因为它的设计不是静态的。

好吧,我希望你已经解决了这个问题,我希望这会有所帮助(即使我真的这么认为,但事实并非如此)。