web-based and command line mcrypting and back again

This is a discussion on web-based and command line mcrypting and back again within the PHP General forums, part of the PHP Programming Forums category; I am trying to figure out how to encrypt data using the web-based php mcrypt function and then decrypt ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-06-2004
Gary C. New
 
Posts: n/a
Default web-based and command line mcrypting and back again

I am trying to figure out how to encrypt data using the web-based php
mcrypt function and then decrypt it using the command line (cli) mcrypt
binary, and vica-versa.

I cannot seem to get the same encrypted output for the same data,
between the two methods. I've tried to ensure I am using the same
algorithms and modes for both methods by testing the mcrypt_enc_get_*
functions. I think the problem may lie in the way both methods are
being seeded.

Web-Based PHP Example:

$key = 'test';
$data[] = 'abcdefghijklmnopqrstuvwxyz';
$m = 0;

foreach ($data as $dt)
{
$td = mcrypt_module_open('tripledes', '', 'ecb', '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$CRYPT[$m] = mcrypt_generic($td, $dt);

/*
echo mcrypt_enc_get_algorithms_name($td);
echo mcrypt_enc_get_block_size($td);
echo mcrypt_enc_get_iv_size($td);
echo mcrypt_enc_get_key_size($td);
echo mcrypt_enc_get_modes_name($td);
echo mcrypt_enc_get_supported_key_sizes($td);
echo mcrypt_enc_is_block_algorithm_mode($td);
echo mcrypt_enc_is_block_algorithm($td);
echo mcrypt_enc_is_block_mode($td);
echo mcrypt_enc_self_test($td);
*/

$DECRYPT[$m] = mdecrypt_generic($td, $dt);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);

echo "crypt_" . base64_encode($CRYPT[$m]) . "_<br />\n";
echo "decrypt_" . base64_decode(rtrim($DECRYPT[$m])) . "_<br />\n";

$m++;
}

Note: I believe it is the line where the $iv variable is being set that
is causing the issue and/or I cannot reproduce the same seeding using
the command line options.


Command Line Example:

echo "abcdefghijklmnopqrstuvwxyz" | mcrypt -Fb -m ecb -a tripledes |
encode-base64

echo "" | decode-base64 | mcrypt -dFb -m ecb -a tripledes


I would appreciate any comments or suggestions.

Respectfully,


Gary
Reply With Quote
  #2 (permalink)  
Old 01-06-2004
Tom Rogers
 
Posts: n/a
Default Re: [PHP] web-based and command line mcrypting and back again

Hi,

Tuesday, January 6, 2004, 8:48:17 PM, you wrote:
GCN> I am trying to figure out how to encrypt data using the web-based php
GCN> mcrypt function and then decrypt it using the command line (cli) mcrypt
GCN> binary, and vica-versa.

GCN> I cannot seem to get the same encrypted output for the same data,
GCN> between the two methods. I've tried to ensure I am using the same
GCN> algorithms and modes for both methods by testing the mcrypt_enc_get_*
GCN> functions. I think the problem may lie in the way both methods are
GCN> being seeded.

GCN> Web-Based PHP Example:

GCN> $key = 'test';
GCN> $data[] = 'abcdefghijklmnopqrstuvwxyz';
GCN> $m = 0;

GCN> foreach ($data as $dt)
GCN> {
GCN> $td = mcrypt_module_open('tripledes', '', 'ecb', '');
GCN> $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
GCN> mcrypt_generic_init($td, $key, $iv);
GCN> $CRYPT[$m] = mcrypt_generic($td, $dt);

GCN> /*
GCN> echo mcrypt_enc_get_algorithms_name($td);
GCN> echo mcrypt_enc_get_block_size($td);
GCN> echo mcrypt_enc_get_iv_size($td);
GCN> echo mcrypt_enc_get_key_size($td);
GCN> echo mcrypt_enc_get_modes_name($td);
GCN> echo mcrypt_enc_get_supported_key_sizes($td);
GCN> echo mcrypt_enc_is_block_algorithm_mode($td);
GCN> echo mcrypt_enc_is_block_algorithm($td);
GCN> echo mcrypt_enc_is_block_mode($td);
GCN> echo mcrypt_enc_self_test($td);
GCN> */

GCN> $DECRYPT[$m] = mdecrypt_generic($td, $dt);
GCN> mcrypt_generic_deinit($td);
GCN> mcrypt_module_close($td);

GCN> echo "crypt_" . base64_encode($CRYPT[$m]) . "_<br />\n";
GCN> echo "decrypt_" . base64_decode(rtrim($DECRYPT[$m])) . "_<br />\n";

GCN> $m++;
GCN> }

GCN> Note: I believe it is the line where the $iv variable is being set that
GCN> is causing the issue and/or I cannot reproduce the same seeding using
GCN> the command line options.


GCN> Command Line Example:

GCN> echo "abcdefghijklmnopqrstuvwxyz" | mcrypt -Fb -m ecb -a tripledes |
GCN> encode-base64

GCN> echo "" | decode-base64 | mcrypt -dFb -m ecb -a tripledes


GCN> I would appreciate any comments or suggestions.

GCN> Respectfully,


GCN> Gary


try setting the iv to all 0's
$iv = 0;
$iv = pack("a".mcrypt_enc_get_iv_size($td),$iv);

--
regards,
Tom
Reply With Quote
  #3 (permalink)  
Old 01-06-2004
Gary C. New
 
Posts: n/a
Default Re: web-based and command line mcrypting and back again

Tom,

I appreciate the suggestion, but even after setting the iv to zero
within the php code and including the --noiv option within the command
line; it still does not produce the same base64 encoded string under
both methods.

I noticed that the command line was keying off of 2 passphrases and the
php code off of only 1, so I forced the command line to key off of only
1 passphrase--to no avail.

I am obviously missing something. Someone out there has had to have
done this at least once before.

Any other suggestions would be appreciated.

Respectfully,


Gary


Tom Rogers wrote:
> Hi,
>
> Tuesday, January 6, 2004, 8:48:17 PM, you wrote:
> GCN> I am trying to figure out how to encrypt data using the web-based php
> GCN> mcrypt function and then decrypt it using the command line (cli) mcrypt
> GCN> binary, and vica-versa.
>
> GCN> I cannot seem to get the same encrypted output for the same data,
> GCN> between the two methods. I've tried to ensure I am using the same
> GCN> algorithms and modes for both methods by testing the mcrypt_enc_get_*
> GCN> functions. I think the problem may lie in the way both methods are
> GCN> being seeded.
>
> GCN> Web-Based PHP Example:
>
> GCN> $key = 'test';
> GCN> $data[] = 'abcdefghijklmnopqrstuvwxyz';
> GCN> $m = 0;
>
> GCN> foreach ($data as $dt)
> GCN> {
> GCN> $td = mcrypt_module_open('tripledes', '', 'ecb', '');
> GCN> $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
> GCN> mcrypt_generic_init($td, $key, $iv);
> GCN> $CRYPT[$m] = mcrypt_generic($td, $dt);
>
> GCN> /*
> GCN> echo mcrypt_enc_get_algorithms_name($td);
> GCN> echo mcrypt_enc_get_block_size($td);
> GCN> echo mcrypt_enc_get_iv_size($td);
> GCN> echo mcrypt_enc_get_key_size($td);
> GCN> echo mcrypt_enc_get_modes_name($td);
> GCN> echo mcrypt_enc_get_supported_key_sizes($td);
> GCN> echo mcrypt_enc_is_block_algorithm_mode($td);
> GCN> echo mcrypt_enc_is_block_algorithm($td);
> GCN> echo mcrypt_enc_is_block_mode($td);
> GCN> echo mcrypt_enc_self_test($td);
> GCN> */
>
> GCN> $DECRYPT[$m] = mdecrypt_generic($td, $dt);
> GCN> mcrypt_generic_deinit($td);
> GCN> mcrypt_module_close($td);
>
> GCN> echo "crypt_" . base64_encode($CRYPT[$m]) . "_<br />\n";
> GCN> echo "decrypt_" . base64_decode(rtrim($DECRYPT[$m])) . "_<br />\n";
>
> GCN> $m++;
> GCN> }
>
> GCN> Note: I believe it is the line where the $iv variable is being set that
> GCN> is causing the issue and/or I cannot reproduce the same seeding using
> GCN> the command line options.
>
>
> GCN> Command Line Example:
>
> GCN> echo "abcdefghijklmnopqrstuvwxyz" | mcrypt -Fb -m ecb -a tripledes |
> GCN> encode-base64
>
> GCN> echo "" | decode-base64 | mcrypt -dFb -m ecb -a tripledes
>
>
> GCN> I would appreciate any comments or suggestions.
>
> GCN> Respectfully,
>
>
> GCN> Gary
>
>
> try setting the iv to all 0's
> $iv = 0;
> $iv = pack("a".mcrypt_enc_get_iv_size($td),$iv);
>

Reply With Quote
  #4 (permalink)  
Old 01-07-2004
Tom Rogers
 
Posts: n/a
Default Re: [PHP] Re: web-based and command line mcrypting and back again

Hi,

Wednesday, January 7, 2004, 8:54:16 AM, you wrote:
GCN> Tom,

GCN> I appreciate the suggestion, but even after setting the iv to zero
GCN> within the php code and including the --noiv option within the command
GCN> line; it still does not produce the same base64 encoded string under
GCN> both methods.

GCN> I noticed that the command line was keying off of 2 passphrases and the
GCN> php code off of only 1, so I forced the command line to key off of only
GCN> 1 passphrase--to no avail.

GCN> I am obviously missing something. Someone out there has had to have
GCN> done this at least once before.

GCN> Any other suggestions would be appreciated.

GCN> Respectfully,


GCN> Gary

The only way I could get it to work was using an encryption class of
mine and a php command line script. The mycrpt command line just didn't get it
right. Here is what I did

<?php
$key = 'test';
$data[] = 'abcdefghijklmnopqrstuvwxyz';
$m = 0;
class encrypt_class{
var $secret;
function encrypt_class(){
$this->secret = 'test';
}
Function encode($id){
$eid = $iv = 0;
$len = strlen($id);
$id = $len.'-'.$id;
$td = mcrypt_module_open(MCRYPT_TripleDES, "", MCRYPT_MODE_ECB, "");
$key = substr($this->secret, 0, mcrypt_enc_get_key_size ($td));
$iv = pack("a".mcrypt_enc_get_iv_size($td),$iv);
mcrypt_generic_init ($td, $key, $iv);
$eid = base64_encode(mcrypt_generic ($td, $id));
mcrypt_generic_deinit($td);
return $eid;
}
Function decode($eid){
$id = $iv = 0;
$td = mcrypt_module_open (MCRYPT_TripleDES, "", MCRYPT_MODE_ECB, "");
$key = substr($this->secret, 0, mcrypt_enc_get_key_size ($td));
$iv = pack("a".mcrypt_enc_get_iv_size($td),$iv);
mcrypt_generic_init ($td, $key, $iv);
$id = mdecrypt_generic ($td, base64_decode($eid));
$len = strtok($id,'-');
$id = substr($id,(strlen($len)+1),$len);
mcrypt_generic_deinit($td);
return $id;
}
}
$code = new encrypt_class();
foreach ($data as $dt)
{

$CRYPT[$m] = $code->encode($dt);


$DECRYPT[$m] = $code->decode($CRYPT[$m]);

echo "crypt_".$CRYPT[$m]."_<br />\n";
echo "decrypt_".$DECRYPT[$m]."_<br />\n";

$m++;
}
?>

shell script decode.php 'encrypted string'

#!/usr/bin/php
<?
Function decode($eid){
$secret = 'test';
$id = $iv = 0;
$td = mcrypt_module_open (MCRYPT_TripleDES, "", MCRYPT_MODE_ECB, "");
$key = substr($secret, 0, mcrypt_enc_get_key_size ($td));
$iv = pack("a".mcrypt_enc_get_iv_size($td),$iv);
mcrypt_generic_init ($td, $key, $iv);
$id = mdecrypt_generic ($td, base64_decode($eid));
$len = strtok($id,'-');
$id = substr($id,(strlen($len)+1),$len);
mcrypt_generic_deinit($td);
return $id;
}
echo decode($argv[1])
?>




--
regards,
Tom
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 08:51 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0