📜 ⬆️ ⬇️

Vizhenera cipher. Python parsing algorithm

Recently, I wanted to remember my “spy” childhood and at least to study the basic methods of encryption at least. And the first choice fell on the cipher Vizhenera. By itself, it is not extremely difficult, but for a long time was considered crypto-resistant. Century commercials from XV and to the very XIX, until someone Kazizki completely cracked the cipher.
However, we restrict the quoting of Wikipedia only to the description of the algorithm itself.

The method is an improved Caesar cipher, where the letters were shifted to a certain position.
Vigener's cipher consists of a sequence of several Caesar ciphers with different shift values.

Suppose we have a certain alphabet, where each letter corresponds to the numbers:

image
')
Then if the letters az correspond to the numbers 0-25, then Vigener's encryption can be written as a formula:

image

Decryption:

image

In fact, we do not need anything more than these two formulas, and we can proceed to the implementation.

Here I want to say that I tried to implement the algorithm is not easier and more elegant, but the most understandable and detailed.
Actually proceed, sir.

Encode the words 'Hello world' with a cunning 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 match the letters in our word with the letters in the dictionary and assign them the appropriate 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 coded 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 associate the key indexes with the indexes 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 convert them to a string using 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]}

Indices: [52, 75, 102, 88, 85, 26, 99, 85, 108, 88, 74]

We receive the coded supersecret message: 4KfXUcUlXJ

All this can be decoded using the full_decode () function, the first argument of which is the list of numeric indices of the cipher, and the second is the list of indexes of the key:

 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 


Still, 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 to even the most 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:
Vigenere cipher description on Wikipedia
Python source code

Source: https://habr.com/ru/post/140820/


All Articles