我需要通过要在我的页面上显示的查询字符串传递一个值。该值必须加密,因为它包含一些用于查看页面的用户的 PII。并且为了使用户可读,它需要在显示时能够被解密。
openssl_encrypt
我正在使用 PHP ,到目前为止的研究已经引导我openssl_decrypt
使用这两个代码资源:
- https://bhoover.com/using-php-openssl_encrypt-openssl_decrypt-encrypt-decrypt-data/
- https://gist.github.com/joashp/a1ae9cb30fa533f4ad94
我非常喜欢 #1,因为iv
它实际上附加到返回的加密字符串的方式。这使我们不必将 an 存储iv
为常量,并且能够在每次调用函数时生成一个新的。这对我来说似乎比每次都使用相同key
的更安全。真的吗?如果是这样,除了痛苦的显而易见的事情之外,是否有任何我应该知道的原因?. 我不喜欢的事情是我认为将and与一个字符/字符串(在这种情况下)连接起来,可以在其他潜在的密码文本中自然发生或iv
iv
key
::
iv
成了问题。使用这种方法,在尝试加密 7000 多个电子邮件地址时,其中超过一半的���6CTΣ
人在解码的字符串中出现了这些奇怪的字符,其中包括),从而破坏了它。
#2很棒,因为它有效!我还没有找到可以破坏它的字符串……尤其是在我的电子邮件列表中。但是如上所述,这需要。和始终是相同的值并存储在某处的变量中iv
。key
这似乎是一个很小的维护问题,但更多的是安全问题。
所以我做了更多的阅读/思考并想出了这个工作示例- 这是代码:
<?php
// generate key with base64_encode(openssl_random_pseudo_bytes(32);
// and save it in a constant.
define('ENCRYPT_KEY_1', 'CuH8WPfXzMj0xRWybHjssWJ+IhTDqL5w0OD9+zXFloA=');
function encrypt_decrypt($action, $string) {
$output = false;
$encrypt_method = "AES-256-CBC";
$key = ENCRYPT_KEY_1;
$ivLen = openssl_cipher_iv_length($encrypt_method);
/**
* the key was generated with 32 pseudo-bytes and then base64Encod()-ed.
* Not sure of the reason for encoding - just decoding in case it's necessary.
*
* Thoughts?
* **/
$key = base64_decode($key);
if ( $action == 'encrypt' ) {
/**
* "AES-256-CBC" expects a 16-byte string - create an 8-byte string to be
* converted to a 16-byte hex before being used as the initialization vector
* TLDR" in order to end up with 16-bytes to feed to openssl_random_pseudo_bytes,
* divide initial length in half as the hex value will double it
* */
$iv = openssl_random_pseudo_bytes($ivLen/2);
$iv = bin2hex($iv);
$tmp_data_str_in = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
$output = base64_encode($tmp_data_str_in . $iv);
} else if( $action == 'decrypt' ) {
$data_str_in = base64_decode($string);
// This time, rather than generating one, we get the iv (converted to hex)
// by grabbing the last 16 characters of the concatenation of the 2 that happened
// during encryption.
$iv = substr($data_str_in, -$ivLen);
// And now we just grab everything BUT the last 16 characters. We'll
// run openssl_decrypt and return a copy of the original input
$encrypted_txt_str = substr($data_str_in, 0, strlen($data_str_in)-$ivLen);
// Notice we are returning the encrypted value of a string consisting of
// encoded versions of the input text and a random `IV` - we'll grab the `IV`
// from it later in order to decrypt later.
$output = openssl_decrypt($encrypted_txt_str, $encrypt_method, $key, 0, $iv);
}
return $output;
}
$plain_txt = "memyselfandi@i.me";
echo "Plain Text = " .$plain_txt. "\n";
$encrypted_txt = encrypt_decrypt('encrypt', $plain_txt);
echo "Encrypted Text = " .$encrypted_txt. "\n";
$decrypted_txt = encrypt_decrypt('decrypt', $encrypted_txt);
echo "Decrypted Text = " .$decrypted_txt. "\n";
if ( $plain_txt === $decrypted_txt ) echo "SUCCESS";
else echo "FAILED";
echo "\n";
?>
所以我想我的主要问题是:
- 我是否认为使用
iv
在执行函数时生成的动态的解决方案比iv
提前定义好静态并用于每次加密更安全?如果没有,我错过了什么? - (可能成功的)攻击有哪些开口,因此任何/所有这些方法都会暴露?如何修复或修改它们以降低风险
- 这些方法中的任何一种(希望是我放在一起的方法)是否可以在显示用户 PII 的网站上的生产环境中使用——本质上没有银行业务或超级绝密——并允许他进行更新?它在 PHP 中使用,看起来有点像:
print "<li>Email: " . encrypt_decrypt('decrypt', my_sanitize_fxn($_GET['ue']) . "</li">;
关于#3的快速说明:
我猜最好加密一些 ISN'T PII(例如用户在数据库中的唯一 ID)以通过查询字符串发送,然后解密该值并使用它来查找他的电子邮件地址数据库查询。尽管我最终可能会以这种方式结束这一切,但让我们说目前有一些因素在起作用(对此进行解释会使这个问题远离主题),这甚至阻止了它成为一个远程切实可行的选项。
我认为了解我在这里得到的东西将很好地延续到最终的解决方案中。除了对我自始至终提出的一些正式问题的回答外,我很想听听任何做得特别差或做得好的事情,或者只是一般性的评论。
提前感谢您愿意分享的任何智慧。