我正在使用 PHPmcrypt
将值加密和解密到 Windows 7 中的平面文件。
如果我在没有其他信息的情况下直接写入该值并将其从文件中解密,则该算法可以正常工作。如果我尝试将其编码为键/值password=xxxxxx
对explode()
(
我trim()
用来切断任何空格(看起来它可能带有 EOL 字符),并且也尝试过手动解析键和值。
似乎没有任何工作。有没有其他人看到过这个问题,如果有,你是如何解决的?
加密/解密代码:
function encrypt ($strToEncrypt) {
global $td, $iv, $key;
/* Intialize encryption */
mcrypt_generic_init($td, $key, $iv);
/* Encrypt data */
$encrypted = mcrypt_generic($td,$strToEncrypt);
echo "encrypted = " . $encrypted . "<BR>";
return $encrypted;
}
function decrypt($strToDecrypt) {
global $td, $iv, $key;
/* Initialize encryption */
mcrypt_generic_init($td, $key, $iv);
/* Decrypt data */
$decrypted = mdecrypt_generic($td, $strToDecrypt);
return $decrypted;
}
?>
文件操作代码:
include 'libEncryption.php';
$tempStr ="";
$tempStr2="";
try {
$nextPage = $_REQUEST["NEXTPAGE"];
switch ($nextPage)
{
case "ENCRYPT":
echo 'String to encrypt = ' . $_POST["txtSeedStr"] ."<BR>";
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$file = fopen("C:\\temp\\EncryptTest.txt", "wb") or exit("Unable to open file!");
} else {
$file = fopen("//home/prem03/EncryptTest.txt", "w") or exit("Unable to open file!");
}
fwrite($file, "username=web_app\npassword=");
$tempStr = encrypt($_POST["txtSeedStr"]);
fwrite($file, $tempStr, strlen($tempStr));
fwrite($file, "\n");
fwrite($file, "DBNAME=DEV11");
fclose($file);
break;
case "DECRYPT":
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$file = fopen("C:\\temp\\EncryptTest.txt", "rb") or exit("Unable to open file!");
} else {
$file = fopen("//home/prem03/EncryptTest.txt", "r") or exit("Unable to open file!");
}
if ($file) {
while (!feof($file)) {
$str1=fgets($file);
echo "str1 = " . $str1 . "<br>";
list($key,$value) = explode("=",$str1);
$var1=strlen(trim($value));
echo "key = ".$key.' value = '.trim($value) . ' ' . $var1 . "<br />";
if ($key == "password") {
$var2 = trim($value);
$tempStr2 = decrypt($var2);
echo 'tempStr2 = ' . $tempStr2 . "<BR>";
}
}
}
fclose($file);
break;
default:
break;
}
} catch (Exception $ex) {
echo $ex->getMessage();
}
?>