diff --git a/README.md b/README.md index fde25e3..4302dbe 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,10 @@ decryptrot13 = Rot13::decrypt("uryy? }?.yqb") #"hello world!" #Binary encrypt Binary::encrypt("hello world") # "1101000 1100101 1101100 1101100 1101111 100000 1110111 1101111 1110010 1101100 1100100 100001" + +#Richard Zorge encrypt +zorg = Zorge.encrypt("HI") #981!" +zorgDec = Zorge.decrypt("981") #"Hi!" ``` ## Development diff --git a/lib/aaa_crypt.rb b/lib/aaa_crypt.rb index 397ec1c..403b166 100644 --- a/lib/aaa_crypt.rb +++ b/lib/aaa_crypt.rb @@ -6,7 +6,7 @@ require_relative 'cesar/cesar' require_relative 'visener/visener' require_relative 'aaa_crypt/rot13' - +require_relative 'aaa_crypt/zorge' module AaaCrypt class Error < StandardError; end diff --git a/lib/aaa_crypt/zorge.rb b/lib/aaa_crypt/zorge.rb new file mode 100644 index 0000000..a0f0f5c --- /dev/null +++ b/lib/aaa_crypt/zorge.rb @@ -0,0 +1,74 @@ +module AaaCrypt + module Zorge + def self.encrypt(text) + matrix = [['0','1','2','3','4','5','6','7','8','9'], + ['S','I','O','E','R','A','T','N'], + ['C','X','U','D','J','P','Z','B','K','Q'], + ['.','W','F','L','/','G','M','Y','H','V']] + encrypted_text = '' + + text.upcase.split('').map do |s| + matrix.each_with_index do |row, num1| + row.each_with_index do |cha, num2| + if s==cha + if num1==0 + encrypted_text+=num2.to_s+num2.to_s + else if num1==1 + encrypted_text+=num2.to_s + else + encrypted_text+=(num1+6).to_s+num2.to_s + end + end + end + end + end + end + encrypted_text + end + + + def self.decrypt(text) + matrix2 = [['0','1','2','3','4','5','6','7','8','9'], + ['S','I','O','E','R','A','T','N'], + ['C','X','U','D','J','P','Z','B','K','Q'], + ['.','W','F','L','/','G','M','Y','H','V']] + decrypted_text = '' + pchar = -2 + number_flag=0 + text+='0' + text.upcase.split('').map do |s| + if (pchar==9)&&(s.to_i==4) + if (number_flag==1) + number_flag=0 + else + number_flag=1 + end + end + + if (pchar==-2) + pchar = s.to_i + else + if (pchar==-1) + pchar = s.to_i + else + if (pchar==s.to_i)&&(number_flag==1) + decrypted_text+=s + pchar= -1 + else + if (pchar<8) + if (number_flag==0) + decrypted_text+=matrix2[1][pchar] + pchar=s.to_i + end + else + decrypted_text+=matrix2[pchar-6][s.to_i] + pchar = -1 + end + end + end + end + end + decrypted_text + end + end +end diff --git a/test/encryption/zorge_test.rb b/test/encryption/zorge_test.rb new file mode 100644 index 0000000..1f8e1e6 --- /dev/null +++ b/test/encryption/zorge_test.rb @@ -0,0 +1,47 @@ +require_relative "../test_helper" + +class Zorgetest < Minitest::Test + include AaaCrypt + include AaaCrypt::Zorge + + def test_zorge_encrypt_short + assert_equal"981", + Zorge::encrypt("HI") + end + + def test_zorge_decrypt_short + assert_equal"HI", + Zorge::decrypt("981") + end + + def test_zorge_encrypt_without_numbers + assert_equal"549697", + Zorge::encrypt("army") + end + + def test_zorge_decrypt_without_numbers + assert_equal"ARMY", + Zorge::decrypt("549697") + end + + def test_zorge_encrypt_with_numbers + assert_equal"54969794555594", + Zorge::encrypt("army/55/") + end + + def test_zorge_decrypt_with_numbers + assert_equal"ARMY/55/", + Zorge::decrypt("54969794555594") + end + + def test_zorge_encrypt_long_with_numbers + assert_equal"5496979455559496096929605960596", + Zorge::encrypt("ARMY/55/MSMFMSAMSAM") + end + + def test_zorge_decrypt_long_with_numbers + assert_equal"ARMY/55/MSMFMSAMSAM", + Zorge::decrypt("5496979455559496096929605960596") + end + +end \ No newline at end of file