Warning: Cannot use a scalar value as an array in /home/admin/public_html/forum/include/fm.class.php on line 757

Warning: Invalid argument supplied for foreach() in /home/admin/public_html/forum/include/fm.class.php on line 770
Форумы портала PHP.SU :: Версия для печати :: Не работает скрипт расшифровки
Форумы портала PHP.SU » » Если скрипт не работает » Не работает скрипт расшифровки

Страниц (1): [1]
 

1. ruslanaxti - 17 Марта, 2015 - 17:41:56 - перейти к сообщению
Функция шифрует строку, но расшифровать не получается. Подскажите где ошибка.

PHP:
скопировать код в буфер обмена
  1.  
  2. function encrypt($decrypted, $password="123", $salt='!kQm*fF3pXe1Kbm%9') {
  3. // Build a 256-bit $key which is a SHA256 hash of $salt and $password.
  4. $key = hash('SHA256', $salt . $password, true);
  5. // Build $iv and $iv_base64.  We use a block size of 128 bits (AES compliant) and CBC mode.  (Note: ECB mode is inadequate as IV is not used.)
  6. srand(); $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
  7. if (strlen($iv_base64 = rtrim(base64_encode($iv), '=')) != 22) return false;
  8. // Encrypt $decrypted and an MD5 of $decrypted using $key.  MD5 is fine to use here because it's just to verify successful decryption.
  9. $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $decrypted . md5($decrypted), MCRYPT_MODE_CBC, $iv));
  10. // We're done!
  11. return $iv_base64 . $encrypted;
  12. }
  13.  
  14.  
  15. function decrypt($encrypted, $password="123", $salt='!kQm*fF3pXe1Kbm%9') {
  16. // Build a 256-bit $key which is a SHA256 hash of $salt and $password.
  17. $key = hash('SHA256', $salt . $password, true);
  18. // Retrieve $iv which is the first 22 characters plus ==, base64_decoded.
  19. $iv = base64_decode(substr($encrypted, 0, 22) . '==');
  20. // Remove $iv from $encrypted.
  21. $encrypted = substr($encrypted, 22);
  22. // Decrypt the data.  rtrim won't corrupt the data because the last 32 characters are the md5 hash; thus any \0 character has to be padding.
  23. $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($encrypted), MCRYPT_MODE_CBC, $iv), "\0\4");
  24. // Retrieve $hash which is the last 32 characters of $decrypted.
  25. $hash = substr($decrypted, -32);
  26. // Remove the last 32 characters from $decrypted.
  27. $decrypted = substr($decrypted, 0, -32);
  28. // Integrity check.  If this fails, either the data is corrupted, or the password/salt was incorrect.
  29. //if (md5($decrypted) != $hash) return false;
  30. // Yay!
  31. return $decrypted;
  32. }
  33.  
  34. $a = base64_encode(encrypt("rus"));
  35. print $a;
  36.  
  37. print base64_encode(decrypt($a));  // здесь строка не расшифровывается
  38.  
2. LIME - 17 Марта, 2015 - 19:30:33 - перейти к сообщению
А зачем еще base64_encode лишний раз?
в нем точно есть причина
3. IllusionMH - 17 Марта, 2015 - 20:15:59 - перейти к сообщению
ruslanaxti, подозреваю что эти операции не коммутируют и судя по всему сначала нужно из base64 декодить( не как в коде), а потом уже использовать магическую самописную функцию
4. ruslanaxti - 17 Марта, 2015 - 20:22:36 - перейти к сообщению
Да, все верно! Причина была в этом. Спасибо!

 

Powered by ExBB FM 1.0 RC1