File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ def encrypt (text , n ):
2+ encrypted = ""
3+ for char in text :
4+ if char .isupper ():
5+ c = ord (char ) + (n % 26 )
6+ if c > ord ('Z' ):
7+ c -= 26
8+ elif char .islower ():
9+ c = ord (char ) + (n % 26 )
10+ if c > ord ('z' ):
11+ c -= 26
12+ else :
13+ c = ord (char ) + 1
14+ encrypted += chr (c )
15+ return encrypted
16+
17+ def decrypt (text , n ):
18+ decrypted = ""
19+ for char in text :
20+ if char .isupper ():
21+ c = ord (char ) - (n % 26 )
22+ if c < ord ('A' ):
23+ c += 26
24+ elif char .islower ():
25+ c = ord (char ) - (n % 26 )
26+ if c < ord ('a' ):
27+ c += 26
28+ else :
29+ c = ord (char ) - 1
30+ decrypted += chr (c )
31+ return decrypted
32+
33+ def main ():
34+ text = input ("Enter a string: " )
35+ n = int (input ("Enter key: " ))
36+ encrypted_string = encrypt (text , n )
37+ print ("\n Encrypted text:" , encrypted_string )
38+ decrypted_string = decrypt (encrypted_string , n )
39+ print ("Decrypted text:" , decrypted_string )
40+
41+ if __name__ == "__main__" :
42+ main ()
You can’t perform that action at this time.
0 commit comments