当前位置:Gxlcms > PHP教程 > mcrypt如何对已加密的字符串解密?

mcrypt如何对已加密的字符串解密?

时间:2021-07-01 10:21:17 帮助过:27人阅读

我在准备使用mcrypt加密/解密用户密码, 但是我发现一个问题,
下面是代码, 但是根本不能解密~~~

问题出在生成$iv上, 两次的$iv不同导致加密后的字符串无法正常解密。
由于这两个方法一个在注册时调用, 一个在登录时调用, 所以没有办法共享$iv.

这得怎么啊?

我google出来的结果都是使用同一个$iv进行加密/解密的~~~

  1. <code> private function encryptPassword($password) {
  2. if ( self::PASSWORD_MAX_LENGTH < strlen($password) ) {
  3. $password = substr($password, 0, self::PASSWORD_MAX_LENGTH);
  4. }
  5. /* @var $accountService AccountService */
  6. $accountService = X::system()->getServiceManager()->get(AccountService::getServiceName());
  7. $passwordSecret = $accountService->getConfiguration()->get('password_secret');
  8. $cipherAlg = MCRYPT_RIJNDAEL_128;
  9. $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipherAlg,MCRYPT_MODE_ECB), MCRYPT_RAND);
  10. $encryptedPassword = mcrypt_encrypt($cipherAlg,$passwordSecret,$password, MCRYPT_MODE_CBC, $iv);
  11. $encryptedPassword = base64_encode($encryptedPassword);
  12. return $encryptedPassword;
  13. }
  14. /**
  15. * @param unknown $encryptedPassword
  16. */
  17. private function decryptPassword($encryptedPassword) {
  18. /* @var $accountService AccountService */
  19. $accountService = X::system()->getServiceManager()->get(AccountService::getServiceName());
  20. $passwordSecret = $accountService->getConfiguration()->get('password_secret');
  21. $cipherAlg = MCRYPT_RIJNDAEL_128;
  22. $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipherAlg,MCRYPT_MODE_ECB), MCRYPT_RAND);
  23. $encryptedPassword = base64_decode($encryptedPassword);
  24. $decryptPassword = mcrypt_decrypt($cipherAlg, $passwordSecret, $encryptedPassword, MCRYPT_MODE_CBC, $iv);
  25. return $decryptPassword;
  26. }
  27. </code>

回复内容:

我在准备使用mcrypt加密/解密用户密码, 但是我发现一个问题,
下面是代码, 但是根本不能解密~~~

问题出在生成$iv上, 两次的$iv不同导致加密后的字符串无法正常解密。
由于这两个方法一个在注册时调用, 一个在登录时调用, 所以没有办法共享$iv.

这得怎么啊?

我google出来的结果都是使用同一个$iv进行加密/解密的~~~

  1. <code> private function encryptPassword($password) {
  2. if ( self::PASSWORD_MAX_LENGTH < strlen($password) ) {
  3. $password = substr($password, 0, self::PASSWORD_MAX_LENGTH);
  4. }
  5. /* @var $accountService AccountService */
  6. $accountService = X::system()->getServiceManager()->get(AccountService::getServiceName());
  7. $passwordSecret = $accountService->getConfiguration()->get('password_secret');
  8. $cipherAlg = MCRYPT_RIJNDAEL_128;
  9. $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipherAlg,MCRYPT_MODE_ECB), MCRYPT_RAND);
  10. $encryptedPassword = mcrypt_encrypt($cipherAlg,$passwordSecret,$password, MCRYPT_MODE_CBC, $iv);
  11. $encryptedPassword = base64_encode($encryptedPassword);
  12. return $encryptedPassword;
  13. }
  14. /**
  15. * @param unknown $encryptedPassword
  16. */
  17. private function decryptPassword($encryptedPassword) {
  18. /* @var $accountService AccountService */
  19. $accountService = X::system()->getServiceManager()->get(AccountService::getServiceName());
  20. $passwordSecret = $accountService->getConfiguration()->get('password_secret');
  21. $cipherAlg = MCRYPT_RIJNDAEL_128;
  22. $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipherAlg,MCRYPT_MODE_ECB), MCRYPT_RAND);
  23. $encryptedPassword = base64_decode($encryptedPassword);
  24. $decryptPassword = mcrypt_decrypt($cipherAlg, $passwordSecret, $encryptedPassword, MCRYPT_MODE_CBC, $iv);
  25. return $decryptPassword;
  26. }
  27. </code>

初始化向量iv是不需要每次都重新生成的,你可以保存下来,拿你的场合来说,你可以把它和密文一起存储,这样就可以解决这个问题了。你可以读php的官方文档的解释 http://php.com/manual/zh/function.mcrypt-create-iv.php

人气教程排行