The RC4 encryption algorithm of VB, which supports Chinese Characters

Source: Internet
Author: User
Recently, I need to write a small test program using VB, so I turned over the abandoned VB for a long time and installed a green version of 10 MB from the Internet. The RC4 algorithm is needed for testing in the code, so I searched the internet and the result was very disappointing. I could hardly use the code that was spread around the network, the algorithm should be correct, but the string is used as the function parameter, and the result is processed incorrectly. This error is still being spread everywhere. It's a huge copy of the article. No one has to make a simple test if there are so many posts? As a result, a program was simply written according to the C code, using a byte array to pass parameters. During the call, strconv can be used to convert the string and byte arrays, or it is no problem to use string directly without any trouble.

Option Base 0Public Type rc4_key    s(256) As Byte    x As Byte    y As ByteEnd TypePublic Sub prepare_key(ByRef key_data() As Byte, ByRef key As rc4_key)    Dim i As Long, j As Byte, keylen As Long, c As Integer      For c = 0 To 255        key.s(c) = c    Next      key.x = 0    key.y = 0        i = 0    j = 0    keylen = UBound(key_data) - LBound(key_data) + 1        For c = 0 To 255          j = ((key_data(i) Mod 256) + key.s(c) + j) Mod 256            key.s(c) = key.s(c) Xor key.s(j)      key.s(j) = key.s(c) Xor key.s(j)      key.s(c) = key.s(c) Xor key.s(j)            i = (i + 1) Mod keylen        Next    End SubPublic Sub rc4(ByRef buff() As Byte, ByRef key As rc4_key)    Dim x As Byte, y As Byte, z As Byte, c As Long, ub As Long, lb As Long      x = key.x    y = key.y    ub = UBound(buff)    lb = LBound(buff)    For c = lb To ub                x = (x + 1) Mod 256        y = ((key.s(x) Mod 256) + y) Mod 256                key.s(x) = key.s(x) Xor key.s(y)        key.s(y) = key.s(x) Xor key.s(y)        key.s(x) = key.s(x) Xor key.s(y)        z = ((key.s(x) Mod 256) + key.s(y)) Mod 256        buff(c) = buff(c) Xor key.s(z)            Next        key.x = x    key.y = yEnd Sub

Call method dim S () as byte, P () as byte dim enkey as rc4_key, denkey as rc4_key S = "Inherit Chinese" P = "123abc test" Call prepare_key (p, enkey) denkey = enkey call RC4 (S, enkey) Call RC4 (S, denkey) msgbox S. If multiple contents are encrypted, you can also perform segmentation decryption, or decrypt all the contents dim S () as byte, P () as byte dim enkey as rc4_key, denkey as rc4_key S = "Beijing Chinese" P = "123abc test" Call prepare_key (p, enkey) denkey = enkey call RC4 (S, enkey) dim S2 () as byte, s3 () as byte S2 = "Information abcd1234" Call RC4 (S2, enkey) redim S3 (0 to ubound (s) + ubound (S2) + 1) call copymemory (S3 (0), S (0), ubound (s) + 1) Call copymemory (S3 (ubound (s) + 1), S2 (0 ), ubound (S2) + 1) Call RC4 (S3, denkey) msgbox S3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.