我一直在找VB的RC4源码,找了很多但没有一个可以正确运行的。请帮我指出下面的错误或提供正确的源码!Public Function Encrypt(ByVal strPassword As String, ByVal strKey As String) As String  Dim i As Long, j As Long, K As Long, x As Long
  Dim KeyLen As Long, PLen As Long
  Dim temp As Byte
  
  Dim PByteArray() As Byte, KByteArray() As Byte, TByteArray(0 To 255) As Byte
  
  KByteArray = StrConv(strKey, vbFromUnicode)
  KeyLen = UBound(KByteArray) + 1
  
  For i = 0 To 255
    TByteArray(i) = i
  Next
  
  For i = 0 To 255
    j = (j + TByteArray(i) + KByteArray(i Mod KeyLen)) Mod 256
    temp = TByteArray(i)
    TByteArray(i) = TByteArray(j)
    TByteArray(j) = temp
  Next
  
  PByteArray = StrConv(strPassword, vbFromUnicode)
  PLen = UBound(PByteArray)
  
  i = 0
  j = 0
  For x = 0 To PLen
    i = (i + 1) Mod 256
    j = (j + TByteArray(i)) Mod 256
    temp = TByteArray(i)
    TByteArray(i) = TByteArray(j)
    TByteArray(j) = temp
    PByteArray(x) = PByteArray(x) Xor TByteArray((TByteArray(i) + TByteArray(j)) Mod 256)
  Next
  Encrypt = StrConv(PByteArray, vbUnicode)
  
End Function

解决方案 »

  1.   

    public sub main() 
    dim key as string 
    for i = 1 to 16 
        randomize 
        key = key & chr(rnd * 255) 
    next i 
    msgbox rc4(rc4("welcome to plindge studio!", key), key) 
    end sub 
    public function rc4(inp as string, key as string) as string 
    dim s(0 to 255) as byte, k(0 to 255) as byte, i as long 
    dim j as long, temp as byte, y as byte, t as long, x as long 
    dim outp as string for i = 0 to 255 
        s(i) = i 
    next j = 1 
    for i = 0 to 255 
        if j > len(key) then j = 1 
        k(i) = asc(mid(key, j, 1)) 
        j = j + 1 
    next i j = 0 
    for i = 0 to 255 
        j = (j + s(i) + k(i)) mod 256 
        temp = s(i) 
        s(i) = s(j) 
        s(j) = temp 
    next i i = 0 
    j = 0 
    for x = 1 to len(inp) 
        i = (i + 1) mod 256 
        j = (j + s(i)) mod 256 
        temp = s(i) 
        s(i) = s(j) 
        s(j) = temp 
        t = (s(i) + (s(j) mod 256)) mod 256 
        y = s(t) 
         
        outp = outp & chr(asc(mid(inp, x, 1)) xor y) 
    next 
    rc4 = outp 
    end function