RC4 Encryption


Attention! 👉Starting November 2024, BlitzCoder.org will now be BlitzBasic.org.

 

Tweet blitz3d code-archives algorithms
BlitzCoder

This Function encrypts And decrypts a String with the use of a key and RC4 Encryption.

The RC4 stream cipher is one of the most widely-used real-world cryptosystems. Since its development in 1980's, RC4 has been used in various software appli- cations and standard protocols such as Microsoft Office, Secure Socket Layer (SSL), Wired Equivalent Privacy (WEP).

Local pass$ = "mySecretKey"
Local msg$ = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."

Print ""
Print "Message before encryption: " + msg

Print ""

Local crypt$ = RC4(msg$, pass$)

Print "Message after encryption: " + crypt

Print ""
Local decrypt$ = RC4(crypt$,pass$)
Print "Message after decryption: " + decrypt

' This Function encrypts And decrypts a String with the use of a key
Function RC4$(inp$, key$)
    Local S[256] ' 255 byte Arrays
    Local K[256]

    Local i,j,t,temp,y
    Local Output$

    For i = 0 To 255
        S[i] = i
    Next

    j = 1
    For i = 0 To 255
        If j > key.length
            j = 1
        EndIf
        K[i] = Asc(Mid(key,j,1))
        j:+ 1
    Next

    j = 0
    For i = 0 To 255 '
        j = ( j + S[i] + K[i] ) & 255
        temp = S[i]
        S[i] = S[j]
        S[j] = temp
    Next

    i = 0
    j = 0
    For Local x = 1 To Len(inp)
        i = (i + 1) & 255
        j = (j + S[i]) & 255
        temp = S[i]
        S[i] = S[j]
        S[j] = temp
        t = (  S[i] + ( S[j] & 255 )  ) & 255
        y = S[t]
        Output:+ Chr(Asc(Mid(inp,x,1)) ~ Y)
    Next

    Return Output$
EndFunction

Reply To Topic (minimum 10 characters)

Please log in to reply