python十六进制输入中的RC4解密



我有一个程序,可以成功加密一段纯文本信息,并将加密的数据输出为十六进制。 我可以通过输入从程序中收到的十六进制数据和我使用的密钥(纯文本)来解密 http://www.fyneworks.com/encryption/rc4-encryption/index.asp

上的数据

我尝试完成的下一步是有两个输入,我可以在其中输入十六进制的加密消息和加密密钥(也是十六进制)。

我无法让程序的解密部分正常工作......

如果我输入密钥作为password,密文作为8c905b7c294a94c30422d81d552e在上面的网站上成功解密......它不起作用。

任何人都知道如何使用十六进制输入在 RC4 中进行解密?

# Global variables
state = [None] * 256
p = q = None
def setKey(key):
   ##RC4 Key Scheduling Algorithm (KSA)
    global p, q, state
    state = [n for n in range(256)]
    p = q = j = 0
    for i in range(256):
        if len(key) > 0:
            j = (j + state[i] + key[i % len(key)]) % 256
        else:
            j = (j + state[i]) % 256
        state[i], state[j] = state[j], state[i]
def byteGenerator():
    ##RC4 Pseudo-Random Generation Algorithm (PRGA)
    global p, q, state
    p = (p + 1) % 256
    q = (q + state[p]) % 256
    state[p], state[q] = state[q], state[p]
    return state[(state[p] + state[q]) % 256]
def encrypt(key,inputString):
    ##Encrypt input string returning a byte list
    setKey(string_to_list(key))
    return [ord(p) ^ byteGenerator() for p in inputString]
def decrypt(inputByteList):
    ##Decrypt input byte list returning a string
    return "".join([chr(c ^ byteGenerator()) for c in inputByteList])

def intToList(inputNumber):
    ##Convert a number into a byte list
    inputString = "{:02x}".format(inputNumber)
    return [int(inputString[i:i + 2], 16) for i in range(0, len(inputString), 2)]
def string_to_list(inputString):
    ##Convert a string into a byte list
    return [ord(c) for c in inputString]
key = raw_input("Enter Key: ")
ciphertext = raw_input("enter ciphertext: ")
print decrypt(intToList(ciphertext))

以下是如何使用 python 的 RC4 参考实现完成加密/解密的一般想法:

def KSA(key):
    keylength = len(key)
    S = range(256)
    j = 0
    for i in range(256):
        j = (j + S[i] + key[i % keylength]) % 256
        S[i], S[j] = S[j], S[i]  # swap
    return S

def PRGA(S):
    i = 0
    j = 0
    while True:
        i = (i + 1) % 256
        j = (j + S[i]) % 256
        S[i], S[j] = S[j], S[i]  # swap
        K = S[(S[i] + S[j]) % 256]
        yield K

def RC4(key):
    S = KSA(key)
    return PRGA(S)

if __name__ == '__main__':
    # ciphertext should be 9D5AB375EC
    key = 'secret'
    plaintext = 'plain'
    def convert_key(s):
        return [ord(c) for c in s]
    key = convert_key(key)
    keystream = RC4(key)
    ciphertext = ''.join([("%02X" % (ord(c) ^ keystream.next())) for c in plaintext])
    print ciphertext
    keystream = RC4(key)
    def convert_ct(s):
        import binascii
        return [ord(ch) for ch in binascii.unhexlify(s)]
    ciphertext = convert_ct(ciphertext)
    plaintext = ''.join([chr(c ^ keystream.next()) for c in ciphertext])
    print plaintext

使用您的代码库,可以像这样完成:

import binascii
# Global variables
state = [None] * 256
p = q = None
def setKey(key):
    ##RC4 Key Scheduling Algorithm (KSA)
    global p, q, state
    state = [n for n in range(256)]
    p = q = j = 0
    for i in range(256):
        if len(key) > 0:
            j = (j + state[i] + key[i % len(key)]) % 256
        else:
            j = (j + state[i]) % 256
        state[i], state[j] = state[j], state[i]
def byteGenerator():
    ##RC4 Pseudo-Random Generation Algorithm (PRGA)
    global p, q, state
    p = (p + 1) % 256
    q = (q + state[p]) % 256
    state[p], state[q] = state[q], state[p]
    return state[(state[p] + state[q]) % 256]
def encrypt(key, plaintext):
    ##Encrypt input string returning a byte list
    pt = string_to_list(plaintext)
    ct = rc4(key, pt)
    return list_to_string(ct, hex=True)
def decrypt(key, ciphertext):
    ##Decrypt input byte list returning a string
    ct = string_to_list(ciphertext, hex=True)
    pt = rc4(key, ct)
    return list_to_string(pt, hex=False)
def string_to_list(input_srt, hex=False):
    ##Convert a string into an int list
    if hex:
        res = [ord(ch) for ch in binascii.unhexlify(input_srt)]
    else:
        res = [ord(ch) for ch in input_srt]
    return res
def list_to_string(lst, hex=True):
    ##Convert an int list into a string
    if hex:
        res = ''.join(["%0.2X" % el for el in lst])
    else:
        res = ''.join([chr(el) for el in lst])
    return res
def rc4(key, ints):
    """Xor list of ints with output generated by RC4. Output list of ints"""
    setKey(string_to_list(key))
    return [x ^ byteGenerator() for x in ints]
# key = raw_input("Enter Key: ")
# ciphertext = raw_input("enter ciphertext: ")
key = 'secret'
plaintext = 'plain'
ciphertext = encrypt(key, plaintext)
print ciphertext
print decrypt(key, ciphertext)

解密和加密的过程基本相同

最新更新