From b69c6a49355167027c3c84ca8af3dc5a97ba56b8 Mon Sep 17 00:00:00 2001 From: Jeffrey ODell Date: Wed, 7 Apr 2010 19:50:02 -0500 Subject: [PATCH] master: add example, check bug off, normalize caesar decrypt, test for transpose --- BUGS | 2 +- EXAMPLE | 22 ++++++++++++++++++++++ lib/toycipher/caesar.rb | 2 +- test/tc_toycipher.rb | 23 ++++++++++++++++++++++- 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 EXAMPLE diff --git a/BUGS b/BUGS index 4de3347..f66de3b 100644 --- a/BUGS +++ b/BUGS @@ -1 +1 @@ -caesar needs to support either letter or numeric keys +caesar needs to support either letter or numeric keys - FIXED diff --git a/EXAMPLE b/EXAMPLE new file mode 100644 index 0000000..8e61575 --- /dev/null +++ b/EXAMPLE @@ -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 == + + + + diff --git a/lib/toycipher/caesar.rb b/lib/toycipher/caesar.rb index cdd9cfe..2773260 100644 --- a/lib/toycipher/caesar.rb +++ b/lib/toycipher/caesar.rb @@ -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 diff --git a/test/tc_toycipher.rb b/test/tc_toycipher.rb index 76397e3..b4b27eb 100755 --- a/test/tc_toycipher.rb +++ b/test/tc_toycipher.rb @@ -1,4 +1,4 @@ - +$: << File.expand_path(File.dirname(__FILE__) + '/../lib/') require 'test/unit' require 'toycipher' @@ -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 +