Android Question Conversion from RC4 text to RC4 byte array

synasir

Member
Licensed User
Longtime User
This is sample code by PSDOS. It only accepts text input. How do I convert it to accept byte array and output byte array? This is for file encryption. Thanks.

From

Sub rc4(message As String, password As String) As String

to

Sub rc4(filebytes() As bytes, password As String) As bytes()


Thanks.


B4X:
 Sub rc4(message As String, password As String) As String

      Dim i As Int
      i = 0
        Dim j As Int
      j = 0
        Dim cipher As StringBuilder
      cipher.Initialize
        Dim returnCipher As String
      returnCipher = ""
    
        Dim sbox(256) As Int
        Dim key(256) As Int

        Dim intLength As Int
      intLength = password.Length

        Dim a As Int
      a = 0
        Do While a <= 255

            Dim ctmp As Char
         ctmp = password.SubString(a Mod intLength)
            key(a) = Asc(ctmp)
            sbox(a) = a
            a = a + 1
        Loop

        Dim x As Int
      x = 0

        Dim b As Int
      b = 0
        Do While b <= 255
            x = (x + sbox(b) + key(b)) Mod 256
            Dim tempSwap As Int
         tempSwap = sbox(b)
            sbox(b) = sbox(x)
            sbox(x) = tempSwap
            b = b + 1
          Loop

        a = 1

        Do While a <= message.Length

            Dim itmp As Int
         itmp = 0

            i = (i + 1) Mod 256
            j = (j + sbox(i)) Mod 256
            itmp = sbox(i)
            sbox(i) = sbox(j)
            sbox(j) = itmp

            Dim k As Int
         k = sbox((sbox(i) + sbox(j)) Mod 256)

            Dim ctmp As Char
         ctmp = message.Substring(a - 1)

            itmp = Asc(ctmp)

            Dim cipherby As Int
         cipherby = Bit.Xor(itmp, k)

            cipher.Append(Chr(cipherby))
            a = a + 1
        Loop

        returnCipher = cipher.ToString
      
      Return returnCipher

    End Sub
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…