[futurebasic] FN SCRAMBLE$/UNSCRAMBLE$

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : October 2001 : Group Archive : Group : All Groups

From: Ken Shmidheiser <k.shmidheiser@...>
Date: Wed, 24 Oct 2001 20:12:20 -0400
I have hit a maddening problem with these functions:

They run correctly and even compile a working app on my B&W G3 
running OS 9.2.1 with FB^3 Release 5. The compiled app runs correctly 
on Alain's Mac in France. But for Alain and others, the code produces 
unprintable characters.

I would like to ask you list members to take a moment and run this 
code and report the output.

Right now we're scratching our heads. The problem appears to be in 
this line in each function which is based on the XOR shortcut found 
in the second to last item in the chart Page 554 of the Reference 
manual:

digit     = digit^^digitMask

If I substitute this:

digit     = digit ^^ digitMask // Note spaces

It breaks. This also breaks:

digit     = digit XOR digitMask

I promise if you help me with this, I will write a nice encrypted 
password demo for the list to show my appreciation.

Thanks,

Ken

Please watch for e-mail line breaks and lost underscores. This runs 
in the Standard BASIC runtime.

'--------- BEGIN FB^3 CODE -------------

CLEAR LOCAL FN SCRAMBLE$( rawStr AS STR255, keyStr AS STR255 )
DIM digit     AS INTEGER
DIM digitSum  AS INTEGER
DIM digitMask AS INTEGER
DIM i         AS INTEGER
DIM rawLen    AS INTEGER
DIM scramStr  AS STR255
DIM maskStr   AS STR255

digitSum = 0

FOR i = 1 TO keyStr[0]
digitSum  = digitSum  + ASC( MID$( keyStr, i, 1 ) )
NEXT i

digitSum = digitSum MOD 128
scramStr = ""
rawLen   = rawStr[0]
maskStr  = keyStr

WHILE maskStr[0] < rawLen
maskStr = maskStr + keyStr
WEND

// To scramble = digit + digitsum of key XOR character of key
FOR i = 1 TO rawStr[0]
digit     = ASC( MID$( rawStr, i, 1 )) + digitSum
digitMask = ASC( MID$( maskStr, i, 1 ) )
digit     = digit^^digitMask
'digit     = digit XOR digitMask
scramStr  = scramStr + CHR$( digit )
NEXT i

END FN = scramStr


CLEAR LOCAL FN UNSCRAMBLE$( scramStr AS STR255, keyStr AS STR255 )
DIM digitSum  AS INTEGER
DIM i         AS INTEGER
DIM scramLen  AS INTEGER
DIM digit     AS INTEGER
DIM digitMask AS INTEGER
DIM maskStr   AS STR255
DIM rawStr    AS STR255

rawStr   = ""
digitSum = 0

FOR i = 1 TO keyStr[0]
digitSum = digitSum + ASC( MID$( keyStr, i, 1 ) )
NEXT i

digitSum = digitSum MOD 128
scramLen = scramStr[0]
maskStr  = keyStr

WHILE maskStr[0] < scramLen
maskStr = maskStr + keyStr
WEND

// To unscramble = digitsum of key XOR character of key - digit
FOR i = 1 TO scramStr[0]
digit     = ASC( MID$( scramStr, i, 1 ) )
digitMask = ASC( MID$( maskStr, i, 1 ) )
digit     = digit^^digitMask
'digit     = digit XOR digitMask
digit     = digit - digitSum
rawStr    = rawStr + CHR$( digit )
NEXT i

END FN = rawStr

WINDOW 1

DIM myStr AS STR255
DIM kStr  AS STR255

myStr = "futurebasic"
kStr  = "alain"

PRINT:PRINT "The word to be scrambled is ""futurebasic""
PRINT "The secret key word is ""alain""
PRINT:PRINT "The scrambled word is:"
PRINT:PRINT FN SCRAMBLE$( myStr, kStr )

PRINT:PRINT

myStr = FN SCRAMBLE$( myStr, kStr )
PRINT:PRINT "The word to be unscrambled is """; mystr; """
PRINT "The secret key word is ""alain""
PRINT:PRINT "The unscrambled word is:"
PRINT:PRINT FN UNSCRAMBLE$( myStr, kStr )

PRINT:PRINT
PRINT:PRINT "Click mouse to end."

DO
HANDLEEVENTS
UNTIL FN BUTTON

'---------- END CODE ----------