Untitled
unknown
python
a year ago
1.2 kB
7
Indexable
def encrypt(text, key): res = "" for char in text: if char.isalpha(): shift = key % 26 if char.islower(): new_char = chr((ord(char) - ord('a') + shift) % 26 + ord('a')) else: new_char = chr((ord(char) - ord('A') + shift) % 26 + ord('A')) res += new_char else: res += char return res def decrypt(encrypted_text,key): decrypted_text = "" for i in range(len(encrypted_text)): c = encrypted_text[i] if c.isalpha(): shift = key % 26 if char.islower(): new_char = chr((ord(char) - ord('a') - shift) % 26 + ord('a')) else: new_char = chr((ord(char) - ord('A') - shift) % 26 + ord('A')) decrypted_text += new_char return decrypted_text text = input("Enter text to encrypt: ") key = int(input("Enter key: ")) encrypted_text = encrypt(text, key) print(encrypted_text) # text = input("Enter text to Decrypt: ") # key = int(input("Enter key: ")) # decrypted_text = decrypt(text, key) # print(decrypted_text)
Editor is loading...
Leave a Comment