python - Python 中的 RC4 加密

标签 python encryption cryptography

我已经浏览了几个使用 RC4 分组密码的 python 脚本...我在完成程序以使其正确输出时遇到问题...

程序当前要求“ key ”和“明文”(用 key 加密的文本)。 并输出一个编码字符串......我认为。因此,如果我输入“明文”一词进行加密,我会得到以下结果。然而我认为这是不完整的......

[187, 243, 22, 232, 217, 64, 175, 10, 211]

我想要十六进制的加密输出

BB F3 16 E8 D9 40 AF 0A D3

我的程序目前还不完整,但只是想要一些关于如何进行的指导

  1. 完成加密部分,使其输出为十六进制(我想我必须将字节转换为十六进制?)
<小时/>

编辑:以上问题已由易卜拉欣解决。只是需要解密方面的帮助

  • 我不知道从哪里开始解密...我希望能够有一个输入,该输入将采用十六进制的 key 和密文;并将密文解密为明文。
  • 我理解加密过程中的逻辑,但我很难掌握解密过程,尽管它非常相似。

    <小时/>
    # Global variables
    state = [None] * 256
    p = q = None
    
    def setKey(key):
        ##RC4 Key Scheduling Algorithm
        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
        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]
    
    
    
    
    loop = 1
    while loop == 1: #simple loop to always bring the user back to the menu
    
        print("RC4 Encryptor/Decryptor")
        print
        print("Please choose an option from the below menu")
        print
        print("1) Encrypt")
        print("2) Decrypt")
        print
    
        choice = input("Choose your option: ")
        choice = int(choice)
    
        if choice == 1:
            key = raw_input("Enter Key: ")
            inputstring = raw_input("enter plaintext: ")
            encrypt(key, inputstring)
    
    
        elif choice == 2:   
            key = raw_input("Enter Key: ")
            ciphertext = raw_input("enter plaintext: ")
            print decrypt(intToList(ciphertext))
    
        elif choice == 3: 
        #returns the user to the previous menu by ending the loop and clearing the screen.
            loop = 0
    
        else:   
            print ("please enter a valid option") #if any NUMBER other than 1, 2 or 3 is entered.
    

    最佳答案

    要将十进制输出转换为十六进制输出:

    >>> arr = [187, 243, 22, 232, 217, 64, 175, 10, 211]
    >>> ' '.join('%02x'%i for i in arr)
    'bb f3 16 e8 d9 40 af 0a d3'
    >>> 
    

    关于python - Python 中的 RC4 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36834013/

    相关文章:

    python - 如何进行正则表达式检查子字符串列表是否出现在字符串之前?

    python - 通过 html 发布 pandas 数据透视表

    java - java中的网络通信加密

    c# - 如何在序列化后加密保存二进制流并读回?

    python - 希望在 python 中解析字符串

    Python:绘制时间戳数据框matplotlib

    Python 中的 PHP openssl AES

    c# - 使用 RsaProtectedConfigurationProvider 加密/解密 app.config 部分

    c - 使用 RSA 的 Visa 测试卡解密错误

    python - 计算丢失的比特币私钥的校验和比特币