python caesar cipher module code example
Example 1: python caesar cipher
def caesar_encrypt():
word = input('Enter the plain text: ')
c = ''
for i in word:
if (i == ' '):
c += ' '
else:
c += (chr(ord(i) + 3))
return c
def caesar_decrypt():
word = input('Enter the cipher text: ')
c = ''
for i in word:
if (i == ' '):
c += ' '
else:
c += (chr(ord(i) - 3))
return c
plain = 'hello'
cipher = caesar_encrypt(plain)
decipher = caesar_decrypt(cipher)
Example 2: caesar cipher python
def cc_encrypt(msg: str,key: int) -> str:
encrypted_msg = ''
try:
for char in msg:
encrypted_msg += str(chr(ord(char)+int(key)))
except:
print(Exception)
pass
return encrypted_msg
def cc_decrypt(msg: str,key: int) -> str:
decrypted_msg = ''
try:
for char in msg:
decrypted_msg += chr(ord(char)-int(key))
except:
print(Exception)
pass
return decrypted_msg
message = 'Hello World!'
key = 9
print(f'Caesar Cipher:\nEncrypted: {cc_encrypt(message,key)}\nDecrypted: {cc_decrypt(cc_encrypt(message,key),key)}')