php 如何解密 crypt("name")

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5620695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:00:50  来源:igfitidea点击:

how to decrypt the crypt("name")

php

提问by user36

how to decrypt the crypt("name")

如何解密 crypt("name")

回答by Felix Kling

You can't. From the documentation:

你不能。从文档

Note:There is no decrypt function, since crypt()uses a one-way algorithm.

注意:没有解密功能,因为crypt()使用单向算法。

Reading documentation helps ;)

阅读文档有帮助;)

回答by Twelve47

crypt is one way hashing, you can't decrypt it.

crypt 是一种散列方式,您无法解密它。

If you want to compare it against another string you could crypt that too and then compare the two crypted strings.

如果您想将它与另一个字符串进行比较,您也可以将其加密,然后比较两个加密的字符串。

回答by Headshota

crypt — One-way string hashing

crypt — 单向字符串哈希

回答by diEcho

use two way hashing

使用两种方式散列

try with mcrypt

尝试使用mcrypt

tutorial

教程

回答by intellion

Since crypt() produces a hash decrypting is not possible. If you need to guess the original data ("name") you can use a combination of a brute force algorithm and a huge dictionary.

由于 crypt() 产生散列解密是不可能的。如果您需要猜测原始数据(“名称”),您可以结合使用蛮力算法和庞大的字典。

回答by madde74

I have find an example for mcrypt and create the two functions, for text or for binary files:

我找到了一个 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);
    }
}

For Example Crypt a string :

例如 Crypt a string :

    $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>';

I hope helpful

我希望有帮助

回答by tucuxi

You can't truly decrypt it, because there are (infinitely) many strings such that crypt($input) == crypt("name")-- but you can, via brute-force trial-and-error, find someof those strings.

你不能真正解密它,因为有(无限)很多这样的字符串crypt($input) == crypt("name")——但是你可以通过蛮力试错法找到其中的一些字符串。

If you know or suspect that the original string is a short dictionary word, and you find a short dictionary word that produces the same output, chances are you have "decrypted" the original string.

如果您知道或怀疑原始字符串是一个简短的字典单词,并且您发现一个生成相同输出的简短字典单词,那么您可能已经“解密”了原始字符串。

md5and many weaker hash functions are attacked in this way routinely.

md5和许多较弱的哈希函数经常以这种方式受到攻击。

回答by Osama Hussain

<?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!";
}

?>