Skip to content

Commit

Permalink
master: add example, check bug off, normalize caesar decrypt, test fo…
Browse files Browse the repository at this point in the history
…r transpose
  • Loading branch information
jodell committed Apr 8, 2010
1 parent c3186df commit b69c6a4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion BUGS
Original file line number Diff line number Diff line change
@@ -1 +1 @@
caesar needs to support either letter or numeric keys
caesar needs to support either letter or numeric keys - FIXED
22 changes: 22 additions & 0 deletions EXAMPLE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

== Caesar ==

> toycipher -e caesar -k G foo
MVV
> toycipher -d caesar -k G mvv
FOO

== Multiple keys ==

> toycipher -e caesar -k G,M,A,R,K "The moose is loose"
AOLTVVZLPZSVVZL
GURZBBFRVFYBBFR
UIFNPPTFJTMPPTF
LZWEGGKWAKDGGKW
ESPXZZDPTDWZZDP

== Playfair ==




2 changes: 1 addition & 1 deletion lib/toycipher/caesar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def decrypt(ciphertext = @ciphertext, offset = @offset)
offset = normalize_key(offset)
#puts "Trying to decrypt #{ciphertext} with offset #{offset}:#{offset.class}"
@plaintext = ''
ciphertext.each_byte { |b| @plaintext += mod_shift(b.chr, -offset) }
normalize(ciphertext).each_byte { |b| @plaintext += mod_shift(b.chr, -offset) }
@plaintext
end

Expand Down
23 changes: 22 additions & 1 deletion test/tc_toycipher.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

$: << File.expand_path(File.dirname(__FILE__) + '/../lib/')
require 'test/unit'
require 'toycipher'

Expand Down Expand Up @@ -58,4 +58,25 @@ def test_one_time_pad
assert_equal @tc.otp(plaintext, key), ciphertext
end

def test_matrix_transposition
matrix = ['AB', 'AB']
ans = ['AA', 'BB']
assert_equal ToyCipher::ToyCipherUtil.transpose(matrix), ans
assert_equal ToyCipher::ToyCipherUtil.transpose2(matrix), ans
matrix = ['ABC', 'DEF', 'GHI']
ans = ['ADG', 'BEH', 'CFI']
assert_equal ToyCipher::ToyCipherUtil.transpose(matrix), ans
assert_equal ToyCipher::ToyCipherUtil.transpose2(matrix), ans
end

def test_matrix_trans_exception
matrix = ['AB', 'ABC']
assert_raise Exception do
ToyCipher::ToyCipherUtil.transpose(matrix)
end
assert_raise Exception do
ToyCipher::ToyCipherUtil.transpose2(matrix)
end
end
end

0 comments on commit b69c6a4

Please sign in to comment.