我需要一个用密码加密变量的PHP脚本。我不是指像md5($ var)这样的哈希;或sha1($ var);
我需要一个可以制作的脚本(例如)md5($ var);哈希也可以从md5($ var)获得;有用的字符串。
期待如
$password = "SomePassword";
$data = "TheVerySecretString";
$encrypted = TheEncyptionFunctionINeed($password, $data); // Output some useless strings
$decrypted = TheDecryptionFunctionINeed($password, $data); // Output: "TheVerySecretString"
答案 0 :(得分:3)
几年后,我想把它打开,但我觉得这很重要 因为它在顶级搜索排名中......
PHP 5.3引入了一种非常容易实现的新加密方法 使用
这是openssl_encrypt和openssl_decrypt ......没有详细记录 在这里,所以这是一个简单的例子..
$textToEncrypt = "My super secret information.";
$encryptionMethod = "AES-256-CBC"; // AES is used by the U.S. gov't to encrypt top secret documents.
$secretHash = "25c6c7ff35b9979b151f2136cd13b0ff";
//To encrypt
$encryptedMessage = openssl_encrypt($textToEncrypt, $encryptionMethod, $secretHash);
//To Decrypt
$decryptedMessage = openssl_decrypt($encryptedMessage, $encryptionMethod, $secretHash);
//Result
echo "Encrypted: $encryptedMessage <br>Decrypted: $decryptedMessage";
答案 1 :(得分:0)
以下是2个功能:
function encryptData($value){ $key = "top secret key"; $text = $value; $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv); return $crypttext; } function decryptData($value){ $key = "top secret key"; $crypttext = $value; $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); $decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $crypttext, MCRYPT_MODE_ECB, $iv); return trim($decrypttext); } ?>
查看手册中的功能:mcrypt_encrypt和mcrypt_decrypt