通过php中的表单检索用户的值

时间:2014-02-15 08:09:44

标签: php mysql forms encryption

大家好我已经编写了一个脚本,它返回一个字符串的加密值.. php代码是

file.php

function encrypt_decrypt($action, $string) {
   $output = false;

   $key = 'My strong random secret key';





$iv = md5(md5($key));



if( $action == 'encrypt' ) {
       $output = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, $iv);


     $output = base64_encode($output);



}
   else if( $action == 'decrypt' ){


$output = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, $iv);

   $output = rtrim($output, "");



 }


return $output;

}

$plain_txt = "This is my plain text";

$encrypted_txt = encrypt_decrypt('encrypt', $plain_txt);

echo "Encrypted Text = $encrypted_txt\n";

echo "<br />";

    $decrypted_txt = encrypt_decrypt('decrypt', $encrypted_txt);

echo "Decrypted Text = $decrypted_txt\n";

此代码返回“这是我的纯文本”

的加密文本

我的form.html文件是..

<form action="file.php" method="get">
  Enter the word to encrypt : <input type="text" name="fname"><br>
 Encrypted text is  : <input type="text" name="lname"><br>
  <input type="submit" value="Submit">
</form>

我需要的是当用户输入他想要的字符串时,他必须得到他在字段中插入的字符串的相应加密文本..我知道这可以用sql完成..请帮我给我一些想法..因为我是php和mysql开发的新手

任何帮助将不胜感激..谢谢.. :)

1 个答案:

答案 0 :(得分:-1)

把你的PHP代码放在这个

    if(isset($_REQUEST['fname']))
    {

    $plain_txt = $_REQUEST['fname'];

    $encrypted_txt = encrypt_decrypt('encrypt', $plain_txt);

    echo "Encrypted Text = $encrypted_txt\n";

    echo "<br />";

    $decrypted_txt = encrypt_decrypt('decrypt', $encrypted_txt);

    echo "Decrypted Text = $decrypted_txt\n";

    }