Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/aaa_crypt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
74 changes: 74 additions & 0 deletions lib/aaa_crypt/zorge.rb
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions test/encryption/zorge_test.rb
Original file line number Diff line number Diff line change
@@ -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