forked from jtemporal/caipyra
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request jtemporal#27 from jcemelanda/VigenereImplementation
Cifra Vigenère
- Loading branch information
Showing
3 changed files
with
111 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Code inspired by | ||
# http://programeveryday.com/post/implementing-a-basic-vigenere-cipher-in-python/ | ||
|
||
# version with encrypter | ||
# https://gist.github.com/jcemelanda/cba27615f50b2c80cbcd5fc1188d4f43 | ||
|
||
from itertools import cycle | ||
from functools import reduce | ||
|
||
ALPHA_LOW = 'abcdefghijklmnopqrstuvwxyz' | ||
ALPHA_UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | ||
|
||
|
||
def decrypt(key, ciphertext): | ||
"""Decrypt the string and return the plaintext""" | ||
pairs = zip(ciphertext, cycle(key)) | ||
result = '' | ||
|
||
for pair in pairs: | ||
if pair[0] in ALPHA_LOW: | ||
total = reduce( | ||
lambda x, y: ALPHA_LOW.index(x) - ALPHA_LOW.index(y), | ||
pair | ||
) | ||
result += ALPHA_LOW[total % 26] | ||
elif pair[0] in ALPHA_UPPER: | ||
total = reduce( | ||
lambda x, y: ALPHA_UPPER.index(x) - ALPHA_LOW.index(y), | ||
pair | ||
) | ||
result += ALPHA_UPPER[total % 26] | ||
else: | ||
result += pair[0] | ||
|
||
return result |