(Python) XOR Operation by Thomas Roccia
Created the Tuesday 13 December 2022. Updated 9 months, 3 weeks ago.
The original message is first converted to binary format using the
encode() method. The key is also converted to binary format using the same method. The XOR operation is then performed on the binary message using the binary key. The resulting ciphertext is then printed to the screen. This ciphertext can then be decrypted using the same key to access the original message.
# Define the key to use for the XOR operation key = "mysecretkey" # Define the original message to be encrypted message = "Hello, world!" # Convert the key and message to binary format binary_key = key.encode('utf-8') binary_message = message.encode('utf-8') # Perform the XOR operation on the binary message using the binary key ciphertext = bytearray(len(binary_message)) for i in range(len(binary_message)): ciphertext[i] = binary_message[i] ^ binary_key[i % len(binary_key)] # Print the resulting ciphertext print(ciphertext)