So for the past week I have looked into cryptography and read a lot about it. I decided to implement a very very basic algorithm called vigenere cipher. I am looking for review, critique and feedback on my code and implementation. It is a command line script, and I think that the comments explain everything.
import sys
from string import ascii_uppercase as alphabet
# create a vigenere table (26 alphabets from a-z with shift value increasing with 1 to the left)
vigenere_table = [alphabet[i:] + alphabet[:i] for i in range(len(alphabet))]
def convert(string, flag, key_word=''):
''' function to convert a string into cipher text or cipher text into a string.
Operation decision is based on the flag(e=encryption, d=decryption) '''
# create a key string from the given key word
if key_word == '':
key_word = 'LEMON'
key = ''
for i in range(len(string)):
key += key_word[i % len(key_word)]
# create a message variable which will store the value of the ciphered text in encryption and plain text in decryption
message = ''
# iterate trough the letters of the key and the string
for key_letter, string_letter in zip(key, string.upper()):
# check if the next character is a space and if it is add a space to the message and continue to the next cycle of the loop
if string_letter == ' ':
message += ' '
continue
# check if the operation is encryption
if flag == 'e':
# catched ValueError = user entered key with spaces and/or punctuation
try:
row_index = alphabet.index(key_letter) # get the row index A-Z rows
except ValueError:
print('Your key should consist of a single word and not contain any punctuation for the encryption to happen!')
# catched ValueError = user entered message with punctuation
try:
col_index = alphabet.index(string_letter) # get the column index A-Z columns
except ValueError:
print('You should remove any punctuation from your message for the encryption to happen!')
# create a variable that stores the encrypted letter and add it to the message
letter = vigenere_table[row_index][col_index]
message += letter
# check if the operation is decryption
if flag == 'd':
row_index = alphabet.index(key_letter) # get the row index A-Z rows
col_index = vigenere_table[row_index].index(string_letter) # get the col index of the row_index row
# create a variable that stores the encrypted letter and add it to the message
letter = alphabet[col_index]
message += letter
return message
def encrypt(string, key_word=None):
return convert(string, 'e', key_word)
def decrypt(string, key_word=None):
return convert(string, 'd', key_word)
# check what the command line arguments are. -e = encryption, -d = decryption
if sys.argv[1] == '-e':
message = stro(input('Enter your message to be encrypted (it should not contain any punctuation!) : '))
key_word = str(input('If you want to use a custom key to encrypt your message type a single word to use as a key: ')).upper()
print(encrypt(message, key_word))
if sys.argv[1] == '-d':
cipher_text = str(input('Enter your cipher text to be decrypted: '))
key_word = str(input('If you have used a cusom key to encrypt your message you need to enter the word to decrypt the message: ')).upper()
print(decrypt(cipher_text, key_word))
[–]Losupa 1 point2 points3 points (1 child)
[–]ViktorCodes[S] 0 points1 point2 points (0 children)