由于此帖似乎得到了相当多的关注,我想告诉您,解决方案最终是在enctype
声明中提供正确的<FORM>
(内容类型)参数。您必须将值设置为multipart/form-data
,以防止使用application/x-www-form-urlencoded
的默认enctype进行编码。 W3.org上Forms in HTML Documents以下的一小段摘录:
内容类型 “application / x-www-form-urlencoded”是 发送大的效率低下 二进制数据或文本的数量 包含非ASCII字符。该 内容类型“multipart / form-data” 应该用于提交表格 包含文件,非ASCII数据, 和二进制数据。
这是正确的FORM声明:
<FORM method="POST" action="/path/to/file/" name="encryptedForm" enctype="multipart/form-data">
我正在开发一个表单垃圾邮件防护类,它基本上使用mcrypt将表单字段名称替换为加密值。这个问题是mcrypt加密不仅限于使表单字段无效的字母数字字符。 考虑到下面的代码,你能想出为什么我在解密已经加密的数组的值时遇到问题的原因吗?
/**
* Two way encryption function to encrypt/decrypt keys with
* the DES encryption algorithm.
*/
public static function encryption($text, $encrypt = true)
{
$encrypted_data = '';
$td = mcrypt_module_open('des', '', 'ecb', '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
if (mcrypt_generic_init($td, substr(self::$randomizer, 16, 8), $iv) != -1) {
if ($encrypt) {
// attempt to sanitize encryption for use as a form element name
$encrypted_data = mcrypt_generic($td, $text);
$encrypted_data = base64_encode($encrypted_data);
$encrypted_data = 'i' . strtr($encrypted_data, '+/=', '-_.');
self::$encrypted[] = $encrypted_data;
} else {
// reverse form element name sanitization and decrypt
$text = substr($text, 1);
$text = strtr($text, '-_.', '+/=');
$text = base64_decode($text);
$encrypted_data = mdecrypt_generic($td, $text);
}
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
}
return $encrypted_data;
}
我稍后使用以下方法调用设置隐藏表单元素的值:
base64_encode(serialize(self::$encrypted))
本质上,隐藏字段包含所有表单字段的数组,这些字段使用其加密值进行加密。这是我知道哪些字段需要在后端解密。在表单提交时,使用以下代码在后端解析此字段:
// load the mapping entry
$encrypted_fields = $input->post('encrypted', '');
if (empty($encrypted_fields)) {
throw new AppException('The encrypted form field was empty.');
}
// decompress array of encrypted fields
$encrypted_fields = @unserialize(base64_decode($encrypted_fields));
if ($encrypted_fields === false) {
throw new AppException('The encrypted form field was not valid.');
}
// get the mapping of encrypted keys to key
$data = array();
foreach ($_POST as $key => $val) {
// if the key is encrypted, add to data array decrypted
if (in_array($key, $encrypted_fields)) {
$decrypted = self::encryption($key, false);
$data[$decrypted] = $val;
unset($_POST[$key]);
} else {
$data[$key] = $val;
}
}
// merge $_POST array with decrypted key array
$_POST += $data;
我尝试解密加密的表单字段键失败。它只是在$_POST
数组中创建一个新的乱码密钥。我的猜测是,base64_encoding
或serialization
正在从$encrypted_data
中删除字符。 有人可以验证这是否是罪魁祸首以及是否有任何替代方法来编码表单键?
答案 0 :(得分:1)
所以我接受了你的代码,并稍微修改了一下,以便我可以删除帖子请求的元素,你的功能似乎工作正常。如果您使用我发布的代码并使用它创建一个脚本,它应该在cli中运行,您将看到它正确地加密/解密字段。这必须意味着post请求是加密/序列化/编码数据的一些混乱。如果使用框架,我会更多地了解它如何处理post数组,因为它可能会改变你的键/值,导致它们不匹配。您发布的代码似乎很好。
<?php
/**
* Two way encryption function to encrypt/decrypt keys with
* the DES encryption algorithm.
*/
function encryption($text, $encrypt = true, &$encryptedFields = array())
{
$encrypted_data = '';
$td = mcrypt_module_open('des', '', 'ecb', '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
if (mcrypt_generic_init($td, substr('sdf234d45)()*5gf512/?>:LPIJ*&U%&^%NBVFYUT^5hfhgvkjtIUUYRYT', 16, 8), $iv) != -1) {
if ($encrypt) {
// attempt to sanitize encryption for use as a form element name
$encrypted_data = mcrypt_generic($td, $text);
$encrypted_data = base64_encode($encrypted_data);
$encrypted_data = 'i' . strtr($encrypted_data, '+/=', '-_.');
//self::$encrypted[] = $encrypted_data;
$encryptedFields[] = $encrypted_data;
} else {
// reverse form element name sanitization and decrypt
$text = substr($text, 1);
$text = strtr($text, '-_.', '+/=');
$text = base64_decode($text);
$encrypted_data = mdecrypt_generic($td, $text);
}
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
}
return $encrypted_data;
}
$encryptedFields = array();
// encrypt some form fields
encryption('firstname', true, $encryptedFields);
encryption('lastname', true, $encryptedFields);
encryption('email_fields', true, $encryptedFields);
echo "Encrypted field names:\n";
print_r($encryptedFields);
// create a usable string of the encrypted form fields
$hiddenFieldStr = base64_encode(serialize($encryptedFields));
echo "\n\nFull string for hidden field: \n";
echo $hiddenFieldStr . "\n\n";
$encPostFields = unserialize(base64_decode($hiddenFieldStr));
echo "\n\nDecrypted field names:\n";
foreach($encPostFields as $field)
{
echo encryption($field, false)."\n";
}
?>