-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxor.py
90 lines (80 loc) · 2.72 KB
/
xor.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from string import ascii_lowercase, ascii_uppercase
def encrypt(message, key):
cypher = []
final = []
for b in message:
if ascii_lowercase.find(b) != -1 and ascii_uppercase.find(b) == -1:
cypher1 = alphabet[ascii_lowercase.find(b)]
elif ascii_lowercase.find(b) == -1 and ascii_uppercase.find(b) != -1:
cypher1 = ALPHABET[ascii_uppercase.find(b)]
else:
cypher1 = b
cypher.append(cypher1)
for y in range(0, len(message)):
semi = int(bin(ord(cypher[y])-96), 2) ^ int(bin(ord(key[y])-96), 2)
semi = semi + 96
final.append(chr(semi))
finalPrint = ''.join(final)
print(finalPrint)
myFile = open('final.txt', 'w')
myFile.write(f'Encrypted message: {finalPrint}')
def decrypt(cypher, key):
msg = []
final = []
for y in range(0, len(cypher)):
semi = int(bin(ord(cypher[y])-96), 2) ^ int(bin(ord(key[y])-96), 2)
semi = semi + 96
msg.append(chr(semi))
for b in msg:
if ascii_lowercase.find(b) != -1 and ascii_uppercase.find(b) == -1:
msg1 = ascii_lowercase[''.join(alphabet).find(b)]
elif ascii_lowercase.find(b) == -1 and ascii_uppercase.find(b) != -1:
msg1 = ascii_uppercase[''.join(ALPHABET).find(b)]
else:
msg1 = b
final.append(msg1)
finalPrint = ''.join(final)
print(finalPrint)
myFile = open('final.txt', 'w')
myFile.write(f'Decrypted message: {finalPrint}')
def starto():
global alphabet, ALPHABET
choice = input('Welcome to the fusion between XOr and Shift cypher!''\n''Would you like to (E)ncrypt or (D)ecrypt?''\n''>>').lower()
message = input('Message: ')
key = input('Key: ')
if len(message) < len(key):
newKey = []
for i in range(0, len(message)):
newKey.append(key[i])
''.join(str(newKey))
elif len(message) > len(key):
newKey = []
x = 0
for i in range(0, len(message)):
newKey.append(key[x])
if x + 1 == len(key):
x = 0
else:
x += 1
''.join((str(newKey)))
else:
newKey = key
alphabet = []
ALPHABET = []
for i in ascii_lowercase:
alphabet.append(i)
ALPHABET.append(i.upper())
for c in range(0, len(key)):
hold = alphabet[0]
alphabet.pop(0)
alphabet.append(hold)
hold = ALPHABET[0]
ALPHABET.pop(0)
ALPHABET.append(hold)
if choice == 'e':
encrypt(message, newKey)
elif choice == 'd':
decrypt(message, newKey)
if __name__ == "__main__" :
starto()
input()