LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-30-2010, 09:31 PM   #1
action_owl
Member
 
Registered: Jan 2009
Location: 127.0.0.1
Distribution: Fedora, CentOS, NetBSD
Posts: 115

Rep: Reputation: 17
[SOLVED] Unusual encryption results with php's mcrypt


I have a simple class that encrypts strings. It seemed to be working fine until I tried to compare the decrypted values to the original. below is the output of the php code. It appears to be decrypted but the length is incorrect.

String Before Encryption: a text string
String Length Before Encryption: 13

String After Encryption: rew2iSYotruIpmJ3llos3A==
String Length After Encryption: 24

String After Decryption: a text string
String Length After Decryption: 16

Any insight would be very helpful, thanks.

Code:
<?

class Encryption 
{
    private $key;
	
    public function __construct($key='Some_Encryption_Key')
    {
	$this->key = $key;
    }

    public function encrypt( $string )
    {
        return base64_encode
        (
        	mcrypt_encrypt
        	(
        		MCRYPT_RIJNDAEL_128, 
        		$this->key, 
        		$string, 
        		MCRYPT_MODE_ECB
        	) 
        );
    }
 
    public function decrypt( $string )
    {
        return mcrypt_decrypt
        ( 
        	MCRYPT_RIJNDAEL_128, 
        	$this->key, 
        	base64_decode( $string ), 
        	MCRYPT_MODE_ECB
        );
    }
}

$e = new Encryption();

$string = 'a text string';

echo "String Before Encryption: $string <br>";
echo 'String Length Before Encryption: ' . strlen($string) . "<br><br>";

$string = $e->encrypt($string);

echo "String After Encryption: $string <br>";
echo 'String Length After Encryption: ' . strlen($string) . "<br><br>";

$string = $e->decrypt($string);

echo "String After Decryption: $string <br>";
echo 'String Length After Decryption: ' . strlen($string) . "<br><br>";

?>
Same results when calling functions directly:

Encrypted Using mcrypt function directly: ì6‰&(¶»ˆ¦bw–Z,Ü
Decrypted Using mcrypt function directly: a text string
String After Decryption: a text string
String Length After Decryption: 16


Code:
$string = 'a text string';
$key = 'Some_Encryption_Key';

$string = mcrypt_encrypt
(
	MCRYPT_RIJNDAEL_128, 
	$key, 
	$string, 
	MCRYPT_MODE_ECB
);

echo "Encrypted Using mcrypt function directly: $string <br>";    	

$string = mcrypt_decrypt
(
	MCRYPT_RIJNDAEL_128, 
	$key, 
	$string, 
	MCRYPT_MODE_ECB
);

echo "Decrypted Using mcrypt function directly: $string <br>";    	
echo "String After Decryption: $string <br>";
echo 'String Length After Decryption: ' . strlen($string) . "<br><br>";

Last edited by action_owl; 05-31-2010 at 12:36 AM. Reason: typo
 
Old 05-30-2010, 09:54 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well on initial inspection I would say that your manual method is not the same as there is no reference to the use of base64_[en|de]code.
If you utilise these functions is your data still correct with the manual method?
 
Old 05-30-2010, 10:10 PM   #3
action_owl
Member
 
Registered: Jan 2009
Location: 127.0.0.1
Distribution: Fedora, CentOS, NetBSD
Posts: 115

Original Poster
Rep: Reputation: 17
Post

The data is still not returned correctly when using base64_[en|de]code and calling the functions directly.

Encrypted Using mcrypt function directly: rew2iSYotruIpmJ3llos3A==
Decrypted Using mcrypt function directly: a text string
String After Decryption: a text string
String Length After Decryption: 16

Code:
// with base64_[en|de]code

$string = 'a text string';
$key = 'Some_Encryption_Key';

$string = base64_encode
(
	mcrypt_encrypt
	(
		MCRYPT_RIJNDAEL_128, 
		$key, 
		$string, 
		MCRYPT_MODE_ECB
	) 
);

echo "Encrypted Using mcrypt function directly: $string <br>";    	

$string = mcrypt_decrypt
(
	MCRYPT_RIJNDAEL_128, 
	$key, 
	base64_decode( $string ), 
	MCRYPT_MODE_ECB
);

echo "Decrypted Using mcrypt function directly: $string <br>";    	
echo "String After Decryption: $string <br>";
echo 'String Length After Decryption: ' . strlen($string) . "<br><br>";

Last edited by action_owl; 05-30-2010 at 10:12 PM.
 
Old 05-30-2010, 10:19 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
My bad I thought you had said it was working manually (RTFQ)
 
Old 05-30-2010, 10:30 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
hmmm ... I have not played a lot with php but when doing a similar exercise in Python you are required to supply the padding for some of the encryption methods.
I noticed after reading a little on mcrypt that it automatically pads for you, so I am wondering if it is this padding that you are getting in the length?
What happens if you try a slightly shorter or longer string? (maybe try 16 characters first to see if that shows us something?)
 
Old 05-30-2010, 10:31 PM   #6
action_owl
Member
 
Registered: Jan 2009
Location: 127.0.0.1
Distribution: Fedora, CentOS, NetBSD
Posts: 115

Original Poster
Rep: Reputation: 17
If I use base64_[en|de]code on the string before and after encrypting,then it will produce the proper results, though I'm not exactly sure why I have to do this. Perhaps mcrypt requires a base64_encoded string to begin with?

Encrypted Using mcrypt function directly: pzQc7oBucoR7KBh+el31g0Rl5dZTwfPDSrNoy8Y9LHc=
Decrypted Using mcrypt function directly: a text string
String After Decryption: a text string
String Length After Decryption: 13

Code:
$string = base64_encode('a text string');
$key = 'Some_Encryption_Key';

$string = base64_encode
(
	mcrypt_encrypt
	(
		MCRYPT_RIJNDAEL_128, 
		$key, 
        $string, 
        MCRYPT_MODE_ECB
	) 
);

echo "Encrypted Using mcrypt function directly: $string <br>";    	

$string = mcrypt_decrypt
(
	MCRYPT_RIJNDAEL_128, 
	$key, 
	base64_decode( $string ), 
	MCRYPT_MODE_ECB
);

$string = base64_decode($string);

echo "Decrypted Using mcrypt function directly: $string <br>";    	
echo "String After Decryption: $string <br>";
echo 'String Length After Decryption: ' . strlen($string) . "<br><br>";
 
Old 05-30-2010, 10:41 PM   #7
action_owl
Member
 
Registered: Jan 2009
Location: 127.0.0.1
Distribution: Fedora, CentOS, NetBSD
Posts: 115

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by grail View Post
hmmm ... I have not played a lot with php but when doing a similar exercise in Python you are required to supply the padding for some of the encryption methods.
I noticed after reading a little on mcrypt that it automatically pads for you, so I am wondering if it is this padding that you are getting in the length?
What happens if you try a slightly shorter or longer string? (maybe try 16 characters first to see if that shows us something?)
I think this is what's happening.
I get proper string length returned with strings that are 16 or 32 characters in length but incorrect results with 13 and 31.

Here's is the properly working encryption class:

Code:
class Encryption 
{
    private $key;
	
    public function __construct($key='Some_Encryption_Key')
    {
	$this->key = $key;
    }

    public function encrypt( $string )
    {
        return base64_encode
        (
        	mcrypt_encrypt
        	(
        		MCRYPT_RIJNDAEL_128, 
        		$this->key, 
        		base64_encode( $string ), 
        		MCRYPT_MODE_ECB
        	) 
        );
    }
 
    public function decrypt( $string )
    {
        return base64_decode
        ( 
			mcrypt_decrypt
			(
				MCRYPT_RIJNDAEL_128, 
				$this->key, 
				base64_decode( $string ), 
				MCRYPT_MODE_ECB
			)
		);
    }
}

Last edited by action_owl; 05-31-2010 at 12:35 AM.
 
Old 05-31-2010, 02:24 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Glad you got it, although I am curious what would happen if you just return the output from mcrypt_[en|de]crypt as it returns a string?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
md5, SHA and php's mcrypt function rjcrews General 1 12-05-2005 12:54 AM
sendmail and PHP's mail() issues Magsol Linux - General 5 03-26-2005 03:15 PM
Problems using PHP's IMAP functions... LWillmann Red Hat 4 04-11-2004 01:55 PM
PHP's imagecreatefromPNG function Tomasfuego Linux - Software 0 06-20-2003 12:58 AM
apache's OK, php's not matt Linux - Software 9 11-25-2001 09:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration