php-openssl_encrypt返回空字符串
作者:互联网
我正在尝试在服务器上创建UUID,由于某种原因,openssl_enrypt正在初始化一个空字符串.如果返回的是false,这将是另一种对话,但不是,返回的是字符串,只是一个空字符串.
这是我用来构建所需的加密字符串的所有代码:
$key = hash_hmac("sha512", "You can decrypt this all day long, won't get you closer to the truth", "myKey");
$iv = openssl_random_pseudo_bytes(16);
$adminVal = filter_var($userData['is_admin'], FILTER_VALIDATE_BOOLEAN);
$userName = $userData["name"];
$dataEncrypt = $adminVal.$userName;
$encrypted = openssl_encrypt($dataEncrypt, "AES-256-XTS", $key, 0, $iv);
我有var_dumped $key,$iv和$dataEncrypt,它们都返回正确的值.
解决方法:
@Fred-ii- You’re absolutely right, thank you! I can’t believe I didn’t see that…I would love to give you the answer!
按照要求:
这似乎是您使用AES-256-XTS的方法.例如,如果将其更改为aes128或AES-128-CBC,您将看到结果.请查阅文档http://php.net/manual/en/function.openssl-encrypt.php-手册中未列出AES-256-XTS.
编辑:我从以前的编辑中删除了错误报告,我将对此进行进一步调查以弄清为什么最终产生空结果.
由于(密码)方法在运行var_dump(openssl_get_cipher_methods());时(实际上)确实存在,因此它也可能取决于openssl;.从错误报告中留下的评论中提取/获取.
如果/一旦我可以得到结果/希望解释为什么OP和我自己的测试为空并从OP获得更多信息,我将更新答案.
编辑:
经过更多研究后,我偶然发现this link,并且该代码使用“ AES-256-XTS”密码方法“开箱即用”(请注意;请参阅结尾处源脚本中的注释行注释).
在查看该代码并将其与OP进行比较时,我注意到需要加密的是数据/消息.
>消息被散列为“作为关键字”,从而使字符串为空.仅对密钥进行哈希处理,而不对密钥和数据/消息进行哈希处理,因为您试图使用消息和密钥“作为密钥”对现有的哈希方法进行加密.
结果脚本如下:
旁注:下面的注释行也适用于它们各自的部分;只是不要同时使用两者.
$plaintext = 'The secret message in plain text';
$password = '3sc3RLrpd17';
$key = substr(hash('sha256', $password, true), 0, 32);
$iv = openssl_random_pseudo_bytes(16);
$method = "aes-256-xts";
$userName = "JOHN";
$encrypted = base64_encode(openssl_encrypt($plaintext, $method, $key, OPENSSL_RAW_DATA, $iv));
// $encrypted = openssl_encrypt($plaintext, $method, $key, OPENSSL_RAW_DATA, $iv);
echo "<br>";
var_dump($iv);
echo "<br>";
var_dump($userName);
echo "<br>";
var_dump($encrypted);
echo "<hr>";
$decrypted = openssl_decrypt(base64_decode($encrypted), $method, $key, OPENSSL_RAW_DATA, $iv);
// $decrypted = openssl_decrypt($encrypted, $method, $key, OPENSSL_RAW_DATA, $iv);
echo 'decrypted to: ' . $decrypted . "\n\n";
特别说明:我在选择Jay Blanchard’s大脑和测试方面也有所帮助;两个脑袋通常比一个脑袋好,因此杰伊为此也值得赞扬.
注意:多个来源表明它(“ AES-256-XTS”)用于文件系统/磁盘加密.您可能会说“ AES-256-XTS和AES-128-XTS”方法真正用于文件系统加密,因此不适合于文本.
从this link提取的源代码:
<?php
$plaintext = 'My secret message 1234';
$password = '3sc3RLrpd17';
$method = 'aes-256-cbc'; // I replaced aes-256-cbc with aes-256-xts during testing
// Must be exact 32 chars (256 bit)
$password = substr(hash('sha256', $password, true), 0, 32);
echo "Password:" . $password . "\n";
// IV must be exact 16 chars (128 bit)
$iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);
// av3DYGLkwBsErphcyYp+imUW4QKs19hUnFyyYcXwURU=
$encrypted = base64_encode(openssl_encrypt($plaintext, $method, $password, OPENSSL_RAW_DATA, $iv));
// My secret message 1234
$decrypted = openssl_decrypt(base64_decode($encrypted), $method, $password, OPENSSL_RAW_DATA, $iv);
echo 'plaintext=' . $plaintext . "\n";
echo 'cipher=' . $method . "\n";
echo 'encrypted to: ' . $encrypted . "\n";
echo 'decrypted to: ' . $decrypted . "\n\n";
标签:cryptography,sha512,php-openssl,hash,php 来源: https://codeday.me/bug/20191025/1929950.html