Vigenere cipher. Python parsing algorithm
Recently I wanted to remember my “spy” childhood and at least basic to learn different encryption methods. And the first choice fell on the Vigenere cipher. By itself, it is not extremely complex, but has long been considered cryptographic. A century of commercials from the XV and to the very XIX, until someone Kaziski completely cracked the code.
However, we restrict the Wikipedia citation to only the description of the algorithm itself.
The method is an advanced cesar of Caesar, where the letters were shifted to a specific position.
The Vigenère cipher consists of a sequence of several Caesar ciphers with different shift values.
Suppose we have a certain alphabet where numbers correspond to each letter:

Then if the letters az correspond to numbers 0-25, then Vigenere encryption can be written in the form of a formula:

Decryption:

In fact, we do not need anything else except these two formulas and we can proceed with the implementation.
Here I want to say that I tried to implement the algorithm is not simpler and more elegant, but the most understandable and detailed.
Actually, sir.
Encode the words 'Hello world' with the tricky key 'key'.
First you need to create a dictionary of characters that will participate in encryption:
Next, you need to compare the letters in our word with the letters in the dictionary and assign them the corresponding numeric indices
And so we encoded our word and key and got 2 lists of indices:
Value = [72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
Key = [107, 101, 121]
Next we match the key indices with the indices of our word with the full_encode () function:
We get our cipher indices and translate them into a string with the decode_val () function:
{0: [72, 107], 1: [101, 101], 2: [108, 121], 3: [108, 107], 4: [ 111, 101], 5: [32, 121], 6: [119, 107], 7: [111, 101], 8: [114, 121], 9: [108, 107], 10: [100, 101]}
Indexes: [52, 75, 102, 88, 85, 26, 99, 85, 108, 88, 74]
We get a coded super-secret message: 4KfXUcUlXJ
You can decode all this using the full_decode () function, the first argument of which is a list of cipher numeric indices, and the second is a list of key indices:
All the same, we get the cipher indices and translate them into a string with the familiar decode_val () function:
[72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
And voila! Our encrypted word: Hello world
Well, the main challenge
The article tried to describe everything so that it was as clear as possible even for the beginner in Python. Although this encryption algorithm is no longer 100% reliable, it is well suited for those who have taken the path of learning more serious things, for example, the same RSA.
Links and code:
Description of the Vigenere cipher on Wikipedia
Python source code
However, we restrict the Wikipedia citation to only the description of the algorithm itself.
The method is an advanced cesar of Caesar, where the letters were shifted to a specific position.
The Vigenère cipher consists of a sequence of several Caesar ciphers with different shift values.
Suppose we have a certain alphabet where numbers correspond to each letter:

Then if the letters az correspond to numbers 0-25, then Vigenere encryption can be written in the form of a formula:

Decryption:

In fact, we do not need anything else except these two formulas and we can proceed with the implementation.
Here I want to say that I tried to implement the algorithm is not simpler and more elegant, but the most understandable and detailed.
Actually, sir.
Encode the words 'Hello world' with the tricky key 'key'.
First you need to create a dictionary of characters that will participate in encryption:
def form_dict():
d = {}
iter = 0
for i in range(0,127):
d[iter] = chr(i)
iter = iter +1
return d
Next, you need to compare the letters in our word with the letters in the dictionary and assign them the corresponding numeric indices
def encode_val(word):
list_code = []
lent = len(word)
d = form_dict()
for w in range(lent):
for value in d:
if word[w] == d[value]:
list_code.append(value)
return list_code
And so we encoded our word and key and got 2 lists of indices:
Value = [72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
Key = [107, 101, 121]
Next we match the key indices with the indices of our word with the full_encode () function:
def comparator(value, key):
len_key = len(key)
dic = {}
iter = 0
full = 0
for i in value:
dic[full] = [i,key[iter]]
full = full + 1
iter = iter +1
if (iter >= len_key):
iter = 0
return dic
def full_encode(value, key):
dic = comparator(value, key)
print 'Compare full encode', dic
lis = []
d = form_dict()
for v in dic:
go = (dic[v][0]+dic[v][1]) % len(d)
lis.append(go)
return lis
def decode_val(list_in):
list_code = []
lent = len(list_in)
d = form_dict()
for i in range(lent):
for value in d:
if list_in[i] == value:
list_code.append(d[value])
return list_code
We get our cipher indices and translate them into a string with the decode_val () function:
{0: [72, 107], 1: [101, 101], 2: [108, 121], 3: [108, 107], 4: [ 111, 101], 5: [32, 121], 6: [119, 107], 7: [111, 101], 8: [114, 121], 9: [108, 107], 10: [100, 101]}
Indexes: [52, 75, 102, 88, 85, 26, 99, 85, 108, 88, 74]
We get a coded super-secret message: 4KfXUcUlXJ
You can decode all this using the full_decode () function, the first argument of which is a list of cipher numeric indices, and the second is a list of key indices:
def full_decode(value, key):
dic = comparator(value, key)
print 'Deshifre=', dic
d = form_dict()
lis =[]
for v in dic:
go = (dic[v][0]-dic[v][1]+len(d)) % len(d)
lis.append(go)
return lis
All the same, we get the cipher indices and translate them into a string with the familiar decode_val () function:
[72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
And voila! Our encrypted word: Hello world
Well, the main challenge
if __name__ == "__main__":
word = 'Hello world'
key = 'key'
print 'Слово: '+ word
print 'Ключ: '+ key
key_encoded = encode_val(key)
value_encoded = encode_val(word)
print 'Value= ',value_encoded
print 'Key= ', key_encoded
shifre = full_encode(value_encoded, key_encoded)
print 'Шифр=', ''.join(decode_val(shifre))
decoded = full_decode(shifre, key_encoded)
print 'Decode list=', decoded
decode_word_list = decode_val(decoded)
print 'Word=',''.join(decode_word_list)
The article tried to describe everything so that it was as clear as possible even for the beginner in Python. Although this encryption algorithm is no longer 100% reliable, it is well suited for those who have taken the path of learning more serious things, for example, the same RSA.
Links and code:
Description of the Vigenere cipher on Wikipedia
Python source code