[futurebasic] FN XORCrypt$ (One-function encryption)

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

From: Ken Shmidheiser <k.shmidheiser@...>
Date: Sat, 27 Oct 2001 20:51:06 -0400
Here is a variation on a theme. FN XORCrypt$ is a XOR encryption 
scheme that uses just one function to both encrypt and decrypt a 
string. The function works by passing a BOOLEAN value of _true for 
encryption and _false for decryption.

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

// Contributed by Ken Shmidheiser 10-27-01

CLEAR LOCAL FN XORCrypt$( txtStr AS STR255, encrypt As Boolean )
Dim x           As Integer
Dim iLen        As Integer
Dim iSeed       As Integer
Dim outStr      AS STR255
Dim sFirstSeed  AS STR255
Dim sSecondSeed AS STR255

On Error GoTo "errhandler"
IF txtStr[0] = 0 THEN EXIT FN

LONG IF encrypt
  sFirstSeed = Left$(txtStr, 1)
sSecondSeed = Mid$(txtStr, 2, 1)
       iSeed = (Asc(sFirstSeed) + Asc(sSecondSeed)) Mod 2
        iLen = Len(txtStr)

For x = 1 To iLen
outStr = Chr$((Asc(Mid$(txtStr, x, 1)) Xor iSeed) + 2) + outStr
Next x

outStr = Chr$(Asc(sFirstSeed) * 2 + 3) + outStr
outStr = outStr + Chr$(Asc(sSecondSeed) * 2 - 3)

XELSE

  sFirstSeed = Chr$((Asc(Left$(txtStr, 1)) - 3) \ 2)
sSecondSeed = Chr$((Asc(Right$(txtStr, 1)) + 3) \ 2)
       iSeed = (Asc(sFirstSeed) + Asc(sSecondSeed)) Mod 2
        iLen = Len(txtStr) - 1

For x = 2 To iLen
outStr = Chr$((Asc(Mid$(txtStr, x, 1)) Xor iSeed) - 2) + outStr
Next x

END IF

EXIT FN

"errhandler"

outStr = ""

END FN = outStr

Dim AS STR255 textToEncode, textEncoded, textDecoded
Dim AS BOOLEAN encrypt, decrypt

textToEncode = "futurebasic"
encrypt = _true
decrypt = _false

textEncoded = FN XORCrypt$( textToEncode, encrypt )
textDecoded = FN XORCrypt$( textEncoded,  decrypt )

PRINT:PRINT "The text to be encoded is:"
PRINT textToEncode
PRINT:PRINT "Our encoded word is:"
PRINT textEncoded
PRINT:PRINT "Our decoded word is:"
PRINT textDecoded

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