Re: Padding with mcrypt_generic

From: Date: Tue, 15 Jan 2002 17:26:45 +0000
Subject: Re: Padding with mcrypt_generic
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-80591@lists.php.net to get a copy of this message
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 ben@yourwebwill.com
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 ("<HTML><BODY>\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 = "99999999"; /* * 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"); ?>

« previous php.general (#80591) next »