-
Notifications
You must be signed in to change notification settings - Fork 0
/
Python_Script_to_Detect_ROT_encryption.py
70 lines (58 loc) · 2.07 KB
/
Python_Script_to_Detect_ROT_encryption.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
'''
.----.
|C>_ |
__|____|__
| ______--|
`-/.::::.\-'a
`--------'
This is a simple project I did on 26/5/2023. I realised the previous version did not work.
This code shows a ROT encryption algorithm.
Letters will be shifted to the right by a certain amount, called the 'rotation number'
Uppercase and lowercase letters will remain upper- or lower-case
Numbers are unaffected.
ASCII ART from https://www.asciiart.eu/computers/computers
'''
UPPER_CHAR_SET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
LOWER_CHAR_SET = "abcdefghijklmnopqrstuvwxyz"
DEFAULT_ROT = 13
def rotate(position: int, rotation: int)-> int:
new_position = position + rotation
if new_position>25:
new_position-= 26
return new_position
# Increases the index of the character. If the index is more than 25, start again from 0.
def encrypt_char(character: str, rotation_number: int) -> str:
if character.isalpha():
if character.isupper():
index = UPPER_CHAR_SET.find(character)
new_index = rotate(index, rotation_number)
new_character = UPPER_CHAR_SET[new_index]
if character.islower():
index = LOWER_CHAR_SET.find(character)
new_index = rotate(index, rotation_number)
new_character = LOWER_CHAR_SET[new_index]
else:
new_character = character
index = "NO INDEX"
return new_character
# Encrypts a Character. If it is an integer, no encryption is applied
def encrypt(input_string: str, rot_number: int = DEFAULT_ROT)-> str:
encrypted_text = list()
for i in input_string:
encrypted_text.append(encrypt_char(i, rot_number))
return listToString(encrypted_text)
# Encrypts the whole string
def listToString(s):
# initialize an empty string
str1 = ""
# traverse in the string
for ele in s:
str1 += ele
# return string
return str1
#def decrypt_char()
#while True:
# i = input("Input: ")
# desired_rot = int(input("ROT: "))
# print(encrypt(i, desired_rot))
# Just code for debugging