-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniversal_caesar_cipher
executable file
·71 lines (58 loc) · 1.9 KB
/
universal_caesar_cipher
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
#!/usr/bin/env python3
########################################################
# aleph8 #
#------------------------------------------------------#
# Universal Caesar Cipher and Decipher #
#------------------------------------------------------#
# #
########################################################
def cipher():
text=input("Insert the text: ")
cipher=int(input("Insert the displacement: "))
result=""
# ord("A") = 65, ord("Z") = 90, ord("a") = 97, ord("z") = 122
# ord(i) => "Given a string representing one Unicode character, return an integer representing the Unicode code"
# chr(i) => "Return the string representing a character whose Unicode code point is the integer i"
for i in text:
tmp=ord(i)
if ( tmp >= 65 and tmp <= 90 ) or ( tmp >= 97 and tmp <= 122 ):
if ( tmp >= 65 and tmp <= 90 ):
result+=chr((tmp-65+cipher)%26+65)
else:
result+=chr((tmp-97+cipher)%26+97)
else:
print("ERROR: INVALID CHARACTER")
print(result)
def decipher(n,text):
result=""
for i in text:
tmp=ord(i)
if ( tmp >= 65 and tmp <= 90 ) or ( tmp >= 97 and tmp <= 122 ):
if ( tmp >= 65 and tmp <= 90 ):
result+=chr((tmp-65-n)%26+65)
else:
result+=chr((tmp-97-n)%26+97)
else:
print("ERROR: INVALID CHARACTER")
print("Caesar(%d):%s\n" % (n,result))
mode=int(input("""OPTIONS
\t0)Cipher
\t1)Decipher
Choose one of them (0 or 1): """))
if mode == 0:
cipher()
elif mode == 1:
text=input("Insert the text: ")
yn=input("Do you know the displacement?(y/n): ")
if yn == "y":
ciphern=int(input("Insert the displacement: "))
decipher(ciphern,text)
elif yn == "n":
for i in range(27):
decipher(i,text)
else:
print("[x]Invalid option!")
exit(255)
else:
print("[x]Invalid option!")
exit(255)