Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Zaszyfrowane dane w bazie MSSQL
ssylwester
post
Post #1





Grupa: Zarejestrowani
Postów: 27
Pomógł: 2
Dołączył: 18.05.2010

Ostrzeżenie: (0%)
-----


Mam dwie różne funkcje szyfrujące i deszyfrujące:

Wykorzystanie mcrypt:
  1. function encryptnow($thecipher, $thekey, $themsg)
  2. {
  3. $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($thecipher), MCRYPT_RAND);
  4. mcrypt_generic_init($thecipher, $thekey, $iv);
  5. $encrypted_text = mcrypt_generic($thecipher, $themsg);
  6. mcrypt_generic_deinit($thecipher);
  7. mcrypt_module_close($thecipher);
  8. return $encrypted_text;
  9. }
  10.  
  11. function decryptnow($thecipher, $thekey, $thencrypted){
  12. $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($thecipher), MCRYPT_RAND);
  13. mcrypt_generic_init($thecipher, $thekey, $iv);
  14. $decrypted_text = mdecrypt_generic($thecipher, $thencrypted);
  15. mcrypt_generic_deinit($thecipher);
  16. mcrypt_module_close($thecipher);
  17. return $decrypted_text;
  18. }
  19.  
  20. function startEncrytpion($choice, $mykey, $msg){
  21. if ($choice == '1'){
  22. $cipher = mcrypt_module_open (MCRYPT_DES, '', 'ecb', '');
  23. //algorithmdetails($cipher);
  24. return encryptnow($cipher, $mykey, $msg);
  25.  
  26. }elseif ($choice == '2'){
  27. $cipher = mcrypt_module_open (MCRYPT_3DES, '', 'ecb', '');
  28. //algorithmdetails($cipher);
  29. return encryptnow($cipher, $mykey, $msg);
  30.  
  31. }elseif ($choice == '3'){
  32. $cipher = mcrypt_module_open (MCRYPT_RIJNDAEL_128, '', 'ecb', '');
  33. //algorithmdetails($cipher);
  34. return encryptnow($cipher, $mykey, $msg);
  35.  
  36. }elseif ($choice == '4'){
  37. $cipher = mcrypt_module_open (MCRYPT_GOST, '', 'ecb', '');
  38. //algorithmdetails($cipher);
  39. return encryptnow($cipher, $mykey, $msg);
  40. }
  41. }
  42.  
  43. function startDecrytpion($choice, $mykey, $msg){
  44. if ($choice == '1'){
  45. $cipher = mcrypt_module_open (MCRYPT_DES, '', 'ecb', '');
  46. //algorithmdetails($cipher);
  47. return decryptnow($cipher, $mykey, $msg);
  48.  
  49. }elseif ($choice == '2'){
  50. $cipher = mcrypt_module_open (MCRYPT_3DES, '', 'ecb', '');
  51. //algorithmdetails($cipher);
  52. return decryptnow($cipher, $mykey, $msg);
  53.  
  54. }elseif ($choice == '3'){
  55. $cipher = mcrypt_module_open (MCRYPT_RIJNDAEL_128, '', 'ecb', '');
  56. //algorithmdetails($cipher);
  57. return decryptnow($cipher, $mykey, $msg);
  58.  
  59. }elseif ($choice == '4'){
  60. $cipher = mcrypt_module_open (MCRYPT_GOST, '', 'ecb', '');
  61. //algorithmdetails($cipher);
  62. return decryptnow($cipher, $mykey, $msg);
  63. }
  64. }


i takie proste szyfrowanie:
  1. function Encrypt($string, $key){
  2. $result = '';
  3. for($i=1; $i<=strlen($string); $i++){
  4. $char = substr($string, $i-1, 1);
  5. $keychar = substr($key, ($i % strlen($key))-1, 1);
  6. $char = chr(ord($char)+ord($keychar));
  7. $result.=$char;
  8. }
  9. return $result;
  10. }
  11.  
  12. function Decrypt($string, $key){
  13. $result = '';
  14. for($i=1; $i<=strlen($string); $i++){
  15. $char = substr($string, $i-1, 1);
  16. $keychar = substr($key, ($i % strlen($key))-1, 1);
  17. $char = chr(ord($char)-ord($keychar));
  18. $result.=$char;
  19. }
  20. return $result;
  21. }


Próbuję zaszyfrować tekst i wpisać go do bazy MSSQL do kolumny o typie "text"
Jednak po wyciągnięciu tych danych z bazy i odszyfrowaniu uzyskuję krzaczki.
Wiem że rozwiązania takie działają z bazą MySql. Czy zna ktoś sposób jak zapisać zaszyfrowane dane do bazy MSSQL tak
aby po wyciągnięciu ich i odszyfrowaniu uzyskać to samo co przed szyfrowaniem i zapisaniem? Niestety nie mogę korzystać
z bazy MySql.
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 23.08.2025 - 20:17