问题描述
我正在开发一个简单的Zend应用程序,我需要在将所有财务数据存储在数据库中之前对其进行加密,并在需要时进行解密。我使用 mcrypt_encrypt()
和 mcrypt_decrypt()
。当我需要解密数字时,我使用了一个常量初始化向量(iv),这是不完全推荐的。 这是我的代码:
define('string' 'WdryhedeescmsfkirYNemsjdesapQ');
define('iv','$ 356?dWuSkm)@ g%dnw#8mA *');
class FormatValues {
const string ='WdryhedeescmsfkirYNemsjdesapQ';
const iv ='$ 356?dWuSkm)@ g%dnw#8mA *';
public function encrypt($ val){
$ enc = mcrypt_encrypt(MCRYPT_RIJNDAEL_256,$ val,self :: string,MCRYPT_MODE_CBC,self :: iv);
return $ enc;
}
public function decrypt($ val){
$ dec = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256,$ val,self :: string,MCRYPT_MODE_CBC,self :: iv) \0);
return $ dec;
}
}
encrypt()
方法对数据进行加密,但是在解密时它没有给出正确的数字。
为什么是这样?有没有一些其他方式可以加密和解密数据,而不需要一个常数iv?
提前感谢
Charu
我使用这样的项目,尝试一下!
$ key ='password to(en / de)crypt';
$ string ='要加密的字符串'; //注意空格
$ encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256,md5($ key),$ string,MCRYPT_MODE_CBC,md5(md5($ key))));
$ decryptpted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256,md5($ key)),base64_decode($ encrypted),MCRYPT_MODE_CBC,md5(md5($ key))),\0);
echo'加密:'。 \\\
;
var_dump($ encrypted);
echo\\\
;
echo'Decrypted:'。 \\\
;
var_dump($ decryptpted); //空格保留
I'm working on a simple Zend application and I need to encrypt all the financial figures before storing them in the database, and decrypt them when needed. I used mcrypt_encrypt()
and mcrypt_decrypt()
. As I need to decrypt the figures I used a constant initialization vector(iv), which is not at all recommended.
here is my code:
define ('string','WdryhedeescmsfkirYNemsjdesapQ');
define ('iv', '$356?dWuSkm)@g%dnw#8mA*');
class FormatValues {
const string= 'WdryhedeescmsfkirYNemsjdesapQ';
const iv = '$356?dWuSkm)@g%dnw#8mA*';
public function encrypt($val){
$enc = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $val,self::string , MCRYPT_MODE_CBC,self::iv);
return $enc;
}
public function decrypt($val){
$dec = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $val,self::string , MCRYPT_MODE_CBC,self::iv), "\0");
return $dec;
}
}
The encrypt()
method encrypts the data, but when decrypting, it doesn't give the correct figure.
Why is this? Is there some other way to encrypt and decrypt data without having a constant iv?
Thanks in advance
Charu
I use something like that for my project, try it !
$key = 'password to (en/de)crypt';
$string = ' string to be encrypted '; // note the spaces
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));
$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($encrypted), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
echo 'Encrypted:' . "\n";
var_dump($encrypted);
echo "\n";
echo 'Decrypted:' . "\n";
var_dump($decrypted); // spaces are preserved
这篇关于mcrypt_decrypt无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!