Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
49
Task/Vigen-re-cipher/Python/vigen-re-cipher.py
Normal file
49
Task/Vigen-re-cipher/Python/vigen-re-cipher.py
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
'''Vigenere encryption and decryption'''
|
||||
|
||||
from itertools import starmap, cycle
|
||||
|
||||
|
||||
def encrypt(message, key):
|
||||
'''Vigenere encryption of message using key.'''
|
||||
|
||||
# Converted to uppercase.
|
||||
# Non-alpha characters stripped out.
|
||||
message = filter(str.isalpha, message.upper())
|
||||
|
||||
def enc(c, k):
|
||||
'''Single letter encryption.'''
|
||||
|
||||
return chr(((ord(k) + ord(c) - 2 * ord('A')) % 26) + ord('A'))
|
||||
|
||||
return ''.join(starmap(enc, zip(message, cycle(key))))
|
||||
|
||||
|
||||
def decrypt(message, key):
|
||||
'''Vigenere decryption of message using key.'''
|
||||
|
||||
def dec(c, k):
|
||||
'''Single letter decryption.'''
|
||||
|
||||
return chr(((ord(c) - ord(k) - 2 * ord('A')) % 26) + ord('A'))
|
||||
|
||||
return ''.join(starmap(dec, zip(message, cycle(key))))
|
||||
|
||||
|
||||
def main():
|
||||
'''Demonstration'''
|
||||
|
||||
text = 'Beware the Jabberwock, my son! The jaws that bite, ' + (
|
||||
'the claws that catch!'
|
||||
)
|
||||
key = 'VIGENERECIPHER'
|
||||
|
||||
encr = encrypt(text, key)
|
||||
decr = decrypt(encr, key)
|
||||
|
||||
print(text)
|
||||
print(encr)
|
||||
print(decr)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue