如何解密crypt("name")
你不能。来自文档:
注意:没有解密函数,因为
crypt()
使用单向算法。
阅读文档有帮助;)
crypt是单向散列,你不能解密它。
如果你想将它与另一个字符串进行比较,你也可以对它进行加密,然后比较两个加密的字符串
crypt -单向字符串散列
使用双向散列
try with McRypt
教程由于crypt()产生散列,因此无法进行解密。如果您需要猜测原始数据("名称"),您可以使用暴力破解算法和大型字典的组合。
我找到了一个mcrypt的例子,并创建了两个函数,用于文本或二进制文件:
function MyDecrypt($input,$key){
/* Open module, and create IV */
$td = mcrypt_module_open('des', '', 'ecb', '');
$key = substr($key, 0, mcrypt_enc_get_key_size($td));
$iv_size = mcrypt_enc_get_iv_size($td);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
/* Initialize encryption handle */
if (mcrypt_generic_init($td, $key, $iv) != -1) {
/* 2 Reinitialize buffers for decryption */
mcrypt_generic_init($td, $key, $iv);
$p_t = mdecrypt_generic($td, $input);
return $p_t;
/* 3 Clean up */
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
}
} // end function Decrypt()
function MyCrypt($input, $key){
/* Open module, and create IV */
$td = mcrypt_module_open('des', '', 'ecb', '');
$key = substr($key, 0, mcrypt_enc_get_key_size($td));
$iv_size = mcrypt_enc_get_iv_size($td);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
/* Initialize encryption handle */
if (mcrypt_generic_init($td, $key, $iv) != -1) {
/* 1 Encrypt data */
$c_t = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
return $c_t;
/* 3 Clean up */
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
}
}
例如:Crypt一个字符串:
$original_text = "Hello world !";
$password = "abc123";
echo '<p>Original_text: '.$original_text.'</p>';
$crypted_text = MyCrypt($original_text,$password);
echo '<p>Crypted_text: '.$crypted_text.'</p>';
$decrypted_text= MyDecrypt($crypted_text,$password);
echo '<p>Decrypted_text: '.$decrypted_text.'</p>';
echo '<p>And if I try with a wrong password?</p>';
$wrong_decrypted_text= MyDecrypt($crypted_text,"wrong_pw");
echo '<p>Decrypted with wrong password: '.$wrong_decrypted_text.'</p>';
希望对大家有所帮助
你不能真正解密它,因为有(无限)许多字符串这样的crypt($input) == crypt("name")
-但你可以,通过暴力试错,找到一些这些字符串。
如果您知道或怀疑原始字符串是一个短字典单词,并且您找到了一个产生相同输出的短字典单词,那么您很可能已经"解密"了原始字符串。
md5和许多较弱的哈希函数通常都以这种方式被攻击。
<?php
$hashed_password = crypt('mypassword'); // let the salt be automatically generated
/* You should pass the entire results of crypt() as the salt for comparing a
password, to avoid problems when different hashing algorithms are used. (As
it says above, standard DES-based password hashing uses a 2-character salt,
but MD5-based hashing uses 12.) */
if (hash_equals($hashed_password, crypt($user_input, $hashed_password))) {
echo "Password verified!";
}
?>