Взлом Vigenere cipher — различия между версиями

Материал из InformationSecurity WIKI
Перейти к: навигация, поиск
м (python)
м
Строка 37: Строка 37:
  
 
==ruby==
 
==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>
 +
  
 
==bash==
 
==bash==

Версия 12:07, 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

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


bash

C/C++

Утилиты

Райтапы

Ссылки