File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ class VigenereCipher :
2+ def __init__ (self ) -> None :
3+ pass
4+
5+ @staticmethod
6+ def encrypt (msg : str , key : str ) -> str :
7+ msg , key = msg .upper (), key .upper ()
8+ if len (msg ) > len (key ):
9+ # Generates the key in a cyclic manner until it's length equal to the length of text
10+ for i in range (len (msg ) - len (key )):
11+ key += key [i % len (key )]
12+ encrypted_words = []
13+ curr_key_index = 0
14+ for text in msg .split ():
15+ result = ''
16+ for i in range (len (text )):
17+ x = (ord (text [i ]) + ord (key [curr_key_index ])) % 26
18+ x += ord ('A' )
19+ result += chr (x )
20+ curr_key_index += 1
21+ encrypted_words .append (result )
22+ return ' ' .join (encrypted_words ).lower ()
23+
24+ @staticmethod
25+ def decrypt (msg : str ,key : str ) -> str :
26+ msg , key = msg .upper (), key .upper ()
27+ if len (msg ) > len (key ):
28+ # Generates the key in a cyclic manner until it's length equal to the length of msg
29+ for i in range (len (msg ) - len (key )):
30+ key += key [i % len (key )]
31+ decrypted_words = []
32+ curr_key_index = 0
33+ for word in msg .split ():
34+ result = ''
35+ for i in range (len (word )):
36+ x = (ord (word [i ]) - ord (key [curr_key_index ]) + 26 ) % 26
37+ x += ord ('A' )
38+ result += chr (x )
39+ curr_key_index += 1
40+ decrypted_words .append (result )
41+ return ' ' .join (decrypted_words ).lower ()
You can’t perform that action at this time.
0 commit comments