Speedy encryption and decryption

Hi all,

I need a really fast way to encrypt and decrypt json strings.
I’m storing some proprietary information in a database that has to be open so that some of the tables and rows can be used by anyone.
Since there can be a ton of these little json strings I can’t have it bog down too much.

Suggestions?

CipherMBS class

1 Like

Thanks Christian. Looking into it now.

The classes in my M_Crypto module are pretty fast and we use them in production every day, if MBS isn’t an option.

3 Likes

MBS is always an option, but I may check out yours as well.
Thanks for the response.

That class by Kem Tekinay is abselutely briliant!! For speedy encryption and decryption of JSON, specifically if large amount of text, I recommend his class, and using Blowfish enc/decryption.

p.s. THANK YOU KEM for the class!!!

1 Like

Hi @Chris_Halford

Maybe you want to take a look also into ARC4 implementation using Xojo code (fast and secure enough for many purposes).

1 Like

Eh, I’d use Blowfish over ARC4 given that enough vulnerabilities have been found with ARC4 for Mozilla and Microsoft to recommend it be disabled, and IETF prohibits it entirely. I mean, why bother when Blowfish is so readily available?

3 Likes

@Javier_Menendez can ARC4 be used in IOS? I tried to duplicate the desktop example and I get an error that the type “MobileTextArea” has no member named “value”. I tried to replace it with .text but I don’t get anything.

Public Function RC4(strData 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

Here’s the original thread where we did some benchmarking and got over a 20x speedup using pure Xojo code:

1 Like

Has anyone written this using API2? I am getting a lot of errors trying to compile in IOS

Michel’s version is old, you can use this function which works fine on iOS and is highly optimized for speed:

Public Function RC4v6(dataString as string, keyString as String) As String
  // highly optimized version of the RC4 algorithm written for Xojo
  // uses pointers and MemoryBlocks for speed
  // written to follow the pseudo-code algorithm described here:  http://en.wikipedia.org/wiki/Rc4
  #Pragma DisableBackgroundTasks
  #Pragma DisableBoundsChecking
  #Pragma NilObjectChecking False
  #Pragma StackOverflowChecking False
  
  
  Dim mbPlaintext as MemoryBlock = dataString  // input data
  dim Plaintext as Ptr = mbPlaintext // a pointer, used for speed
  
  Dim mbciphertext as New MemoryBlock(mbPlaintext.Size)    // output data, same size as input
  dim ciphertext as Ptr = mbciphertext // a pointer, used for speed
  
  dim mbKey as MemoryBlock = keyString  // the key, as a MemoryBlock
  dim Key as Ptr = mbKey
  
  dim keylength as integer = mbKey.size
  
  
  // do the Key Scheduling Algorithm (KSA)
  dim mbS as new MemoryBlock(256)
  dim S as Ptr = mbS // a pointer, used for speed
  
  // first, fill it with Identity (0-255)
  for i as integer = 0 to 255
    S.byte(i) = i
  next
  
  // now, do the KSA
  dim i,j as integer
  for i = 0 to 255
    j = (j + S.byte(i) + Key.byte(i mod keylength) ) mod 256
    // swap values of S[i] and S[j]
    dim tmp as Byte = S.byte(j)
    S.byte(j) = S.byte(i)
    S.byte(i) = tmp
  next
  
  
  // now, do the encoding
  i = 0
  j = 0
  
  dim U as integer = mbPlaintext.Size-1 // iterate from 0...U
  for x as integer = 0 to U
    i= (i + 1 ) mod 256
    j = (j + S.byte(i) ) mod 256
    // swap values of S[i] and S[j]
    dim tmp as Byte = S.byte(j)
    S.byte(j) = S.byte(i)
    S.byte(i) = tmp
    
    // K is the keystream value which is XORed with the Plaintext to make the ciphertext
    dim K as Byte  = S.byte( (S.byte(i) + S.byte(j))  mod 256)
    
    ciphertext.byte(x) = Plaintext.byte(x) XOR K
  next
  
  return mbciphertext
  
End Function

Sorry I am trying it but get this error. Not clear about the encoding though.

“The data could not be converted to text with this encoding.”

I am using this to call up the function
Var vstring as string

vstring=SourceDataArea.Text

TargetDataArea.Text= RC4(vString,“Mirnaarissa123”).ToText

Thanks

Why are you using ToText on the last line?

Is your TextArea an iOSTextArea?
Changing it to MobileTextArea and removing “.ToText” should do the trick

targetsourcearea and sourcedataarea are both mobile text area but I get the error “The data could not be converted to text with this encoding.”

I know it has to do with text vs string. I am not understanding it quite right in IOS

Looks like this issue
https://tracker.xojo.com/xojoinc/xojo/-/issues/70980

1 Like

Thank you Jeremie. Hopefully they resolve the issue soon to see if i can implement it. Quick question, do you think it will work the same in windows? or will it have different results. Another words, I am trying to use the same sqlite database on both web app (running in a windows machine) and ios and I want to encrypt the password field.

I had used another method I found here but is giving different results between platform

The code I presented “RC4v6” has been tested by me in a few ways:

  • it delivers the correct byte sequence with known test vectors, e.g. if you encrypt the string “Plaintext” with the key “Key” you will get a string with these bytes: BBF316E8D940AF0AD3 (see RC4 - Wikipedia for other tests)
  • it works the same on macOS and Windows
  • I have not tested it under iOS, but I believe it should work the same. If you are seeing differences, my first guess would be that RC4 is working fine, but you are having encoding issues with your Text/String conversions…

thanks mike. it has to be that bug in ios