TGC Codebase Backup



XOR Hex Encryption/Decryption by Flatlander

28th Mar 2011 22:48
Summary

Allows you to encrypt and decrypt data strings.



Description

This code will allow you to enter a password or code key plus a data string of characters you wish to have encrypted/decrypted.

What the encryption function does is turn each character into a hex value, then gets the hex value of the password, XOR the data hex value with the hex value of the password and passes it back as a variable.

What the decryption function does is simply reverses the process.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    global code$ as string
global data$ as string
global lendata as integer
global lencodekey as integer

if file exist("log.txt")
   delete file "log.txt"
endif
open to write 15, "log.txt"

Code$ = "password"
data$ = "Test Data"

lendata = len(data$)
lencodekey = len(code$)

strXOREncryption$ = xorencryption(code$,data$,lendata,lencodekey)
print strXOREncryption$
write string 15,strXOREncryption$
strXORDecryption$ = XORDecryption(code$,strXOREncryption$,lendata,lencodekey)
print strXORDecryption$
write string 15,strXORDecryption$
wait key
end


Function XOREncryption(CodeKey$ As String, DataIn$ As String, DataInSize as integer, CodeKeySize as integer)
    
    local DataPtr As integer
    local strDataOut$ As String
    local temp As Integer
    local tempstring As String
    local intXOrValue1 As Integer
    local intXOrValue2 As Integer
    local strXOREncryption$ as string

    For DataPtr = 1 to DataInSize
        `The first value to be XOr-ed comes from the data to be encrypted
        intXOrValue1 = Asc(Mid$(DataIn$, DataPtr))
        `The second value comes from the code key
        intXOrValue2 = Asc(Mid$(CodeKey$, (DataPtr Mod CodeKeySize) + 1))
        temp = (intXOrValue1 xor intXOrValue2)
        if temp<16
           strDataOut$ = strDataOut$ + "0"+Hex$(temp)
        else     
            strDataOut$ = strDataOut$ + Hex$(temp)
        endif
    Next DataPtr
    
EndFunction strDataOut$

Function XORDecryption(CodeKey$ As String, DataIn$ as string, DataInSize as integer, CodeKeySize as integer)
    
    local DataPtr As integer
    local strDataOut$ As String
    local intXOrValue1 As Integer
    local intXOrValue2 As Integer
    local strxordecryption$ as string
    local temp1$ as string
    local temp as integer
    local strXOREncryption$ as string
    
    For DataPtr = 1 to DataInSize
        `The first value to be XOr-ed comes from the data to be decrypted
        temp1$ = Mid$(DataIn$, (2 * DataPtr) - 1)
        temp1$ = temp1$ + Mid$(DataIn$, (2 * DataPtr))
        temp = hex to decimal( temp1$ )
        intXOrValue1 = temp
        `The second value comes from the code key
        temp1$ = Mid$(CodeKey$, (DataPtr Mod CodeKeySize) + 1)
        intXOrValue2 = Asc(temp1$)
        
        strDataOut$ = strDataOut$ + Chr$(intXOrValue1 xor intXOrValue2)
    Next DataPtr
EndFunction strDataOut$