-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCesarEncryption.py
87 lines (66 loc) · 1.91 KB
/
CesarEncryption.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
"""
Author: Edgard Diaz
Date: 18th March 2020
In this class, the methods of encrypting and decrypting the plain
text file using modular arithmetic are implemented to move the original
message n times and thus make it unreadable in the case of encryption,
the same process is performed for decryption using the file containing the
encrypted text and scrolled n times to retrieve the original text.
In the alphabet the use of lowercase and capital letters is considered as
well as the numbers from zero to nine.
"""
import string
class Encryption:
def EncrypText(self, PlaneText, n):
EncrypT = "";
EncrypTxt = self.TextToNumber(PlaneText)
i = 0;
while i < len(EncrypTxt):
aux = EncrypTxt[i]
try:
x = int(EncrypTxt[i])
if x >= 0 or x <= 60:
E_n = ((x - n) % 61)
#print(E_n)
letter = self.NumberToText(E_n)
EncrypT += letter
i += 1;
except ValueError:
#i += 1;
EncrypT += aux
i += 1;
return EncrypT
def DecrypText(self, EncrypTxt, n):
Text = ""
StringNumber = self.TextToNumber(EncrypTxt)
i = 0;
while i < len(StringNumber):
aux = StringNumber[i]
try:
x = int(StringNumber[i])
if x >= 0 or x <= 60:
D_n = ((x + n) % 61)
letter = self.NumberToText(D_n)
Text += letter
i += 1;
except ValueError:
#i += 1;
Text += aux
i += 1;
return Text
def NumberToText(self,Number):
letter = 'abcdefghijklmnopqrstuvwxyzABCDFGHIJKLMNOPQRSTUVWXYZ0123456789'
return letter[Number]
def TextToNumber(self,Text):
NumberString = []
letter = 'abcdefghijklmnopqrstuvwxyzABCDFGHIJKLMNOPQRSTUVWXYZ0123456789'
i = 0
for c in Text:
#c = Text[i]
if c in letter:
#NumberString[i] = str(letter.index(c))
NumberString.append(str(letter.index(c)))
else:
NumberString.append(c);
i += 1;
return NumberString