-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathciphers.py
185 lines (139 loc) · 4.03 KB
/
ciphers.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"""
Problem
-------
**Vigenere / Vernam / Ceasar Ciphers**
Functions for encrypting and decrypting data
messages. Then send them to a friend.
Solution
--------
Implementation of the 3 chipers
Author
------
dbonadiman
"""
import sys
import random
def cesar_enc(s, i=4):
"""
cesar encript
An implementation of the cesar chiper
Parameter:
s ==> the string to encript must be in lower case
i ==> the cesar encoder parameter that represent
the number of position in the dictionary to
add to each character in the string
Test:
>>> cesar_enc('ciao')
'gmes'
>>> cesar_enc('a',1)
'b'
"""
return ''.join([chr(((ord(c)-ord('a')+i) % 26)+ord('a'))
if ord(c) >= ord('a') and ord(c) <= ord('z')
else c for c in s])
def cesar_dec(s, i=4):
"""
cesar decript
this function decripts the encoded message,
it basically apply tje cesar enc function
using - the key
Parameter:
s ==> the string to encript must be in lower case
i ==> the cesar decoder parameter that represent
the number of position in the dictionary to
add to each character in the string
Test:
>>> cesar_dec('gmes')
'ciao'
>>> cesar_dec('b',1)
'a'
"""
return cesar_enc(s, -i)
def vigenere_enc(s, key):
"""
vigenere enc
this function encripts a stirng using the
vigenere cipher this works like the cesar cipher with the
difference that it uses a string key to encript
the message
Parameter:
s ==> the string to encript
key ==> a string to use as password
Test:
>>> vigenere_enc('ciao','lemon')
'nmmc'
>>> vigenere_enc('aaaaa','lemon')
'lemon'
"""
return ''.join(cesar_enc(s[i],
ord(key[i % len(key)]) % ord('a'))
for i in range(len(s)))
def vigenere_dec(s, key):
"""
vigenere enc
this function decripts a stirng using the
vigenere cipher this works like the cesar cipher with the
difference that it uses a string key to encript
the message
Parameter:
s ==> the string to encript
key ==> a string to use as password
Test:
>>> vigenere_dec('nmmc','lemon')
'ciao'
>>> vigenere_dec('lemon','lemon')
'aaaaa'
"""
return ''.join(cesar_dec(s[i],
ord(key[i % len(key)]) % ord('a'))
for i in range(len(s)))
def get_random_key(s):
"""
get random key
this will generate a random key to use in the vernam cipher
Is it not possible to test it
"""
return [chr(random.randint(ord('a'), ord('z')))
if (ord(c) >= ord('a') and ord(c) <= ord('z'))
else c for c in s]
def vernam_enc(s, key):
"""
vernam enc
This works like the vigenere enc
with the difference that it uses a pass key generate at random
of the same lenght of the string to encript
"""
assert len(s) == len(key)
return vigenere_enc(s, key)
def vernam_dec(s, key):
"""
vernam enc
This works like the vigenere dec
with the difference that it uses need a pass key
of the same lenght of the string to encript
"""
assert len(s) == len(key)
return vigenere_dec(s, key)
def main():
try:
print("\nThis program encript a word you enter "
"using mant different cipher\n"
"Please enter the string: \n")
text = raw_input("-->")
text = text.lower()
print("\nCesar: {}".format(cesar_enc(text, 4)))
print(cesar_dec(cesar_enc(text, 4), 4) == text)
print("\nVigenere: {}".format(vigenere_enc(text, "lemon")))
print(vigenere_dec(vigenere_enc(text, "lemon"), "lemon") == text)
key = get_random_key(text)
print("\nVernam: {}".format(vernam_enc(text, key)))
print(vernam_dec(vernam_enc(text, key), key) == text)
return 0
except Exception, e:
print(e)
return 1
if __name__ == "__main__":
import doctest
doctest.testmod()
status = main()
sys.exit(status)