Skip to content

Commit 4ad424a

Browse files
authored
Update shuffled_shift_cipher.py
1 parent 21e2c45 commit 4ad424a

1 file changed

Lines changed: 15 additions & 15 deletions

File tree

ciphers/shuffled_shift_cipher.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def __str__(self) -> str:
4242
"""
4343
:return: passcode of the cipher object
4444
"""
45-
return "".join(self.__passcode)
45+
return self.__passcode
4646

4747
def __neg_pos(self, iterlist: list[int]) -> list[int]:
4848
"""
@@ -55,20 +55,19 @@ def __neg_pos(self, iterlist: list[int]) -> list[int]:
5555
for i in range(1, len(iterlist), 2):
5656
iterlist[i] *= -1
5757
return iterlist
58-
59-
def __passcode_creator(self) -> list[str]:
58+
def __passcode_creator(self) -> str:
6059
"""
6160
Creates a random password from the selection buffer of
6261
1. uppercase letters of the English alphabet
6362
2. lowercase letters of the English alphabet
6463
3. digits from 0 to 9
6564
66-
:rtype: list
65+
:rtype: str
6766
:return: a password of a random length between 10 to 20
6867
"""
6968
choices = string.ascii_letters + string.digits
7069
password = [random.choice(choices) for _ in range(random.randint(10, 20))]
71-
return password
70+
return ''.join(password)
7271

7372
def __make_key_list(self) -> list[str]:
7473
"""
@@ -104,15 +103,14 @@ def __make_key_list(self) -> list[str]:
104103
temp_list: list[str] = []
105104

106105
# algorithm for creating a new shuffled list, keys_l, out of key_list_options
107-
for i in key_list_options:
108-
temp_list.extend(i)
106+
for char in key_list_options:
107+
temp_list.append(char)
109108

110109
# checking breakpoints at which to pivot temporary sublist and add it into
111110
# keys_l
112-
if i in breakpoints or i == key_list_options[-1]:
111+
if char in breakpoints or char == key_list_options[-1]:
113112
keys_l.extend(temp_list[::-1])
114113
temp_list.clear()
115-
116114
# returning a shuffled keys_l to prevent brute force guessing of shift key
117115
return keys_l
118116

@@ -135,13 +133,14 @@ def decrypt(self, encoded_message: str) -> str:
135133
136134
"""
137135
decoded_message = ""
136+
key_len = len(self.__key_list)
138137

139138
# decoding shift like Caesar cipher algorithm implementing negative shift or
140139
# reverse shift or left shift
141-
for i in encoded_message:
142-
position = self.__key_list.index(i)
140+
for char in encoded_message:
141+
position = self.__key_list.index(char)
143142
decoded_message += self.__key_list[
144-
(position - self.__shift_key) % -len(self.__key_list)
143+
(position - self.__shift_key) % key_len
145144
]
146145

147146
return decoded_message
@@ -157,13 +156,14 @@ def encrypt(self, plaintext: str) -> str:
157156
158157
"""
159158
encoded_message = ""
159+
key_len = len(self.__key_list)
160160

161161
# encoding shift like Caesar cipher algorithm implementing positive shift or
162162
# forward shift or right shift
163-
for i in plaintext:
164-
position = self.__key_list.index(i)
163+
for char in plaintext:
164+
position = self.__key_list.index(char)
165165
encoded_message += self.__key_list[
166-
(position + self.__shift_key) % len(self.__key_list)
166+
(position + self.__shift_key) % key_len
167167
]
168168

169169
return encoded_message

0 commit comments

Comments
 (0)