mixkat
mixkat

Reputation: 3785

Correct decryption returns empty string

I am trying to decrypt some data that I have stored in my db.

Can someone tell me what i'm doing wrong here?

I have checked and rechecked all the values that I am passing in and seem to be identical to the ones I used for encrypting.

Also whenever i pass in an incorrect key it does produce output but whenever i'm passing the right key it returns an empty string.

And the funny thing is that javascript doesn't see it as an empty string although firebug shows it as one.

My code for encryption:

$iv = hash('sha256',$_POST['ben'].$_POST['assetName'],true);
$secretKeyFromAnswer = hash('sha256',$_POST['answer'],true);
$encedUsername = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $secretKeyFromAnswer , $_POST['username'], MCRYPT_MODE_CBC, $iv));

My code for decryption:

$hashKey = hash('sha256',$_POST['key'],true);
$iv = hash('sha256',$_POST['ben'].$_POST['assetName'],true);
$theData = base64_decode($userToDec);
$decUsername = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $hashKey, $theData , MCRYPT_MODE_CBC, $iv),'\0');

Upvotes: 3

Views: 1598

Answers (1)

Scott Arciszewski
Scott Arciszewski

Reputation: 34113

"Are you sure you're not running the encryption on an empty string?" -- AtkinsSJ

You are the man! Had a typo in my jquery code and was in fact reading an empty string before encrypting! Put up an answer if you want so i can accept and u get your rep! Cheers

Upvotes: 2

Related Questions