Codebank: Basic Xor encryptor for Xojo

Guys
Can someone translate this into Xojo, I already tried but I couldnt. I get too many errors…
Thanks in advance

’ Name: Simple Xor Encryption
’ Description:This is a simple Xor Encryption routine from MSDN. Very simple, not earth shattering, but it does the job. The same function will encrypt and decrypt if the passwords are the same. Remember, this code is not meant to be uncrackable. It is a very simple way of keeping the newbie hackers and end users out of your files. Please don’t go bitching and moaning about “how primitive” Xor encryption is…that’s a broken record. Again, this is Microsoft’s code from the MSDN CDs so don’t bother voting.
’ By: Paul Mather

[code]Public Function Encrypt(ByVal inputString As String, ByVal PassWord As String) As String
Dim L As Integer
Dim X As Integer
Dim Char As String

'Encrypt(“Hello”,“123”) = “yW_]]”
'Encrypt(“yW_]]”,“123”) = “Hello”
’ inputString = the string you wish to encrypt or decrypt.
’ PassWord = the password with which to encrypt the string.
L = Len(PassWord$)
For X = 1 To Len(inputString)
Char = Asc(Mid$(PassWord$, (X Mod L) - L * ((X Mod L) = 0), 1))
Mid$(inputString, X, 1) = Chr$(Asc(Mid$(inputString, X, 1)) Xor Char)
Next
Encrypt = inputString
End Function[/code]

  Dim L As Integer
  Dim X As Integer
  Dim Char As integer
  
  'Encrypt("Hello","123") = "yW_]]"
  'Encrypt("yW_]]","123") = "Hello"
  ' inputString = the string you wish to encrypt or decrypt.
  ' PassWord = the password with which to encrypt the string.
  L = Len(PassWord)
  dim chars() as string
  
  dim pwIDX as integer = 1
  For X = 1 To Len(inputString)
    Char = Asc(Mid(PassWord, pwIDX , 1))
    pwIDX = pwIDX + 1
    if pwIDX > L then pwIDX = 1
    
    dim inputChar as string = Mid(inputString, X, 1)
    chars.append Chr(Asc(inputChar) Xor Char)
    
  Next
  
  return Join(chars,"")

thanks Norman !

If someone wants to use this simple xor encryptor , remember that the password must be at least as long as the text to encrypt.
peace out

Now Im changing the thread title so if someone else wants to search for this encryptor he can find it

It would be good practice to do that but XOR “encryption” isn’t really decent for anything beyond making somehing slightly harder to read as plain text

This code will handle a password thats shorter but it is mostly “forum code” so its probably not quite really robust (ie/ I suspect an empty password would make this blow up)

the RC4 function described here is far better and still open.
http://forums.realsoftware.com/viewtopic.php?p=204303#p204303

That Rc4 code doesnt work. I tested it and it’s not a simmetrical encriptor.

I use it in password encryption and it works reversal for months …

RC4 code works perfectly if implemented properly (I use it quite a lot)
and it is Symetrical… same routine encodes and decodes

FUNCTION RC4(data as string, strKey as string) as string

  #If Not DebugBuild
    #pragma DisableBackgroundTasks
    #pragma DisableBoundsChecking
    #pragma DisableAutoWaitCursor
    #pragma StackOverflowchecking False
    #pragma NilObjectChecking False
  #EndIf
  Dim MM As MemoryBlock = strData
  Dim MM2 As New MemoryBlock(LenB(strData))
  Dim memAsciiArray(255) As Integer
  Dim memKeyArray(255)   As Integer
  Dim memJump As Integer
  Dim memTemp As Integer
  Dim memY As Integer
  Dim intKeyLength As Integer
  Dim intIndex As Integer
  Dim intT As Integer
  Dim intX As Integer
  
  intKeyLength = Len(strKey)
  
  For intIndex = 0 To 255
    memKeyArray(intIndex) = Asc(Mid(strKey, ((intIndex) Mod (intKeyLength)) + 1, 1))
  Next
  
  For intIndex = 0 To 255
    memAsciiArray(intIndex) = intIndex
  Next
  
  For intIndex = 0 To 255
    memJump = (memJump + memAsciiArray(intIndex) + memKeyArray(intIndex)) Mod 256
    memTemp = memAsciiArray(intIndex)
    memAsciiArray(intIndex) = memAsciiArray(memJump)
    memAsciiArray(memJump) = memTemp
  Next
  
  intIndex = 0
  memJump = 0
  
  For intX = 1 To MM2.Size
    intIndex = (intIndex + 1) Mod 256
    memJump = (memJump + memAsciiArray(intIndex)) Mod 256
    intT = (memAsciiArray(intIndex) + memAsciiArray(memJump)) Mod 256
    memTemp = memAsciiArray(intIndex)
    memAsciiArray(intIndex) = memAsciiArray(memJump)
    memAsciiArray(memJump) = memTemp
    memY = memAsciiArray(intT)
    mm2.Byte(intX - 1) = bitwise.bitxor(Val("&h" + Hex(MM.Byte(IntX - 1))), bitwise.bitxor(memTemp,memY))
  Next
  
  Return MM2

END FUNCTION RC4

We had a prior discussion with ways to optimize RC4 written in native Xojo code. Check it out (Version 6 is the one to use):

https://forum.xojo.com/13818-faster-encyrption-obscuration-than-rc4-for-large-24mb-15mb-file