-
Notifications
You must be signed in to change notification settings - Fork 0
/
vigenere.py
executable file
·130 lines (101 loc) · 4.08 KB
/
vigenere.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, sys
'''
uso: python3 vigenere.py
You will cipher/decipher the given text with the given key in key-autokey mode
https://www.dcode.fr/cifrado-vigenere
'''
def ask_inputs():
alph = ""
input_string = ""
key = ""
# Takes string from user
alph = input("Write S for swedish alphabet, else for english : ")
alph = alph.lower()
alphabet = "abcdefghijklmnopqrstuvwxyz"
if alph == "s":
alphabet+="åäö"
# Takes key from user
key = input("Please enter encryption key: ")
key = key.lower()
# Takes string from user
input_string = input("Please enter a string of text: ")
input_string = input_string.lower()
return alphabet,key,input_string
def vigenere_enc(alphabet,enc_key,input_string):
enc_string = ""
# Lengths of input_string
string_length = len(input_string)
# Expands the encryption key to make it longer than the inputted string
expanded_key = enc_key
expanded_key_length = len(expanded_key)
while expanded_key_length < string_length:
# Adds another repetition of the encryption key
expanded_key = expanded_key + enc_key
expanded_key_length = len(expanded_key)
key_position = 0
alph_len = len(alphabet)
for letter in input_string:
if letter in alphabet:
# cycles through each letter to find it's numeric position in the alphabet
position = alphabet.find(letter)
# moves along key and finds the characters value
key_character = expanded_key[key_position]
key_character_position = alphabet.find(key_character)
key_position = key_position + 1
# changes the original of the input string character
new_position = position + key_character_position
if new_position >= alph_len:
new_position = new_position - alph_len
new_character = alphabet[new_position]
enc_string = enc_string + new_character
return enc_string
#print("The solution is: \n" + enc_string)
#print(enc_string)
def vigenere_dec(alphabet,dec_key,input_string):
dec_string = ""
# Lengths of input_string
string_length = len(input_string)
# Expands the encryption key to make it longer than the inputted string
expanded_key = dec_key
expanded_key_length = len(expanded_key)
while expanded_key_length < string_length:
# Adds another repetition of the encryption key
expanded_key = expanded_key + dec_key
expanded_key_length = len(expanded_key)
key_position = 0
alph_len = len(alphabet)
for letter in input_string:
if letter in alphabet:
# cycles through each letter to find it's numeric position in the alphabet
position = alphabet.find(letter)
# moves along key and finds the characters value
key_character = expanded_key[key_position]
key_character_position = alphabet.find(key_character)
key_position = key_position + 1
# changes the original of the input string character
new_position = position - key_character_position
if new_position >= alph_len:
new_position = new_position + alph_len
new_character = alphabet[new_position]
dec_string = dec_string + new_character
return dec_string
#print("The solution is: \n" + dec_string)
if __name__ == "__main__":
contin=True
while contin:
# ask for code or decode
operation = input("Write D for Decode Operation, else for Code operation: ")
operation = operation.lower()
alphabet,key,input_string = ask_inputs()
sol = ""
if operation == "d":
sol = vigenere_dec(alphabet,key,input_string)
else:
sol = vigenere_enc(alphabet,key,input_string)
print("The solution is: \n" + sol)
keep = input("Continue to do more operations? press Y to continue, else terminate ")
keep = keep.lower()
if not (keep == 'y'):
contin=False