Re: Re: web-based and command line mcrypting and back again
| From: | Tom Rogers | Date: | Wed, 07 Jan 2004 22:23:04 +0000 |
| Subject: | Re: Re: web-based and command line mcrypting and back again | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-174013@lists.php.net to get a copy of this message | ||
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