-
Notifications
You must be signed in to change notification settings - Fork 0
/
CaesarMan.py
110 lines (109 loc) · 3.22 KB
/
CaesarMan.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/python3
encdec = input("(E)ncrypt/(D)ecrypt?: ")
if encdec.lower() == "e":
text = input("Enter text to Encrypt: ")
try:
shift = int(input("Please enter the shifting number (between - 25 and 25): "))
except ValueError:
print("Error: Expected an integer")
exit(-1)
if shift < -25 or shift > 25:
print("Error: Shifting Number not in Range")
exit(-2)
output = ""
for char in text:
if char.islower() == False and char.isupper() == False:
output += char
else:
compensate = 96 if char.islower() == True else 64
acc = ord(char) + shift
if acc > (compensate + 26):
compchar = chr(acc - 26)
elif acc < (compensate + 1):
compchar = chr(acc + 26)
else:
compchar = chr(acc)
output += compchar
print("The Encrypted Text:", output)
elif encdec.lower() == "d":
text = input("Enter text to Decrypt: ")
shift = input("Please enter the shifting number (between - 25 and 25) or B for Brute Force: ")
try:
shift = int(shift)
except ValueError:
if shift.lower() != "b":
print("Error: Expected an integer or 'B'")
exit(-1)
if (shift != "b" and shift != "B") and (shift < -25 or shift > 25):
print("Error: Shifting Number not in Range")
exit(-2)
if shift != "b" and shift != "B":
output = ""
for char in text:
if char.islower() == False and char.isupper() == False:
output += char
else:
compensate = 96 if char.islower() == True else 64
acc = ord(char) - shift
if acc > (compensate + 26):
compchar = chr(acc - 26)
elif acc < (compensate + 1):
compchar = chr(acc + 26)
else:
compchar = chr(acc)
output += compchar
print("The Decrypted Text:", output)
else:
intelligent = True if input("Enter 'Y' if you want intelligent bruteforcing: ").lower() == "y" else False
if intelligent:
for i in range(51):
shift = i - 25
output = ""
for char in text:
if char.islower() == False and char.isupper() == False:
output += char
else:
compensate = 96 if char.islower() == True else 64
acc = ord(char) - shift
if acc > (compensate + 26):
compchar = chr(acc - 26)
elif acc < (compensate + 1):
compchar = chr(acc + 26)
else:
compchar = chr(acc)
output += compchar
words = output.split(" ")
flagged = 0
twords = 0
for word in words:
if word.lower() == word and word.upper() == word:
continue
if "a" not in word and "e" not in word and "i" not in word and "o" not in word and "y" not in word and "u" not in word:
flagged = flagged + 1
twords = twords + 1
if flagged:
if (flagged/twords) < (1/3):
print("\nPossible Decrypted Text:", output)
else:
print("\nPossible Decrypted Text:", output)
else:
for i in range(51):
shift = i - 25
output = ""
for char in text:
if char.islower() == False and char.isupper() == False:
output += char
else:
compensate = 96 if char.islower() == True else 64
acc = ord(char) - shift
if acc > (compensate + 26):
compchar = chr(acc - 26)
elif acc < (compensate + 1):
compchar = chr(acc + 26)
else:
compchar = chr(acc)
output += compchar
print("\nPossible Decrypted Text:", output)
else:
print("Exitting..")
exit(0)