Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_offset(c):
- # uses Python inline if: http://stackoverflow.com/questions/11880430/how-to-write-inline-if-statement-for-print
- return 65 if c.isupper() else 97
- def encrypt_char(c, key):
- offset = get_offset(c)
- return chr(((ord(c) - offset + key) % 26) + offset)
- def decrypt_char(c, key):
- offset = get_offset(c)
- ordinal = ord(c) - offset - key + 26 if ((ord(c) - offset) < key) else (ord(c) - offset - key)
- return chr((ordinal % 26) + offset)
- def vigenere(message, key, encrypt=True):
- op = encrypt_char if encrypt else decrypt_char
- key = [ord(c) - get_offset(c) for c in key]
- def vigenere_helper(message, key_index, ciphertext, op):
- return ciphertext if message == '' else vigenere_helper(message[1:], (key_index + 1) % len(key),
- ciphertext + (op(message[0], key[key_index]) if message[0].isalpha() else message[0]), op)
- return vigenere_helper(message, 0, '', op)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement