'TypeError: ord() expected string of length 1, but int found, Traceback (most recent call last):

I am getting an error while encrypting a file.

    C:\Users\username>python xor_encryptor.py raw.txt > new.txt
Traceback (most recent call last):
  File "C:\Users\username\xor_encryptor.py", line 19, in <module>
    ciphertext = xor(plaintext, KEY)
  File "C:\Users\username\xor_encryptor.py", line 10, in xor
    output_str += chr(ord(current) ^ ord(current_key))
TypeError: ord() expected string of length 1, but int found

The xor_encryptor script is:

import sys
KEY = "x"
def xor(data, key):
    key = str(key)
    l = len(key)
    output_str = ""
    for i in range(len(data)):
        current = data[i]
        current_key = key[i % len(key)]
        output_str += chr(ord(current) ^ ord(current_key))
    return output_str
def printCiphertext(ciphertext):
    print('{ 0x' + ', 0x'.join(hex(ord(x))[2:] for x in ciphertext) + ' };')
try:
    plaintext = open(sys.argv[1], "rb").read()
except:
    print("File argument needed! %s " % sys.argv[0])
    sys.exit()
ciphertext = xor(plaintext, KEY)
print('{ 0x' + ', 0x'.join(hex(ord(x))[2:] for x in ciphertext) + ' };')

Kindly give an appropriate solution.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source