jargen Posted February 16, 2012 Share Posted February 16, 2012 Hi, Im using this code: function encryptdata($data_input,$key){ $td = mcrypt_module_open('cast-256', '', 'ecb', ''); $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND); mcrypt_generic_init($td, $key, $iv); $encrypted_data = mcrypt_generic($td, $data_input); mcrypt_generic_deinit($td); mcrypt_module_close($td); $encoded_64=base64_encode($encrypted_data); return $encoded_64; } function decryptdata($encoded_64,$key){ $decoded_64=base64_decode($encoded_64); $td = mcrypt_module_open('cast-256', '', 'ecb', ''); $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND); mcrypt_generic_init($td, $key, $iv); $decrypted_data = mdecrypt_generic($td, $decoded_64); mcrypt_generic_deinit($td); mcrypt_module_close($td); return $decrypted_data; } To try to encrypt and decrypt some data, However the decrypted password has the first 2 letters missing. Whatever length string i put in its always the first 2 letters that go missing. I have tried multiple pieces of code over the internet with the exact same problem. Example: (Original Password, Encrypted Password, Decrypted Password) BEBw3nJ9rT y3CdmtNeTmhO3Jrq/00YOA== Bw3nJ9rT Any help? Thanks. P.s Yes i am using the same key for the encryption and decryption. Quote Link to comment Share on other sites More sharing options...
scootstah Posted February 16, 2012 Share Posted February 16, 2012 You shouldn't be encrypting passwords. Quote Link to comment Share on other sites More sharing options...
jargen Posted February 16, 2012 Author Share Posted February 16, 2012 You shouldn't be encrypting passwords. That isnt the point. Quote Link to comment Share on other sites More sharing options...
kicken Posted February 17, 2012 Share Posted February 17, 2012 Appears to work fine for me if I copy/paste to a test script. define('ENCKEY', 'asdf'); $txt = 'kicken'; $enctxt = encryptdata($txt, ENCKEY); $out = decryptdata($enctxt, ENCKEY); var_dump($txt, $enctxt, $out); /* string(6) "kicken" string(24) "k9+Jryul9CZ3Bi9GvnwEaw==" string(16) "kicken" */ Sure there isn't a small bug in your test code somewhere? edit: Just noticed the output is string(16) as it's padded with NUL bytes. A quick rtrim solved that. Quote Link to comment Share on other sites More sharing options...
jargen Posted February 17, 2012 Author Share Posted February 17, 2012 The test code is $password = generatepassword(); $passwordsafe = encryptdata($password,"dddsgsdfnhjkdgh"); $test = decryptdata($passwordsafe, "dddsgsdfnhjkdgh"); print($password." ".$passwordsafe." ".$test); So i dont understand what is happening Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.