简体   繁体   中英

Rc4 decrypt ValueError: Unknown format code 'x' for object of type 'str'

When I get to the following line in my code, I receive the following error:

inputString = "{:02x}".format(inputNumber)

ValueError: Unknown format code 'x' for object of type 'str'

How can I avoid this?

# 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 [(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 = input("Enter Key: ")
        inputstring = input("enter plaintext: ")
        print(encrypt(key, inputstring))


    elif choice == 2:  
        key = input("Enter Key: ")
        ciphertext = 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.

The format string code x converts an integer to its hexadecimal representation as a string. For example:

>>> "{:02x}".format(54563)
'd523'

The error you are getting occurs when you pass a string as inputNumber . Arguments passed to your intToList function should be integers. Consider using intToList(int(your_argument_here)) .

It looks like you're trying to convert a string to hex. Unfortunately that's not the way to do it: "{:02x}".format is for formatting a single integer, so it doesn't work for a whole string.

Python 3 includes the method bytes.hex() which performs this transformation for you, but you'll need a byte string rather than a normal string. You can use string encoding to convert from str to bytes.

If you wanted to do the transformation yourself, you could call "{:02x}".format on the character codes of each character in your string, and concatenate the individual hex values together. Be careful with Unicode in this case; you might be better off working with a byte string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM