简体   繁体   English

Yii加密和解密密码

[英]Yii encrypt and decrypt password

I'm using Yii CSecurityManager for Password encryption: 我正在使用Yii CSecurityManager进行密码加密:

$this->securityManager->encrypt('TEST', '1');

*the TEST is the string to encrypt and the 1 is the key. * TEST是要加密的字符串,1是密钥。

but when i test before i decrypt i find that the value keeps changing. 但是当我在解密之前进行测试时,我发现值一直在变化。

for ($index = 0; $index < 10; $index++) {
        $EncPassword = $this->securityManager->encrypt('TEST', '1');
        echo $EncPassword;
    }

i'm relying on this value in another part of my application...I dug into the encrypt password i see that it is in fact random: 我在应用程序的另一部分中依赖此值...我研究了加密密码,我发现它实际上是随机的:

public function encrypt($data,$key=null)
{
    $module=$this->openCryptModule();
    $key=$this->substr($key===null ? md5($this->getEncryptionKey()) : $key,0,mcrypt_enc_get_key_size($module));
    srand();
    $iv=mcrypt_create_iv(mcrypt_enc_get_iv_size($module), MCRYPT_RAND);
    mcrypt_generic_init($module,$key,$iv);
    $encrypted=$iv.mcrypt_generic($module,$data);
    mcrypt_generic_deinit($module);
    mcrypt_module_close($module);
    return $encrypted;
}

so my question is how can i encrypt based on a key and get the same value each time? 所以我的问题是如何基于密钥加密并每次都获得相同的值?

thanks, Danny 谢谢,丹尼

In principle you can create the same ciphertext each time. 原则上,您每次都可以创建相同的密文。 Just use a static IV and you would have accomplished it. 只要使用静态IV,就可以完成。 It would however mean that you would leak information about the passwords. 但是,这意味着您将泄漏有关密码的信息。 Identical passwords would have the same ciphertext for different users. 对于不同的用户,相同的密码将具有相同的密文。

If you really want to have the same ciphertext, prepend the first 16 bytes of a hash over the username to the password and encrypt with a zero IV. 如果您确实希望拥有相同的密文,请在用户名之前将哈希的前16个字节放在密码之前,并使用零IV进行加密。 Note that this still could leak a bit of information about the password in time. 请注意,这仍然会及时泄漏有关密码的一些信息。

Note that using the ciphertext value for other means than storage of the plain text is a very bad idea in general. 请注意,将密文值用于除存储纯文本以外的其他方式通常是一个非常糟糕的主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM