Взлом Vigenere cipher — различия между версиями
Материал из InformationSecurity WIKI
Drakylar (обсуждение | вклад) м (→python) |
Drakylar (обсуждение | вклад) м (→python) |
||
Строка 7: | Строка 7: | ||
==python== | ==python== | ||
+ | ===encrypt=== | ||
<syntaxhighlight lang="python" line> | <syntaxhighlight lang="python" line> | ||
def encryption(plaintext, keyword): | def encryption(plaintext, keyword): | ||
Строка 18: | Строка 19: | ||
encoded += chr(newchar + 97) | encoded += chr(newchar + 97) | ||
return encoded | return encoded | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ===decrypt=== | ||
+ | <syntaxhighlight lang="python" line> | ||
+ | def decrypt(key, ciphertext): | ||
+ | from itertools import cycle | ||
+ | ALPHA = 'abcdefghijklmnopqrstuvwxyz' | ||
+ | pairs = zip(ciphertext, cycle(key)) | ||
+ | result = '' | ||
+ | for pair in pairs: | ||
+ | total = reduce(lambda x, y: ALPHA.index(x) - ALPHA.index(y), pair) | ||
+ | result += ALPHA[total % 26] | ||
+ | return result | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Версия 12:02, 1 июня 2016
Содержание
Где часто используется
Скрипты
python
encrypt
1 def encryption(plaintext, keyword):
2 txt_len = len(plaintext)
3 keyword *= txt_len // len(keyword) + 1
4 keyword = keyword[:txt_len]
5 encoded = ""
6 for c in range(txt_len):
7 newchar = ord(plaintext[c]) + ord(keyword[c]) - 194
8 newchar %= 25
9 encoded += chr(newchar + 97)
10 return encoded
decrypt
1 def decrypt(key, ciphertext):
2 from itertools import cycle
3 ALPHA = 'abcdefghijklmnopqrstuvwxyz'
4 pairs = zip(ciphertext, cycle(key))
5 result = ''
6 for pair in pairs:
7 total = reduce(lambda x, y: ALPHA.index(x) - ALPHA.index(y), pair)
8 result += ALPHA[total % 26]
9 return result