Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added vernam.rb and vigenere.rb #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/aaa_crypt.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# frozen_string_literal: true

require_relative "aaa_crypt/version"

require_relative "aaa_crypt/vernam"
require_relative "aaa_crypt/vigenere"
module AaaCrypt
class Error < StandardError; end
# Your code goes here...
Expand Down
53 changes: 53 additions & 0 deletions lib/aaa_crypt/vernam.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require 'securerandom'
class Vernam

#generates random key, len-length of key
def self.key_generate(len)
#key= SecureRandom.hex(len/2)
o = [('a'..'z'),('A'..'Z'),(0..9)].map(&:to_a).flatten
key = (0...len).map { o[rand(o.length)] }.join
key
end
#makes array of binary codes(for every char) from string
def self.string_to_bin(str)
str_new= String.new()
str.each_byte {|c| str_new += c.to_s(2)+" "}
arr=str_new.split(" ")
arr.each_with_index{|arr,i | while(arr.length<8)
arr[0]="0"+arr[0]
end}
arr
end
#vernam encryption
def self.vernam_encrypt(str,key)
length_str=str.length
arr=string_to_bin(str)
key_arr=string_to_bin(key)
res=Array.new()
arr.zip(key_arr).each {|arr, key_arr| res.push((arr.to_i(2) ^ key_arr.to_i(2)).to_s(2).rjust(arr.length, '0'))}
result=String.new()
res.each {|c| result += c+" "}
result

end
#makes string of chars from string of binary codes
def self.bin_to_string(str)
str_new= String.new()
arr=str.split(" ")
arr.each {|c| str_new += (c.to_i(2)).chr}
str_new
end
#vernam decryption
def self.vernam_decrypt(str,key)
length_str=str.length
arr=str.split(" ")
key_arr=string_to_bin(key)
res=Array.new()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

res = []

arr.zip(key_arr).each {|arr, key_arr| res.push((arr.to_i(2) ^ key_arr.to_i(2)).to_s(2).rjust(arr.length, '0'))}
result=String.new()
res.each {|c| result += c+" "}
resstr=bin_to_string(result)
resstr

end
end
36 changes: 36 additions & 0 deletions lib/aaa_crypt/vigenere.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'securerandom'
class Vigenere
#generates random key, len-length of future key (includes only downcase latin letters)
def self.key_generate(len)
#key= SecureRandom.hex(len/2)
o = [('a'..'z')].map(&:to_a).flatten
key = (0...len).map { o[rand(o.length)] }.join
key
end
# These Vigenere encryption and decryption are based on Vigenere table that includes only downcase latin letters
#vigenere encryption
def self.vigenere_encrypt(str,key)
alph=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
n = alph.length
res=String.new()
arr=str.chars
arrk=key.chars
arr.zip(arrk).each do |c,k| t=(alph.index(c)+alph.index(k))%n
res+=alph[t]
end
res
end
#vigenere decryption
def self.vigenere_decrypt(str,key)
alph=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, extract alph to class variable

n = alph.length
res=String.new()
arr=str.chars
arrk=key.chars
arr.zip(arrk).each do |c,k| t=(alph.index(c)+n -alph.index(k))%n
res+=alph[t]
end
res
end
end

31 changes: 31 additions & 0 deletions test/encryption/vernam_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require_relative "../test_helper"

class VernamTest < Minitest::Test
include AaaCrypt

def test_vernam_encrypt_a_with_key_1
assert_equal"01010000 ", Vernam.vernam_encrypt("a","1")
end
def test_vernam_decrypt_01010000
assert_equal"a", Vernam.vernam_decrypt("01010000 ","1")
end

def test_vernam_encrypt_Hello_world_with_key_wr3u44uejxm1
assert_equal"00111111 00010111 01011111 00011001 01011011 00010100 00000010 00001010 00011000 00010100 00001001 00010000 ", Vernam.vernam_encrypt("Hello world!","wr3u44uejxm1")
end
def test_vernam_decrypt_00111111_00010111_01011111_00011001_01011011_00010100_00000010_00001010_00011000_00010100_00001001_00010000
assert_equal"Hello world!", Vernam.vernam_decrypt("00111111 00010111 01011111 00011001 01011011 00010100 00000010 00001010 00011000 00010100 00001001 00010000 ","wr3u44uejxm1")
end

def test_vernam_encrypt_Number_11_with_key_fdt4udht7
assert_equal"00101000 00010001 00011001 01010110 00010000 00010110 01001000 01000101 00000110 ", Vernam.vernam_encrypt("Number 11","fdt4udht7")
end
def test_vernam_decrypt_00101000_00010001_00011001_01010110_00010000_00010110_01001000_01000101_00000110
assert_equal"Number 11", Vernam.vernam_decrypt("00101000 00010001 00011001 01010110 00010000 00010110 01001000 01000101 00000110 ","fdt4udht7")
end


end

33 changes: 33 additions & 0 deletions test/encryption/vigenere_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require_relative "../test_helper"

class VigenereTest < Minitest::Test
include AaaCrypt


def test_vigenere_encrypt_a_with_key_d
assert_equal"d", Vigenere.vigenere_encrypt("a","d")
end
def test_vernam_decrypt_d
assert_equal"a", Vigenere.vigenere_decrypt("d","d")
end

def test_vigenere_encrypt_abba_with_key_mama
assert_equal"mbna", Vigenere.vigenere_encrypt("abba","mama")
end
def test_vernam_decrypt_mbna
assert_equal"abba", Vigenere.vigenere_decrypt("mbna","mama")
end

def test_vigenere_encrypt_awesome_with_key_weekend
assert_equal"waicszh", Vigenere.vigenere_encrypt("awesome","weekend")
end
def test_vernam_decrypt_waicszh
assert_equal"awesome", Vigenere.vigenere_decrypt("waicszh","weekend")
end


end