-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar_cipher.py
More file actions
72 lines (57 loc) · 2.25 KB
/
caesar_cipher.py
File metadata and controls
72 lines (57 loc) · 2.25 KB
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 python
"""Caesar cipher class.
In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift
cipher, Caesar's code or Caesar shift, is one of the simplest and most
widely known encryption techniques.
It is a type of substitution cipher in which each letter in the plaintext
is replaced by a letter some fixed number of positions down the alphabet.
For example, with a left shift of 3, D would be replaced by A, E would
become B, and so on.
The method is named after Julius Caesar, who used it in his
private correspondence.
"""
class CaesarCipher(object):
"""Caesar cipher, is one of the simplest encryption techniques."""
cipher = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def __init__(self, message, shift_by=3):
"""Constructor for CaesarCipher.
Args:
message: the message to encrypted or decryted.
shift_by: the number to offset by [1, 25].
"""
self.message = [character.upper() for character in message]
self.shift_by = shift_by % 26
def encrypt(self):
"""Encrypt string using cipher.
The encryption can also be represented using modular arithmetic by first
transforming the letters into numbers, according to the scheme,
A = 0, B = 1,..., Z = 25.
Encryption of a letter by a shift n can be described mathematically as...
En(X) = (x + n) mod 26
Returns:
string: The encrypted string.
"""
encrypted_string = ''
for character in self.message:
if character in self.cipher:
encrypt_index = (self.cipher.index(character) + self.shift_by) % 26
encrypted_string += self.cipher[encrypt_index]
else:
encrypted_string += character
return encrypted_string
def decrypt(self):
"""Decrypt string using cipher.
Decryption is performed similarly to the encryption scheme.
Decryption of a letter by a shift n can be described mathematically as...
Dn(X) = (x - n) mod 26
Returns:
string: The decrypted string.
"""
decrypted_string = ''
for character in self.message:
if character in self.cipher:
decrypt_index = (self.cipher.index(character) - self.shift_by) % 26
decrypted_string += self.cipher[decrypt_index]
else:
decrypted_string += character
return decrypted_string