Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Python - Caesar Cipher
# Caesar Cipher
LEN,UP_MIN,LOW_MIN = 26,65,97
def getchar(ch, key, enc):
if not ch.isalpha():
return ch
key = key if enc else -key
(min,max) = (UP_MIN, UP_MIN+LEN) if ch.isupper() else (LOW_MIN, LOW_MIN+LEN)
idx = ord(ch) + key
mul = 1 if idx < min else (-1 if idx > max else 0)
return chr(idx + mul * LEN)
def encdec(message, key, enc):
for i in message:
return ''.join(result)
print('Enter the message : ', end='')
while not (key > 0 and key < LEN):
print('Enter key [1-26] : ', end='')
encMsg = encdec(message, key, True)
print('Encrypted message : %s' % encMsg)
decMsg = encdec(encMsg, key, False)
print('Decrypted message : %s' % decMsg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment