php 加密和解密md5
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15194663/
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
encrypt and decrypt md5
提问by Tomer
I am using code $enrypt=md5($pass)and inserting $encryptto database. I want to find out a way to decrypt them. I tried using a decrypting software but it says the hash should be of exactly 16 bytes. is there any way to decrypt it or to make it a 16 byte md5 hash?
我正在使用代码$enrypt=md5($pass)并插入$encrypt到数据库中。我想找到一种方法来解密它们。我尝试使用解密软件,但它说散列应该正好是 16 个字节。有什么方法可以解密它或使其成为 16 字节的 md5 哈希?
My hash looks like this: c4ca4238a0b923820dcc
我的哈希看起来像这样: c4ca4238a0b923820dcc
回答by BIT CHEETAH
As already stated, you cannot decrypt MD5 without attempting something like brute force hacking which is extremely resource intensive, not practical, and unethical.
如前所述,如果不尝试诸如暴力破解之类的方法,就无法解密 MD5,这种方法非常耗费资源,不实用且不道德。
However you could use something like this to encrypt / decrypt passwords/etc safely:
但是你可以使用这样的东西来安全地加密/解密密码/等:
$input = "SmackFactory";
$encrypted = encryptIt( $input );
$decrypted = decryptIt( $encrypted );
echo $encrypted . '<br />' . $decrypted;
function encryptIt( $q ) {
$cryptKey = 'qJB0rGtIn5UB1xG03efyCp';
$qEncoded = base64_encode( mcrypt_encrypt( MCRYPT_RIJNDAEL_256, md5( $cryptKey ), $q, MCRYPT_MODE_CBC, md5( md5( $cryptKey ) ) ) );
return( $qEncoded );
}
function decryptIt( $q ) {
$cryptKey = 'qJB0rGtIn5UB1xG03efyCp';
$qDecoded = rtrim( mcrypt_decrypt( MCRYPT_RIJNDAEL_256, md5( $cryptKey ), base64_decode( $q ), MCRYPT_MODE_CBC, md5( md5( $cryptKey ) ) ), "/* you can match the exact string with table value*/
if(md5("string to match") == $res["hashstring"])
echo "login correct";
");
return( $qDecoded );
}
Using a encypted method with a salt would be even safer, but this would be a good next step past just using a MD5 hash.
使用带盐的加密方法会更安全,但这将是仅使用 MD5 散列之后的一个很好的下一步。
回答by Niet the Dark Absol
There is no way to decrypt MD5. Well, there is, but no reasonableway to do it. That's kind of the point.
没有办法解密MD5。嗯,有,但没有合理的方法来做到这一点。这就是重点。
To check if someone is entering the correct password, you need to MD5 whatever the user entered, and see if it matches what you have in the database.
要检查是否有人输入了正确的密码,您需要对用户输入的任何内容进行 MD5,并查看它是否与您在数据库中的内容匹配。
回答by Mahesan Rv
$string = 'c4ca4238a0b923820dcc';
$encrypted = \Illuminate\Support\Facades\Crypt::encrypt($string);
$decrypted_string = \Illuminate\Support\Facades\Crypt::decrypt($encrypted);
var_dump($string);
var_dump($encrypted);
var_dump($decrypted_string);
回答by Somnath Muluk
This question is tagged with PHP. But many people are using Laravel framework now. It might help somebody in future. That's why I answering for Laravel. It's more easy to encrypt and decrypt with internal functions.
这个问题是用 PHP 标记的。但是现在很多人都在使用 Laravel 框架。将来可能会对某人有所帮助。这就是我为 Laravel 回答的原因。使用内部函数进行加密和解密更容易。
$password = Input::get('password_from_user');
$hashed = Hash::make($password); // save $hashed value
Note: Be sure to set a 16, 24, or 32 character random string in the key option of the config/app.php file. Otherwise, encrypted values will not be secure.
注意:请务必在 config/app.php 文件的 key 选项中设置一个 16、24 或 32 个字符的随机字符串。否则,加密的值将不安全。
But you should not use encrypt and decrypt for authentication. Rather you should use hash make and check.
但是您不应该使用加密和解密进行身份验证。相反,您应该使用哈希制作和检查。
To store password in database, make hash of password and then save.
要将密码存储在数据库中,请对密码进行散列,然后保存。
// $user is database object
// $inputs is Input from user
if( \Illuminate\Support\Facades\Hash::check( $inputs['password'], $user['password']) == false) {
// Password is not matching
} else {
// Password is matching
}
To verify password, get password stored of account from database
要验证密码,请从数据库中获取存储的帐户密码
crypt('String', 'a$twentytwocharactersalt$');
回答by Mayur S
It's not possible to decrypt MD5hash which created. You need all information to decrypt the MD5value which was used during encryption.
无法解密MD5创建的哈希。您需要所有信息来解密MD5加密期间使用的值。
You can use AESalgorithm to encrypt and decrypt
您可以使用AES算法来加密和解密
JavaScript AES encryption and decryption (Advanced Encryption Standard)
回答by ShuklaSannidhya
Hashes can not be decrypted check this out.
哈希值无法解密,请查看。
If you want to encrypt-decrypt, use a two way encryption function of your database like - AES_ENCRYPT (in MySQL).
如果要加密-解密,请使用数据库的双向加密功能,例如 - AES_ENCRYPT(在 MySQL 中)。
But I'll suggest CRYPT_BLOWFISH algorithm for storing password. Read this- http://php.net/manual/en/function.crypt.phpand http://us2.php.net/manual/en/function.password-hash.php
但我会建议使用 CRYPT_BLOWFISH 算法来存储密码。阅读这个 - http://php.net/manual/en/function.crypt.php和http://us2.php.net/manual/en/function.password-hash.php
For Blowfish by crypt()function -
对于 Blowfish 按crypt()功能 -
$options = [
'cost' => 7,
'salt' => 'BCryptRequires22Chrcts',
];
password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options);
password_hashwill be introduced in PHP 5.5.
password_hash将在 PHP 5.5 中引入。
Once you have stored the password, you can then check if the user has entered correct password by hashing it again and comparing it with the stored value.
存储密码后,您可以通过再次散列并将其与存储的值进行比较来检查用户是否输入了正确的密码。

