加密字符串写入文件,然后在 PHP 中解密文件
Encrypting strings write to file and then decrypt file in PHP
我在使用 "openssl_encrypt" 加密包含 HTML 的文本字符串时遇到一个小问题,将该字符串写入文件,然后在 单独的页面,我正在使用"openssl_decrypt"解密整个文件。我确保使用相同的加密密钥、相同的方法和相同的 iv。我想这是我作为加密新手看不到的东西。预先感谢您的帮助!
下面是一些示例代码:
//An example of the string
$string = "<div class='mod'><div><span class='datetimestamp'>On 06/28/2016 at 04:32:09 PM, ** modified a record with id of \"5\" in the \"results\" table:</span><br><span class='record-label'>Prev Record:</span>jobnumber='none', dropdate='07/06/2016', eventdate='07/16/2016', dealership='ABC Nissan', pieces='3700', datatype='DB', letter='t'";
//The encryption
$encrypt = openssl_encrypt($string, 'AES-256-XTS', '93jkak3rzp72', 1, '45gh354687ls0349');
$file = fopen("logs/2016-06-28.log", 'a');
fwrite($file, $encrypt);
fclose($file);
//The decryption - DONE IN A SEPARATE PAGE
$file = @fopen("logs/2016-06-28.log", "r");
if ($file) {
while (($data = fgets($file)) !== false) {
$decrypt .= openssl_decrypt($data, 'AES-256-XTS', '93jkak3rzp72', 1, '45gh354687ls0349');
}
}
我不会使用 fgets()
,因为它一次只能从一个文件中获取一行,而且您不能一次拆分一个加密的字符串并解密单个片段。
您可以使用 fgets()
,但您需要读入所有内容并将其存储在一个变量中,然后在您对所有内容进行解密之后。
或者您可以简单地使用 file_get_contents()
之类的东西来获取整个文件的内容,然后解密。
也许问题是您正在尝试附加额外的加密数据,但由于多种原因,这通常不会起作用,一个主要原因是 AES 是基于块的,很可能会有填充。许多模式使用某种形式的链接,这在附加加密数据时也会失败。
您正在以附加模式打开要写入的文件,这不是您所需要的,而是使用写入 w
模式。这导致每次加密都被附加到以前的数据上,这也是第一个连接起作用但随后多次起作用的原因。如果您在每次加密后检查文件长度,就会清楚发生了什么。
您需要使用:
$file = fopen("logs/2016-06-28.log", 'w');
'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() has no effect, writes are always appended.
我在使用 "openssl_encrypt" 加密包含 HTML 的文本字符串时遇到一个小问题,将该字符串写入文件,然后在 单独的页面,我正在使用"openssl_decrypt"解密整个文件。我确保使用相同的加密密钥、相同的方法和相同的 iv。我想这是我作为加密新手看不到的东西。预先感谢您的帮助!
下面是一些示例代码:
//An example of the string
$string = "<div class='mod'><div><span class='datetimestamp'>On 06/28/2016 at 04:32:09 PM, ** modified a record with id of \"5\" in the \"results\" table:</span><br><span class='record-label'>Prev Record:</span>jobnumber='none', dropdate='07/06/2016', eventdate='07/16/2016', dealership='ABC Nissan', pieces='3700', datatype='DB', letter='t'";
//The encryption
$encrypt = openssl_encrypt($string, 'AES-256-XTS', '93jkak3rzp72', 1, '45gh354687ls0349');
$file = fopen("logs/2016-06-28.log", 'a');
fwrite($file, $encrypt);
fclose($file);
//The decryption - DONE IN A SEPARATE PAGE
$file = @fopen("logs/2016-06-28.log", "r");
if ($file) {
while (($data = fgets($file)) !== false) {
$decrypt .= openssl_decrypt($data, 'AES-256-XTS', '93jkak3rzp72', 1, '45gh354687ls0349');
}
}
我不会使用 fgets()
,因为它一次只能从一个文件中获取一行,而且您不能一次拆分一个加密的字符串并解密单个片段。
您可以使用 fgets()
,但您需要读入所有内容并将其存储在一个变量中,然后在您对所有内容进行解密之后。
或者您可以简单地使用 file_get_contents()
之类的东西来获取整个文件的内容,然后解密。
也许问题是您正在尝试附加额外的加密数据,但由于多种原因,这通常不会起作用,一个主要原因是 AES 是基于块的,很可能会有填充。许多模式使用某种形式的链接,这在附加加密数据时也会失败。
您正在以附加模式打开要写入的文件,这不是您所需要的,而是使用写入 w
模式。这导致每次加密都被附加到以前的数据上,这也是第一个连接起作用但随后多次起作用的原因。如果您在每次加密后检查文件长度,就会清楚发生了什么。
您需要使用:
$file = fopen("logs/2016-06-28.log", 'w');
'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() has no effect, writes are always appended.