Jump to content

[SOLVED] Encypt/Decrypt via mcrypt problem...


Cory94bailly

Recommended Posts

<?
if (!extension_loaded('mcrypt')) {
    print "Mcrypt not loaded!";
    exit;
} 
// Encrypt Function
function mc_encrypt($encrypt, $mc_key) {
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
$passcrypt = trim(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($encrypt), MCRYPT_MODE_ECB, $iv));
$encode = base64_encode($passcrypt);
return $encode;
}

// Decrypt Function
function mc_decrypt($decrypt, $mc_key) {
$decoded = base64_decode($decrypt);
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
$decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($decoded), MCRYPT_MODE_ECB, $iv));
return $decrypted;
}

echo "<b>Encrypt:</b> ". mc_encrypt("test", "key");
echo "<br>";
echo "<br>";
echo "<b>Decrypt:</b> ".mc_decrypt("test", "key");
?>

 

Well there's my code to try it out and here's what is being displayed:

 

Encrypt: JmZvVd0H+KeqflJNoQWwS3oJ4LxPvL9yd5R4QG7J

 

Decrypt: ‚Î7'#d;0XÒk†zÈ#×âMeǪ‚šbÃóéMˆ

 

 

(Incase the forum changes it's format, here:)

 

j7t56w.png

Link to comment
https://forums.phpfreaks.com/topic/166634-solved-encyptdecrypt-via-mcrypt-problem/
Share on other sites

and the problem is ?

 

EDIT:

Unless the you where expecting the same result the reason your not is because your trying to decrypt unencrypted data..

try

$data = mc_encrypt("test", "key");
echo "<b>Encrypt:</b> ". $data;
echo "<br>";
echo "<br>";
echo "<b>Decrypt:</b> ".mc_decrypt($data, "key");

Ok whatever I do, I can't get them to be the same..  :wtf: :wtf:

 

I am not getting this:

Encrypt: JmZvVd0H+KeqflJNoQWwS3oJ4LxPvL9yd5R4QG7J

Decrypt: ƒ£`€<8Òö:0>/ˆ_‹YYVfk¤K‰ƒgc

 

 

And my code is this:

<?
if (!extension_loaded('mcrypt')) {
    print "Mcrypt not loaded!";
    exit;
} 
// Encrypt Function
function mc_encrypt($encrypt, $mc_key) {
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
$passcrypt = trim(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($encrypt), MCRYPT_MODE_ECB, $iv));
$encode = base64_encode($passcrypt);
return $encode;
}

// Decrypt Function
function mc_decrypt($decrypt, $mc_key) {
$decoded = base64_decode($decrypt);
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
$decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($decoded), MCRYPT_MODE_ECB, $iv));
return $decrypted;
}

echo "<b>Encrypt:</b> ". mc_encrypt("test", "key");
echo "<br>";
echo "<br>";
echo "<b>Decrypt:</b> ".mc_decrypt("JmZvVd0H+KeqflJNoQWwS3oJ4LxPvL9yd5R4QG7J", "key");
?>

 

Any help?

Hmm... Check this out:

 

$key = 'key';
$message = 'test';

$encrypted = mc_encrypt($message, $key);
$decrypted = mc_decrypt($encrypted, $key);

if ($decrypted == $message) {
echo 'Works';
}
else {
echo 'Broken';
}

 

That will output "Broken", but if you do $key = 'abc'; it will output "Works". I cannot give an explanation to that.

Hmm... Check this out:

 

$key = 'key';
$message = 'test';

$encrypted = mc_encrypt($message, $key);
$decrypted = mc_decrypt($encrypted, $key);

if ($decrypted == $message) {
echo 'Works';
}
else {
echo 'Broken';
}

 

That will output "Broken", but if you do $key = 'abc'; it will output "Works". I cannot give an explanation to that.

 

Yep.. Same thing for me..

 

Is there a better encrypt/decrypt function available (Using any form of mcrypt..)

Actually.. It seems to work with a longer key..

 

Yeah, as I said, it only seems to happen if the key is "key".

 

Ok, thanks ;)

 

I marked it solved..

 

 

Jeez.. phpfreaks is always helpful, I love this community  ::)  :shy:

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.