Взлом 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
def encryption(plaintext, keyword):
txt_len = len(plaintext)
keyword *= txt_len // len(keyword) + 1
keyword = keyword[:txt_len]
encoded = ""
for c in range(txt_len):
newchar = ord(plaintext[c]) + ord(keyword[c]) - 194
newchar %= 25
encoded += chr(newchar + 97)
return encodeddecrypt
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