SEU*_*SEU 7 php forms encryption html-parsing query-string
我打算为我的表单使用GET,但是想要加密查询字符串中的值,以便用户无法更改它.(不是为了安全,而是为了数据隐藏目的)
我遇到了几个解释加密的网站,但是一旦用户按下提交按钮,我不清楚如何实现它.例如:http://myscriptlibrary.wordpress.com/2010/04/14/how-to-encrypt-query-string-in-php/
有没有一个例子可以证明这一点?
谢谢.
Gle*_*ton 14
根据我对您提供的链接的理解.您想要加密GET变量,或者至少对它们进行模糊处理.
这可以做到的最好和最简单的方法是使用base64_decode/encode
例如对字符串进行编码,你会做类似的事情:
$link = "http://www.example.com/?item=".urlencode(base64_encode("user-data"));
Run Code Online (Sandbox Code Playgroud)
$link会看起来像http://www.example.com/?item=rklgEwkelnf%3D%3D,但要将看似乱码(base64ed)的文本翻译成可用的东西你会使用:
foreach($_GET as $loc=>$item)
$_GET[$loc] = base64_decode(urldecode($item));
Run Code Online (Sandbox Code Playgroud)
然后您可以$_GET像往常一样自由使用变量.
以下解决方案很容易实现,并且足够强大,除非您处理非常敏感的数据,如信用卡信息或NASA算法......
当您通过发送参数时.GET - 添加哈希值,例如:
$parameter = "abc"; //The parameter which you'll pass as a GET parameter
$salt = "cV0puOlx";
$hashed = md5($salt.$parameter);//A hash that you'll pass as well
header("Location: http://www.yourdomain.com?param=$parameter&hash=$hash");
Run Code Online (Sandbox Code Playgroud)
然后,当您读取参数时,检查哈希值是否有效:
$parameter = $_GET['param'];
$hash = $_GET['hash'];
$salt = "cV0puOlx";
$hashed = md5($salt.$parameter);
//now you check:
if ($hash === $hashed){
//everything's fine - continue processing
}
else{
// ERROR - the user tried to tamper with your parameter
// show error-message and bail-out
}
Run Code Online (Sandbox Code Playgroud)
这里接受的答案并没有提供任何真正的保护.您可以将编码参数放入在线base64_decode中,并显示值,就像您刚刚直接传递它们一样!
另一个答案使用$ hash作为传递值,但该值尚未定义为$ hashed.
1-加密你的var
2-确保使用base64 MIME正确编码。
3-做你想做的事(例如:存储在数据库中以便以后解密,传递给GET等)
4-安全解码base64您的var。
5-解密您的var
我实现了一个可以完成这项工作的课程。(安全性和数据隐藏)使用带有ES-256模式CBC的openssl方法来保护crypt(不要忘记初始化向量)
class Encryption{
/**
*
* Retourne la chaîne de caracère encodéé en MIME base64
* ----------------------------------------------------
* @param string
* @return string
*
**/
public static function safe_b64encode($string='') {
$data = base64_encode($string);
$data = str_replace(['+','/','='],['-','_',''],$data);
return $data;
}
/**
*
* Retourne la chaîne de caracère MIME base64 décodée
* -------------------------------------------------
* @param string
* @return string
*
**/
public static function safe_b64decode($string='') {
$data = str_replace(['-','_'],['+','/'],$string);
$mod4 = strlen($data) % 4;
if ($mod4) {
$data .= substr('====', $mod4);
}
return base64_decode($data);
}
/**
*
* Crypte une chaîne de caractères avec un algorithme de cryptage aes-256 mode cbc
* Le crypatage s'effectue avec une clé définie dans le fichier core.php
* ------------------------------------------------------------------------------------------
* @param string
* @return string
*
**/
public static function encode($value=false){
if(!$value) return false;
$iv_size = openssl_cipher_iv_length('aes-256-cbc');
$iv = openssl_random_pseudo_bytes($iv_size);
$crypttext = openssl_encrypt($value, 'aes-256-cbc', 'your security cipherSeed', OPENSSL_RAW_DATA, $iv);
return self::safe_b64encode($iv.$crypttext);
}
/**
*
* Decrypte une chaîne de caractères
* ---------------------------------
* @param string
* @return string
*
**/
public static function decode($value=false){
if(!$value) return false;
$crypttext = self::safe_b64decode($value);
$iv_size = openssl_cipher_iv_length('aes-256-cbc');
$iv = substr($crypttext, 0, $iv_size);
$crypttext = substr($crypttext, $iv_size);
if(!$crypttext) return false;
$decrypttext = openssl_decrypt($crypttext, 'aes-256-cbc', 'your security cipherSeed', OPENSSL_RAW_DATA, $iv);
return rtrim($decrypttext);
}
}
Run Code Online (Sandbox Code Playgroud)
范例:
$pass_get = 'hello';
$base64_crypt = Encryption::encode($pass_get); // get base64 of crypt data
Run Code Online (Sandbox Code Playgroud)
//例如,稍后传入$ _GET
<a href="https://toto.com?v=<?php echo $base64_crypt;?>" >Other page</a>
Run Code Online (Sandbox Code Playgroud)
//在另一个页面中,恢复您的var
$my_get_crypt_var = $_GET['v'];
Encryption::decode($my_get_crypt_var); // return 'hello' or false in case the string to be decrypted is invalid.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29232 次 |
| 最近记录: |