include("../include.php"); writeHeader("RC4 Encryption", "Programming » Visual Basic 6 » Visual Basic 6 Code » Algorithms » ");?>
'========================================================================= ' clsRC4 ' Encrypts and decrypts data using the same function for both ' Very strong encryption ' Source: unknown '========================================================================= Option Explicit Dim sbox(255) Dim key(255) Private Sub RC4Initialize(strPwd) '========================================================================= ' This routine called by EnDeCrypt function. Initializes the ' sbox and the key array) '========================================================================= Dim tempSwap As String Dim a As Long Dim b As Long Dim intLength As Integer intLength = Len(strPwd) For a = 0 To 255 key(a) = Asc(Mid(strPwd, (a Mod intLength) + 1, 1)) sbox(a) = a Next b = 0 For a = 0 To 255 b = (b + sbox(a) + key(a)) Mod 256 tempSwap = sbox(a) sbox(a) = sbox(b) sbox(b) = tempSwap Next End Sub Public Function EnDeCrypt(plaintxt As String, psw As String) '========================================================================= ' This routine does all the work. Call it both to Encrypt ' and to Decrypt your data. '========================================================================= Dim temp As String Dim a As Long Dim i As Long Dim j As Long Dim k As Long Dim cipherby As Long Dim cipher As String i = 0 j = 0 RC4Initialize psw For a = 1 To Len(plaintxt) i = (i + 1) Mod 256 j = (j + sbox(i)) Mod 256 temp = sbox(i) sbox(i) = sbox(j) sbox(j) = temp k = sbox((sbox(i) + sbox(j)) Mod 256) cipherby = Asc(Mid(plaintxt, a, 1)) Xor k cipher = cipher & Chr(cipherby) Next EnDeCrypt = cipher End Function