Re: mcrypt
| From: | Purushotham Komaravolu | Date: | Tue, 30 Jul 2002 18:44:20 +0000 |
| Subject: | Re: mcrypt | ||
| References: | 1 2 | Groups: | php.dev php.general |
| Request: | Send a blank email to php-dev+get-86344@lists.php.net to get a copy of this message | ||
Hi,
Thanks for the prompt answer. But I am still getting the same error.
/////////////////////////////////////////////////////
original: meet at secret place
encrypted: d40d72f1b224b9bf86a7dbc52402c1d02a5cf90adb9050f0
Warning: mcrypt_generic_init: Memory allocation error in
/mount/marsellus/gwolfe/puruclient/staging/vhosts/partnersdev.sec.yaga.com/h
tml/time/cancelsubscription/new.php on line 29
Warning: mdecrypt_generic(): 2 is not a valid MCrypt resource in
/mount/marsellus/gwolfe/puruclient/staging/vhosts/partnersdev.sec.yaga.com/h
tml/time/cancelsubscription/new.php on line 30
Warning: mcrypt_generic_end(): 2 is not a valid MCrypt resource in
/mount/marsellus/gwolfe/puruclient/staging/vhosts/partnersdev.sec.yaga.com/h
tml/time/cancelsubscription/new.php on line 31
decrypted:
///////////////////////////////////////////
Regards,
Purushotham Komaravolu
Software Engineer
Yaga, Inc. - "advanced payment services"
Direct: 415-901-7343
Fax: 415-901-1586
http://www.yaga.com
----- Original Message -----
From: "Tech Support" <jgrill@web-1hosting.net>
To: "Purushotham Komaravolu" <purikk@hotmail.com>
Sent: Tuesday, July 30, 2002 11:34 AM
Subject: Re: [PHP] mcrypt
> Rather than tease you with hints I'll give you some working code ;-)
>
> Documentation for practical usage of mcrypt is weak. I agree.
>
> <?
> // crypto.inc
> $key = "secret key crap";
>
> function hex2bin($data)
> {
> $len = strlen($data);
> return pack("H" . $len, $data);
> }
>
> function encrypt($string, $key)
> {
> // version 2.4.x of lib mcrypt
> $td = mcrypt_module_open (MCRYPT_TripleDES, "", MCRYPT_MODE_ECB, "");
> $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND);
> mcrypt_generic_init ($td, $key, $iv);
> $crypted = mcrypt_generic ($td, $string);
> mcrypt_generic_end ($td);
> return bin2hex($crypted);
> }
>
> function decrypt($string, $key)
> {
> //version 2.4.x of lib mcrypt
> $string = hex2bin($string);
> $td = mcrypt_module_open (MCRYPT_TripleDES, "", MCRYPT_MODE_ECB, "");
> $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND);
> mcrypt_generic_init ($td, $key, $iv);
> $decrypted = mdecrypt_generic ($td, $string);
> mcrypt_generic_end ($td);
> return trim($decrypted);
> }
> ?>
>
>
> usage:
> <?
> include ("path/to/crypto.inc");
> $secret = "meet at secret place";
> $encrypted = encrypt($secret, $key);
> print "original: " . $secret . "<br>";
> print "encrypted: " . $encrypted . "<br>";
> $decrypted = decrypt($encrypted, $key);
> print "decrypted: " . $decrypted . "<br>";
> ?>
>
> Note: if you are encrypting really secret crap like credit card numbers or
> something of that nature then NEVER include the key anywhere in your code.
> Make a form where you have to type it in or something in order to display
> the results.
>
>
> Jim Grill
> Support
> Web-1 Hosting
> http://www.web-1hosting.net
> ----- Original Message -----
> From: "Purushotham Komaravolu" <purikk@hotmail.com>
> To: <php-general@lists.php.net>; <php-dev@lists.php.net>
> Sent: Tuesday, July 30, 2002 12:52 PM
> Subject: [PHP] mcrypt
>
>
> Hello,
> I am getting some odd errors trying to get an encrypt/decrypt
> process to
> work. Looking at the manual examples and some other literature, I
have
> tried
> the two approaches listed below. For each, I get a sometimes-works,
> sometimes fails result. The manual entry has a string of user notes
> with
> problem like mine, but I still have problems.
>
>
>
> Server API Apache
>
>
>
> mcrypt
> mcrypt support enabled
> version 2.4.x
> Supported ciphers twofish rijndael-128 rijndael-192
rijndael-256
> saferplus rc2 xtea serpent safer-sk64 safer-sk128 cast-256 loki97
gost
> threeway cast-128 des tripledes enigma arcfour panama wake
> Supported modes ofb cfb nofb cbc ecb stream
>
>
> --]
>
>
>
> The first attempt used the following code:
>
>
> -->
> <?php
> $key = "this is a secret key";
> $input = "Let us meet at 9 o'clock at the secret place.";
>
>
> $td = mcrypt_module_open ('tripledes', '', 'ecb',
> '');
> $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td),
> MCRYPT_RAND);
> mcrypt_generic_init ($td, $key, $iv);
> $encrypted_data = mcrypt_generic ($td, $input);
> $decrypted_data = mdecrypt_generic ($td, $encrypted_data);
> mcrypt_generic_end ($td);
>
>
> echo "key:
>
>
$key<br>input:$input<br>encrypted:$encrypted_data<br>decrypted:$decrypted_da
> ta";
> ?>
> --]
>
>
> This resulted, at first, in "key: this is a secret key
> input:Let us meet at 9 o'clock at the secret place.
> encrypted:\ºþêTÏ'áz(v¹FýaõFËU³æç SäÇÚÖzßù5Qì<±_T-:Í
> decrypted:Let us meet at 9 o'clock at the secret place."
>
>
> BUT after I refreshed/reloaded a couple of times, I got this:
> "Warning:
> mcrypt_generic_init: Memory allocation error in
> /home/pndrdrm001/www/administrator/crypt.php on line 64
>
>
> Warning: 1 is not a valid MCrypt resource in
> /home/pndrdrm001/www/administrator/crypt.php on line 65
>
>
> Warning: 1 is not a valid MCrypt resource in
> /home/pndrdrm001/www/administrator/crypt.php on line 66
>
>
> Warning: 1 is not a valid MCrypt resource in
> /home/pndrdrm001/www/administrator/crypt.php on line 67
> key: this is a secret key
> input:Let us meet at 9 o'clock at the secret place.
> encrypted:
> decrypted: "
>
>
> There were no changes to the code.
>
>
>
> The second try used the following:
>
>
> In file 1, the functions:
> -->
> <?php
>
>
> function my_encrypt($sString)
> {
> GLOBAL $sCryptoKey;
>
>
> $iIV = mcrypt_create_iv (mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256,
> MCRYPT_MODE_ECB), MCRYPT_RAND);
>
>
> $sEncrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $sCryptoKey,
> $sString,
> MCRYPT_MODE_ECB, $iIV);
>
>
> return($sEncrypted);
> } // End function my_encrypt
>
>
> function my_decrypt($sString)
> {
> GLOBAL $sCryptoKey;
>
>
> $iIV = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256,
> MCRYPT_MODE_ECB), MCRYPT_RAND);
>
>
> $sDecrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $sCryptoKey,
> $sString,
> MCRYPT_MODE_ECB, $iIV);
>
>
> return(trim($sDecrypted));
>
>
> } // End function my_decrypt
> ?>
> --]
> and in file 2, the main page:
> -->
> <?php
> include "cryption.php";
> $sCryptoKey = "key";
> $input = "test";
> $encrypted = my_encrypt($input);
> $decrypted = my_decrypt($encrypted);
>
>
> echo "key:
>
>
$sCryptoKey<br>input:$input<br>encrypted:$encrypted<br>decrypted:$decrypted"
> ;
>
>
>
> ?>
> --]
>
>
> This resulted in "key: key
> input:test
> encrypted: &foUÝø§ª~RM¡°Kz à¼O¼¿rw"x@nÉ
> decrypted:test " the first time, but then I got "Fatal error:
> generic_init
> failed in /home/pndrdrm001/www/administrator/cryption.php on line 9"
> on the
> second refresh.
>
>
>
> Is there a missing call to free resources, or something? What can be
> done?
>
>
> Thanks!
> Regards,
>
> Puru
>