Re: [PHP] Padding with mcrypt_generic

2002-01-15 Thread Joe Conway (wwc)

Ben Sinclair wrote:

 That would work for me, but I have to deal with many files that I have already
 encrypted and no longer know the correct sizes of. My search and replace for
 the padding characters doesn't work because the files sometimes contain those
 padding characters.
 
 --
 Ben Sinclair
 [EMAIL PROTECTED]

Well, if your original files ended in NUL bytes you are out of luck, but 
I don't think that is likely. You should be able to simply decrypt and 
strip trailing NUL bytes to get the original files back. See example below.

-- Joe
=

?PHP
print (HTMLBODY\n);

$plaintext = 123456789;
echo Plaintext =  . $plaintext . BR;
echo Plaintext length =  . strlen($plaintext) . BR;

/*
  * open the desired module
  */
$td = mcrypt_module_open (MCRYPT_TRIPLEDES, , MCRYPT_MODE_CBC, );

/*
  * Just for illustration, a real iv should be random of course
  */
$iv = ;

/*
  * and I hope a better password is actually used
  */
$key = mysecret;

/*
  * initialize the module structures
  */
$ret = mcrypt_generic_init($td, $key, $iv);

/*
  * finally encrypt it
  */
$ciphertext = mcrypt_generic($td, $plaintext);

echo Ciphertext length =  . strlen($ciphertext) . BR;

/*
  * get ready for decryption
  */
$ret = mcrypt_generic_init($td, $key, $iv);

/*
  * now decrypt
  */
$newplaintext = mdecrypt_generic($td, $ciphertext);

$ptr = strlen($newplaintext);
echo New plaintext length =  . $ptr . BR;

while (substr($newplaintext, $ptr - 1, 1) == chr(0))
{
$ptr--;
}

$origplaintext = substr($newplaintext, 0, $ptr);
echo Origplaintext =  . $origplaintext . BR;
echo Origplaintext length =  . strlen($origplaintext) . BR;

print (/BODY/HTML\n);
?



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] Padding with mcrypt_generic

2002-01-14 Thread Joe Conway (wwc)

Ben Sinclair wrote:

 I'm trying to use mcrypt_generic and it works fine except it pads the data
 with ^@'s when, according to the manual page, the length of the data is not
 n * blocksize.
 
 I've been trying to remove the padding from my decrypted data using
 something like $string = str_replace(^@,,$string);, but it doesn't
 seem to work right (^@ is a single character, not just ^ . @).
 
 Has anyone had to do this before and found a solution?
 
 --
 Ben Sinclair
 [EMAIL PROTECTED]

I worked around this by padding the plaintext myself. Basically, add NUL 
(character 0) bytes so that your plaintext becomes an exact multiple of 
blocksize. Then change the very last byte to the number of padding bytes 
used. If the plaintext is already an exact multiple of blocksize, then 
pad with an entire block.

On decryption, reverse the process, and you'll have your original string 
back exactly the way you started.

Here are two functions that I use for this:
(note: I modified these slightly from the originals without testing)

function pad_plaintext($data, $blocksize)
{
 $buffer = ;
 $numpad = $blocksize - (strlen($data) % $blocksize);

 $nul_str = chr(0);
 $buffer = $data . str_repeat($nul_str, $numpad);
 $buffer[strlen($buffer) - 1] = chr($numpad);

 return($buffer);
}

function depad_plaintext($data)
{
 $buffer = ;
 $numpad = ord($data[strlen($data) - 1]);

 $buffer = substr($data, 0, -1 * $numpad);

 return($buffer);
}

HTH,

--Joe


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]