Взлом Vigenere cipher — различия между версиями
Материал из InformationSecurity WIKI
Drakylar (обсуждение | вклад) м (→python) |
Drakylar (обсуждение | вклад) м |
||
(не показано 8 промежуточных версий этого же участника) | |||
Строка 1: | Строка 1: | ||
+ | Является производным шифром от шифра Цезаря. | ||
+ | [[Категория:Crypto]] | ||
+ | ==Где часто используется== | ||
− | = | + | ==Скрипты== |
− | + | ===python=== | |
− | = | + | ====encrypt==== |
− | |||
− | == | ||
<syntaxhighlight lang="python" line> | <syntaxhighlight lang="python" line> | ||
def encryption(plaintext, keyword): | def encryption(plaintext, keyword): | ||
Строка 16: | Строка 17: | ||
newchar = ord(plaintext[c]) + ord(keyword[c]) - 194 | newchar = ord(plaintext[c]) + ord(keyword[c]) - 194 | ||
newchar %= 25 | newchar %= 25 | ||
− | encoded += chr(newchar + 97 | + | encoded += chr(newchar + 97) |
return encoded | return encoded | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | ==perl== | + | ====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> | ||
+ | |||
+ | ===perl=== | ||
+ | |||
+ | ===ruby=== | ||
+ | <syntaxhighlight lang="ruby" line> | ||
+ | module VigenereCipher | ||
+ | |||
+ | BASE = 'A'.ord | ||
+ | SIZE = 'Z'.ord - BASE + 1 | ||
+ | |||
+ | def encrypt(text, key) | ||
+ | crypt(text, key, :+) | ||
+ | end | ||
+ | |||
+ | def decrypt(text, key) | ||
+ | crypt(text, key, :-) | ||
+ | end | ||
+ | |||
+ | def crypt(text, key, dir) | ||
+ | text = text.upcase.gsub(/[^A-Z]/, '') | ||
+ | key_iterator = key.upcase.gsub(/[^A-Z]/, '').chars.map{|c| c.ord - BASE}.cycle | ||
+ | text.each_char.inject('') do |ciphertext, char| | ||
+ | offset = key_iterator.next | ||
+ | ciphertext << ((char.ord - BASE).send(dir, offset) % SIZE + BASE).chr | ||
+ | end | ||
+ | end | ||
+ | |||
+ | end | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | ===Java=== | ||
+ | ====encrypt==== | ||
+ | <syntaxhighlight lang="ruby" line> | ||
+ | static String encrypt(String text, final String key) { | ||
+ | String res = ""; | ||
+ | text = text.toUpperCase(); | ||
+ | for (int i = 0, j = 0; i < text.length(); i++) { | ||
+ | char c = text.charAt(i); | ||
+ | if (c < 'A' || c > 'Z') continue; | ||
+ | res += (char)((c + key.charAt(j) - 2 * 'A') % 26 + 'A'); | ||
+ | j = ++j % key.length(); | ||
+ | } | ||
+ | return res; | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | ====decrypt==== | ||
+ | <syntaxhighlight lang="ruby" line> | ||
+ | static String decrypt(String text, final String key) { | ||
+ | String res = ""; | ||
+ | text = text.toUpperCase(); | ||
+ | for (int i = 0, j = 0; i < text.length(); i++) { | ||
+ | char c = text.charAt(i); | ||
+ | if (c < 'A' || c > 'Z') continue; | ||
+ | res += (char)((c - key.charAt(j) + 26) % 26 + 'A'); | ||
+ | j = ++j % key.length(); | ||
+ | } | ||
+ | return res; | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | ===C/C++=== | ||
+ | |||
+ | ==Утилиты== | ||
+ | |||
+ | https://www.cryptool.org/ - одна из лучших утилит для криптографии | ||
+ | |||
+ | ==Райтапы== | ||
+ | |||
+ | https://github.com/ctfs/write-ups-2014/tree/master/defkthon-ctf/crypto-100 - DEFKTHON CTF: Crypto 100 | ||
+ | |||
+ | |||
+ | http://0xa.li/th3jackers-2015-ctf-crypto200-writeup/ - Codegate CTF 2011 Crypto 200 | ||
+ | |||
− | = | + | http://www.nobbd.de/blog/artikel.php?titel=write-up-Ekoparty-CTF---Crypto-50-100-200 - Crypto 100 |
− | == | + | ==Ссылки== |
− | + | https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher - официальная вики статья | |
− | |||
+ | http://www.counton.org/explorer/codebreaking/vigenere-cipher.php - один из калькуляторов | ||
− | |||
+ | http://planetcalc.ru/2468/ - русский вариант шифра виженера | ||
− | + | http://planetcalc.com/2468/ - еще один калькулятор |
Текущая версия на 13:11, 2 июня 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
perl
ruby
1 module VigenereCipher
2
3 BASE = 'A'.ord
4 SIZE = 'Z'.ord - BASE + 1
5
6 def encrypt(text, key)
7 crypt(text, key, :+)
8 end
9
10 def decrypt(text, key)
11 crypt(text, key, :-)
12 end
13
14 def crypt(text, key, dir)
15 text = text.upcase.gsub(/[^A-Z]/, '')
16 key_iterator = key.upcase.gsub(/[^A-Z]/, '').chars.map{|c| c.ord - BASE}.cycle
17 text.each_char.inject('') do |ciphertext, char|
18 offset = key_iterator.next
19 ciphertext << ((char.ord - BASE).send(dir, offset) % SIZE + BASE).chr
20 end
21 end
22
23 end
Java
encrypt
1 static String encrypt(String text, final String key) {
2 String res = "";
3 text = text.toUpperCase();
4 for (int i = 0, j = 0; i < text.length(); i++) {
5 char c = text.charAt(i);
6 if (c < 'A' || c > 'Z') continue;
7 res += (char)((c + key.charAt(j) - 2 * 'A') % 26 + 'A');
8 j = ++j % key.length();
9 }
10 return res;
11 }
decrypt
1 static String decrypt(String text, final String key) {
2 String res = "";
3 text = text.toUpperCase();
4 for (int i = 0, j = 0; i < text.length(); i++) {
5 char c = text.charAt(i);
6 if (c < 'A' || c > 'Z') continue;
7 res += (char)((c - key.charAt(j) + 26) % 26 + 'A');
8 j = ++j % key.length();
9 }
10 return res;
11 }
C/C++
Утилиты
https://www.cryptool.org/ - одна из лучших утилит для криптографии
Райтапы
https://github.com/ctfs/write-ups-2014/tree/master/defkthon-ctf/crypto-100 - DEFKTHON CTF: Crypto 100
http://0xa.li/th3jackers-2015-ctf-crypto200-writeup/ - Codegate CTF 2011 Crypto 200
http://www.nobbd.de/blog/artikel.php?titel=write-up-Ekoparty-CTF---Crypto-50-100-200 - Crypto 100
Ссылки
https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher - официальная вики статья
http://www.counton.org/explorer/codebreaking/vigenere-cipher.php - один из калькуляторов
http://planetcalc.ru/2468/ - русский вариант шифра виженера
http://planetcalc.com/2468/ - еще один калькулятор