-
Notifications
You must be signed in to change notification settings - Fork 7
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
anastasiiiaaa
wants to merge
2
commits into
mmcs-ruby:master
Choose a base branch
from
anastasiiiaaa:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, extract |
||
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
res = []