-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathXOR Cipher.py
59 lines (49 loc) · 1.71 KB
/
XOR Cipher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# *********
# -*- Made by VoxelPixel
# -*- For YouTube Tutorial
# -*- https://github.com/VoxelPixel
# -*- Support me on Patreon: https://www.patreon.com/voxelpixel
# *********
def cipher_encryption():
msg = input("Enter message: ")
key = input("Enter key: ")
encrypt_hex = ""
key_itr = 0
for i in range(len(msg)):
temp = ord(msg[i]) ^ ord(key[key_itr])
# zfill will pad a single letter hex with 0, to make it two letter pair
encrypt_hex += hex(temp)[2:].zfill(2)
key_itr += 1
if key_itr >= len(key):
# once all of the key's letters are used, repeat the key
key_itr = 0
print("Encrypted Text: {}".format(encrypt_hex))
def cipher_decryption():
msg = input("Enter message: ")
key = input("Enter key: ")
hex_to_uni = ""
for i in range(0, len(msg), 2):
hex_to_uni += bytes.fromhex(msg[i:i+2]).decode('utf-8')
decryp_text = ""
key_itr = 0
for i in range(len(hex_to_uni)):
temp = ord(hex_to_uni[i]) ^ ord(key[key_itr])
# zfill will pad a single letter hex with 0, to make it two letter pair
decryp_text += chr(temp)
key_itr += 1
if key_itr >= len(key):
# once all of the key's letters are used, repeat the key
key_itr = 0
print("Decrypted Text: {}".format(decryp_text))
def main():
choice = int(input("1. Encryption\n2. Decryption\nChoose(1,2): "))
if choice == 1:
print("---Encryption---")
cipher_encryption()
elif choice == 2:
print("---Decryption---")
cipher_decryption()
else:
print("Invalid Choice")
if __name__ == "__main__":
main()