on EncryptString( argMsg, argKey ) ---- -- FUNCTION: EncryptString() ---- -- Description: -- This function will modify a 'message' string based on -- the contents of a second 'key' string for the purposes -- of hiding any sensitive data from prying eyes when -- it is saved to the user's harddrive. ---- -- Parameters: -- argMsg: The message string to be encrypted. -- argKey: The key string used to garble the message -- string. The key string can be any length, -- but any more than 16 chars is probably -- overkill. The key string used to encrypt a -- message is also the exact same string needed -- to decrypt the message, therefore, make sure -- that the value of the key string will not -- change over time or between user sessions. ---- -- Returns: -- sOut: The garbled version of the incoming message -- string. ---- sOut = EMPTY iKeyLength = argKey.length iMsgLength = argMsg.length iKeyIndex = 1 -- Encrypt the string one character at a time. repeat with i = 1 to iMsgLength iMsgChar = CharToNum(argMsg.char[i]) iKeyChar = CharToNum(argKey.char[iKeyIndex]) sOutChar = NumToChar( BitXOR(iMsgChar,iKeyChar) ) put sOutChar after sOut -- Use the next character in the key string; -- wrap back to first char when the key length -- is exceeded. iKeyIndex = iKeyIndex + 1 if iKeyIndex > iKeyLength then iKeyIndex = 1 end repeat return sOut end EncryptString on EncodeString( argString ) ---- -- FUNCTION: EncodeString() ---- -- Description: -- This function prepares a text string so that -- it can be output with the SetPref() function. -- SetPref() will not work properly with strings -- containing certain non-alphanumeric characters. -- Passing a string through this function will -- reduce the data to 4-bits-per-character instead -- of 8 and will return the string using only -- characters that SetPref() can handle. ---- -- Parameters: -- argString: The incoming string to be encoded. ---- -- Returns: -- sOut: The encoded string. ---- iStrLength = argString.length sOut = EMPTY iHiMask = 15 -- Integer equivalent to the binary "00001111" iLoMask = 240 -- Integer equivalent to the binary "11110000" iValOffset = 97 repeat with i = 1 to iStrLength iCharVal = CharToNum( argString.char[i] ) iHiVal = BitNOT( BitOR( BitNOT( iCharVal ), iHiMask ) ) / 16 + iValOffset iLoVal = BitNOT( BitOR( BitNOT( iCharVal ), iLoMask ) ) + iValOffset put NumToChar( iHiVal ) & NumToChar( iLoVal ) after sOut end repeat return sOut end EncodeString on DecodeString( argString ) ---- -- FUNCTION: DecodeString() ---- -- Description: -- This function removes the encoding from -- a string created by the companion -- EncodeString() function. ---- -- Parameters: -- argString: The incoming string to be decoded. ---- -- Returns: -- sOut: The decoded string. ---- sOut = EMPTY iStrLength = argString.length iValOffset = 97 i = 1 repeat while i < iStrLength iHiVal = ( CharToNum( argString.char[i] ) - 97 ) * 16 iLoVal = CharToNum( argString.char[i+1] ) - 97 put NumToChar( iHiVal + iLoVal ) after sOut -- skip to the next character pair i = i + 2 end repeat return sOut end DecodeString