Created
June 13, 2012 16:07
-
-
Save BlackRabbit-github/2925008 to your computer and use it in GitHub Desktop.
implementation of vigenere cipher in python
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
letters = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' ','.',',','?','1','2','3','4','5','6','7','8','9','0'] | |
cipher = [] | |
pos_key = [] | |
pos_text = [] | |
pos_cipher = 0 | |
key = [] | |
plain = [] | |
letters_len = 40 | |
import os | |
def find_pos(value): | |
temp = [] | |
i=0 | |
while i<len(value): | |
j=0 | |
while j<letters_len: | |
if value[i] == letters[j]: | |
temp.append(j) | |
j = j+1 | |
i= i+1 | |
return temp | |
def chek_end(m,n,size): | |
if m==size and n<size: | |
m=0 | |
return m | |
if m==size and n==size: | |
return m,n | |
return m | |
def vigenere_encrypt(n): | |
K = raw_input('Enter The Key ::::') | |
P= raw_input('Enter The String To encrypt or decrypt ::::') | |
key = map(lambda k:k.lower(),K) | |
plain = map(lambda p:p.lower(),P) | |
pos_key = find_pos(key) | |
pos_text = find_pos(plain) | |
if len(pos_key) > len(pos_text): | |
range_list = pos_key | |
else : | |
range_list = pos_text | |
i=0 | |
j=0 | |
loop_pass = 0 | |
while i<len(range_list) and j<len(range_list) and loop_pass<len(pos_text): | |
try: | |
pos_cipher = n*pos_key[i] + pos_text[j] | |
if pos_cipher<letters_len: | |
cipher.append(letters[pos_cipher]) | |
else: | |
pos_cipher = pos_cipher-letters_len | |
cipher.append(letters[pos_cipher]) | |
i = i+1 | |
j = j+1 | |
if i==len(pos_key) and j<len(pos_text): | |
i=0 | |
elif j==len(pos_text) and i<len(pos_key): | |
j=0 | |
loop_pass = loop_pass + 1 | |
except: | |
print("Oops Something went wrong") | |
result = "" .join(cipher) | |
print result | |
if __name__=='__main__': | |
n = input (' 1 for encrypt \n-1 for decrypt\n::::') | |
vigenere_encrypt(n) | |
end=input ('press <enter>') | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment