python - 使用 VBScript 和 Python 的 RC4

标签 python encryption vbscript cryptography rc4-cipher

我一直在尝试学习 VBScript,并给自己带来了一些挑战。我想在 VBScript 中创建一个函数,在 RC4 中加密某些内容,并在 Python 中解密(本质上是相同的过程/算法,但我想验证它是否正确)。

对于我的 python 代码,我有以下内容:

def rc4crypt(data, key):
    x = 0
    box = range(256)
    for i in range(256):
        x = (x + box[i] + ord(key[i % len(key)])) % 256
        box[i], box[x] = box[x], box[i]
    x = 0
    y = 0
    out = []
    for char in data:
        x = (x + 1) % 256
        y = (y + box[x]) % 256
        box[x], box[y] = box[y], box[x]
        out.append(chr(ord(char) ^ box[(box[x] + box[y]) % 256]))

    return ''.join(out)

据我从互联网研究中得知,这是实现 RC4 的一种非常标准的方法。然而,当涉及到 VBScript 时,我却遇到了一些困难。

这是迄今为止我的代码:

Function RunRC4(sMessage, strKey)
    Dim kLen, x, y, i, j, temp, l
    Dim s(256), k(256)

    'Init keystream
    kLen = Len(strKey)
    For i = 0 To 255
        s(i) = i
        l = Mid(strKey, (i Mod kLen) + 1, 1)
        k(i) = Asc(Mid(strKey, (i Mod kLen) + 1, 1))

    Next

    j = 0
    For i = 0 To 255
        j = (j + k(i) + s(i)) Mod 256
        temp = s(i)
        s(i) = s(j)
        s(j) = temp
    Next

    'Encode/Decode
    For i = 1 To Len(sMessage)
        x = (x + 1) Mod 256
        y = (y + s(x)) Mod 256
        temp = s(x)
        s(x) = s(y)
        s(y) = temp

        temp1 = Asc(Mid(sMessage, i, 1))
        temp2 = Chr(s((s(x) + s(y)) Mod 256))
        RunRC4 = RunRC4 & Chr(temp1 Xor temp2)
    Next
End Function

这与许多其他帖子非常相似。我发现了一些相关的帖子,人们提出了类似的问题,但并不完全是我正在寻找的答案:

Encrypt a string in java and decryption in VBScript using RC4

RC4 decryption with key in Python

正如您可能知道的那样,两者都使用非常相似的脚本。但在尝试解码时似乎都无法正常工作。我还查看了其他地方,包括类似的算法:

https://bytes.com/topic/access/insights/906671-rc4-encryption-algorithm-vba-vbscript

有人可以帮忙吗?最好知道您是否能够成功运行每个函数。理想情况下,目标是使用 VBScript 运行加密,获取输出并使用 Python 解密,并获得预期的原始结果。

最佳答案

问题出在编码上。只需查看 Python 文档 chr() function

Returns a character (a string) from an integer (represents unicode code point of the character)

而在 VBScript 中 Chr() function

Returns the character associated with the specified ANSI character code

这并不等效,您需要在 VBScript 中使用 ChrW() 函数。

ChrW is provided for 32-bit platforms that use Unicode characters. Its argument is a Unicode (wide) character code, thereby avoiding the conversion from ANSI to Unicode.

此外,返回字符代码时,您需要使用 AscW() 函数而不是 Asc() function相当于Python ord() function .

AscW is provided for 32-bit platforms that use Unicode characters. It returns the Unicode (wide) character code, thereby avoiding the conversion from Unicode to ANSI.

<小时/>

有用的链接

关于python - 使用 VBScript 和 Python 的 RC4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54896169/

相关文章:

python - 如何根据元组中的值删除列表中的元组?

machine-learning - 用于文件解密的神经网络 - 可能吗?

windows - 在 Linux 和 Windows 中使用 Base64 转换文本

arrays - For Next 循环中每次迭代 VBS 中的新数组

VBScript 将所有文件从一个文件夹移动到另一个文件夹

javascript - 为 Enterprise Architect 编写脚本时,使用 VBScript 还是 JavaScript 更方便?

python - 关于将 bool 值与 True 或 False 进行比较的奇怪 PEP8 建议

Python xlrd 异常 : Unknown cell type 'n+'

python - 从数组的每一行创建一个对象

ruby-on-rails - 如何为密码添加基本加密?