fwPack/fwUnpack in php?
shaosean at unitz.ca
shaosean at unitz.ca
Tue Mar 2 00:24:49 EST 2004
well here are the two functions.. next ;-)
-Sean
<%
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