-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar_cipher.py
More file actions
73 lines (57 loc) · 2.03 KB
/
caesar_cipher.py
File metadata and controls
73 lines (57 loc) · 2.03 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
72
73
"""
A kind of cipher algorithm.
Functions:
find_index: find the index of a given `char` in the given `string`.
encrypt: Encrypting the given data with a key provided by the function.
decrypt: Decrypting the given data with reversing the key provied by the function.
brute_force: A function for the Brute Force attack by testing 52 steps from 1-52.
"""
from string import ascii_letters
data_input = input('Enter your data: ')
key_input = int(input('Enter steps: '))
ALPHA = ascii_letters
def find_index(string: str, char: str) -> int:
"""
I don't want to use `.index` method in Python strings,
then I wrote this to underestand how `.index.` works.
"""
for ch in range(0, len(string)):
if string[ch] == char:
return ch
return "Your index not found in this string."
def encrypt(data: str, key: int) -> int:
"""
The encrypting function.
Attributes:
data(str): The string will be encrypted.
key(int): The step(shift).
"""
result = ''
for char in data:
if char not in ALPHA:
result += char
else:
alpha_key = (find_index(ALPHA, char) + key) % len(ALPHA)
# you can use `alpha.index(char)` instead of `find_index(ALPHA, char)` to use Python abilities.
result += ALPHA[alpha_key]
return result
def decrypt(data: str, key: int) -> str:
"""
The decrypting function.
Attributes:
data(str): The encrypted string that will be decrypted.
key(str): The step(shift).
"""
# Reverse the key to decrypt the data
key *= -1
# Return decrypted function with a new key(reversed key)
return encrypt(data=data, key=key)
def brute_force(data: str) -> dict:
"""
The brute force function for find the decrypted string from 1-52 steps.
"""
brute_force_data = {}
for step in range(1, len(ALPHA)+1):
# Call decrypt function for test 52 steps on it
brute_force_data[step] = decrypt(data, step)
return brute_force_data