Hi,

Wednesday, December 4, 2002, 3:39:10 PM, you wrote:
>>Is there a minimum field size for using mcrypt?

SY>     Boy I feel dumb now.  :)  My answer was in my post.  Mcrypt returns a
SY> string that is usually longer than the original string, since the return has
SY> to be a multiple of the block size used. So a 2-character string takes
SY> "blocksize" characters when encrypted.  Mcrypt also apparently needs the
SY> extra characters when decrypting.

SY>  - Steve Yates
SY>  - If at first you don't succeed, lower your standards.

SY> ~ Taglines by Taglinator - www.srtware.com ~

Here is a class I made that will handle any size string and always returns the
same string that was encoded.

class encrypt_class{
        var $secret;
        function encrypt_class(){
                $this->secret = 'this is a very long key, even too long for me';
        }
        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;
        }
}









-- 
regards,
Tom


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to