Instructions
Requirements and Specifications
- User will input a Word and to encrypt
- User will input a Key
- Your program will take these inputs and use the Python ord function to encrypt the word
- Your output needs to have the following
- Your encrypted text
- Your plain text
- Binary value of the plain text
- Binary value of the Key
- Binary value of the XOR (encrypted) output
- Hex value of the text plain text
- Hex value of the text encrypted text
- User will input a Word and to encrypt
- User will input a Key
- Your program will take these inputs and use the Python ord function to encrypt the word
- Your output needs to have the following
- Your encrypted text
- Your plain text
- Binary value of the plain text Binary val
- ue of the Key
- Binary value of the XOR (encrypted) output
- Hex value of the text plain text
- Hex value of the text encrypted text
Source Code
if __name__ == '__main__':
# Ask for string to encrypt
input_str = input("Enter the cipher text or plain text: ")
# Ask for key
key = input('Enter the key for encryption or decryption: ')
encrypt_str = ""
decrypt_str = ""
# Create a list to store all the encrypted values but in binary format
encrypted_binary = list()
# Create a list to store the decrypted values in binary
decrypted_binary = list()
# Create a list to store the binary of the key
key_binary = [bin(ord(char)) for char in key]
# Now, we XOR each character in the message with each character in the key
key_index = 0
# Loop through each char in message
for char in input_str:
key_val = ord(key[key_index]) # Get the current char in the key, converted to ord
# Apply XOR
xor_result = ord(char)^key_val
# Now, we convert to char again and add to the encrypt_str variable
encrypt_str += chr(xor_result)
# We convert this value to binary and add to a list
encrypted_binary.append(bin(xor_result))
# Increment the index so in the next loop we use the next char of the key
key_index += 1
if key_index >= len(key): # If we reach the end of the key, we start with the first chara again
key_index = 0
# Now, in the next loop, we will decrypt the message
# Loop through each char in the Encrypted Message
for char in encrypt_str:
key_val = ord(key[key_index]) # Get the current char in the key, converted to ord
# Apply XOR
xor_result = ord(char) ^ key_val
# Now, we convert to char again and add to the encrypt_str variable
decrypt_str += chr(xor_result)
# We convert this value to binary and add to a list
decrypted_binary.append(bin(xor_result))
# Increment the index so in the next loop we use the next char of the key
key_index += 1
if key_index >= len(key): # If we reach the end of the key, we start with the first chara again
key_index = 0
# Finally, we print the results
print("Here's the encrypted output:", encrypt_str)
print("Here's the decrypted output:", decrypt_str)
print("Here's the binary of the encrypted output:", *encrypted_binary)
print("Here's the binary of the key:", *key_binary)
print("Here's the binary of the decrypted output:", *decrypted_binary)