Skip to content

Instantly share code, notes, and snippets.

@egenedy97
Created October 17, 2019 23:39
Show Gist options
  • Save egenedy97/96ae8669873913a980523346a956133b to your computer and use it in GitHub Desktop.
Save egenedy97/96ae8669873913a980523346a956133b to your computer and use it in GitHub Desktop.
#Vigenere Cipher
alphabet_pos = {'A':0, 'a':0, 'B':1, 'b':1, 'C':2, 'c':2, 'D':3, 'd':3,
'E':4, 'e':4, 'F':5, 'f':5, 'G':6, 'g':6, 'H':7, 'h':7, 'I':8, 'i':8,
'J':9, 'j':9, 'K':10, 'k':10, 'L':11, 'l':11, 'M':12, 'm':12, 'N': 13,
'n':13, 'O':14, 'o':14, 'P':15, 'p':15, 'Q':16, 'q':16, 'R':17, 'r':17,
'S':18, 's':18, 'T':19, 't':19, 'U':20, 'u':20, 'V':21, 'v':21, 'W':22,
'w':22, 'X':23, 'x':23, 'Y':24, 'y':24, 'Z':25, 'z':25 }
def alphabet_position(letter):
alphabet_pos = {'A':0, 'a':0, 'B':1, 'b':1, 'C':2, 'c':2, 'D':3,
'd':3, 'E':4, 'e':4, 'F':5, 'f':5, 'G':6, 'g':6, 'H':7, 'h':7, 'I':8,
'i':8, 'J':9, 'j':9, 'K':10, 'k':10, 'L':11, 'l':11, 'M':12, 'm':12,
'N': 13, 'n':13, 'O':14, 'o':14, 'P':15, 'p':15, 'Q':16, 'q':16,
'R':17, 'r':17, 'S':18, 's':18, 'T':19, 't':19, 'U':20, 'u':20, 'V':21,
'v':21, 'W':22, 'w':22, 'X':23, 'x':23, 'Y':24, 'y':24, 'Z':25, 'z':25
}
pos = alphabet_pos[letter]+1
return pos
def rotate(letter, rot):
shift = 97 if letter.islower() else 65
return chr((ord(letter) + rot - shift) % 26 + shift)
def encryptwithrepeatation(text, key):
encrypted = []
starting_index = 0
for letter in text:
# if it's alphanumerical, keep it that way
# find alphabet position
rotation = alphabet_position(key[starting_index])
# if it's a space or non-alphabetical character, append and move on
if not letter in alphabet_pos:
encrypted.append(letter)
elif letter.isalpha():
encrypted.append(rotate(letter, rotation))
#if we've reached last index, reset to zero, otherwise + by 1
if starting_index == (len(key) - 1):
starting_index = 0
else:
starting_index += 1
return ''.join(encrypted)
dict1 = {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4,
'F': 5, 'G': 6, 'H': 7, 'I': 8, 'J': 9,
'K': 10, 'L': 11, 'M': 12, 'N': 13, 'O': 14,
'P': 15, 'Q': 16, 'R': 17, 'S': 18, 'T': 19,
'U': 20, 'V': 21, 'W': 22, 'X': 23, 'Y': 24, 'Z': 25}
dict2={0:'A',1:'B',2:'C',3:'D',4:'E',
5:'F',6:'G',7:'H',8:'I',9:'J',
10:'K',11:'L',12:'M',13:'N',14:'O',
15:'P',16:'Q',17:'R',18:'S',19:'T',
20:'U',21:'V',22:'W',23:'X',24:'Y',25:'Z'}
#This function generates the key in
#a cyclic manner until it's length isi'nt
#equal to the length of original text
def generate_key(message,key):
x=len(message)
i=0
while True:
if x==i:
i=0
if len(key)==len(message):
break
key+=key[i]
i+=1
return key
#This function returns the encrypted text
#generated with the help of the key
def cipherText(message,key_new):
cipher_text=''
i=0
for letter in message:
if letter==' ':
cipher_text+=' '
else:
x=(dict1[letter]+dict1[key_new[i]])%26
i+=1
cipher_text+=dict2[x]
return cipher_text
#This function decrypts the encrypted text
#and returns the original text
text = input("Enter some text:")
key = input("Enter a key:")
print (encryptwithrepeatation(text, key))
key_new=generate_key(text,key)
cipher_text=cipherText(text,key_new)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment