Jump to content

Php Encryption/Decryption with Key issues


jargen

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.