Взлом Vigenere cipher — различия между версиями
Материал из InformationSecurity WIKI
								
												
				| Drakylar (обсуждение | вклад)  (Новая страница: «  =Где часто используется=   =Скрипты=  ==python==  ==perl==  ==ruby==  ==bash==  ==C/C++==  =Утилиты=   =Райтапы=    =С…») | Drakylar (обсуждение | вклад)  м | ||
| (не показано 10 промежуточных версий этого же участника) | |||
| Строка 1: | Строка 1: | ||
| + | Является производным шифром от шифра Цезаря. | ||
| + | [[Категория:Crypto]] | ||
| + | ==Где часто используется== | ||
| − | = | + | ==Скрипты== | 
| + | ===python=== | ||
| + | ====encrypt==== | ||
| + | <syntaxhighlight lang="python" line> | ||
| + | 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 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> | ||
| − | == | + | ===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
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 resultperl
ruby
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
Java
encrypt
    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;
    }decrypt
    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;
    }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/ - еще один калькулятор
