fwPack/fwUnpack in php?

Richard Gaskin ambassador at fourthworld.com
Tue Mar 2 01:20:12 EST 2004


shaosean at unitz.ca wrote:

> well here are the two functions.. next ;-)
> 
> -Sean
> 

A thing of beauty, Sean.  Thank you.

-- 
  Richard Gaskin
  Fourth World Media Corporation
  ___________________________________________________________
  Ambassador at FourthWorld.com       http://www.FourthWorld.com

> 
> <%
> 
> function fwPack($pData, $pPassword="") {
>   if ($pPassword === ""):
>     $pData = "00" . gzencode($pData);
> 
>   else:
>     $tKeyString = pack("H*", md5($pPassword));
>     $tKeyStringLen = strlen($tKeyString);
> 
>     $pData = gzencode($pData);
>     $tDataLen = strlen($pData);
> 
>     $tCryptoText = "";
>     $i = 0;
>     for ($k=0; $k < $tDataLen; $k++):
>       if ($i >= $tKeyStringLen):
>         $i = 0;
>       endif;
>       $tCryptoText .= chr( ord($pData{$k}) ^ ord($tKeyString{$i}) );
>       $i++;
>     endfor;
>     $pData = "01" . $tCryptoText;
>   endif;
>   return base64_encode($pData);
> }
> // end fwPack
> 
> 
> function fwUnpack($pData, $pPassword="") {
>   $pData = base64_decode($pData);
>   $tEncryptionMethod = $pData{0} . $pData{1};   // get the encryption 
> method
>   $pData = substr($pData,2);    // remove the encryption method from the 
> data
> 
>   switch($tEncryptionMethod):
>   case "00":   // no encryption
>     break;
>   case "01":   // md5 encryption
>     $tKeyString = pack("H*", md5($pPassword));
>     $tKeyStringLen = strlen($tKeyString);
> 
>     $tDataLen = strlen($pData);
> 
>     $tClearText = "";
>     $i = 0;
>     for ($k=0; $k < $tDataLen; $k++):
>       if ($i >= $tKeyStringLen):
>         $i = 0;
>       endif;
>       $tClearText .= chr( ord($pData{$k}) ^ ord($tKeyString{$i}) );
>       $i++;
>     endfor;
>     $pData = $tClearText;
>   endswitch;
> 
>   return gzinflate(substr($pData,10)); 
> }
> // end fwUnpack
> 
> 
> echo fwUnpack(fwPack("abraxas", "fred"), "fred");
> 
> %>
> 


More information about the use-livecode mailing list