Skip to content

Commit

Permalink
master: add support for brute forcing caesar
Browse files Browse the repository at this point in the history
  • Loading branch information
jodell committed Apr 24, 2010
1 parent 4e725f0 commit a41eba4
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 6 deletions.
30 changes: 30 additions & 0 deletions EXAMPLE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,36 @@ MVV
> toycipher -d caesar -k G mvv
FOO

* Brute forcing caesar:

> toycipher -d caesar -b foobar
ENNAZQ
DMMZYP
CLLYXO
BKKXWN
AJJWVM
ZIIVUL
YHHUTK
XGGTSJ
WFFSRI
VEERQH
UDDQPG
TCCPOF
SBBONE
RAANMD
QZZMLC
PYYLKB
OXXKJA
NWWJIZ
MVVIHY
LUUHGX
KTTGFW
JSSFEV
IRREDU
HQQDCT
GPPCBS
FOOBAR

== Multiple keys ==

> toycipher -e caesar -k G,M,A,R,K "The moose is loose"
Expand Down
6 changes: 6 additions & 0 deletions ROADMAP
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
-==-
* Rudimentary frequency analysis
* Caesar brute force guessing
* LICENSE
* Support supplying a key file

1.0

* Large text testing

1.1

Expand Down
8 changes: 2 additions & 6 deletions lib/toycipher/caesar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@ def initialize
@offset = 0
end

# TODO
def brute
(0..25).each do |i|
(@guess ||= []) << decrypt(ciphertext, i)
end
@guess
def brute(ciphertext = @ciphertext)
@alph.inject([]) { |acc, ch| acc << decrypt(ciphertext, ch) }
end

def encrypt(plaintext = @plaintext, offset = @offset)
Expand Down
13 changes: 13 additions & 0 deletions script/toycipher
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class OptParseToyCipher
opts.on("-t", "--transpose", "Transpose m x n lines of text to be n x m") do
options[:transpose] = true
end
opts.on("-b", "--brute", "Brute force if possible (Caesar)") do
options[:brute] = true
end
opts.on("-v", "--verbose", "Verbose setting") { options[:verbose] = true }
opts.on("-V", "--version", "Show version info") { puts "#{ToyCipher::VERSION_STR}"; exit 0; }
end
Expand Down Expand Up @@ -157,6 +160,11 @@ end

print_result(@results) if @results

if @options[:cipher]
#puts "Setting cipher to #{@options[:cipher]}"
@cipher = instance_eval "ToyCipher::#{@options[:cipher]}.new" # shouldn't have to do this per key
end

@cipher ||= ToyCipher::ToyCipherBase.new

if @options[:analysis]
Expand All @@ -171,4 +179,9 @@ if @options[:analysis]
end
end

if @options[:brute]
puts "Trying to brute force #{@options[:ciphertext]}" if @options[:verbose]
puts @cipher.brute @options[:ciphertext]
end


33 changes: 33 additions & 0 deletions test/tc_caesar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,38 @@ def test_decrypt
assert_equal @plaintext, @cipher.decrypt(@ciphertext, @offset)
end

def test_brute
ans = <<-ANS
CDECDE
BCDBCD
ABCABC
ZABZAB
YZAYZA
XYZXYZ
WXYWXY
VWXVWX
UVWUVW
TUVTUV
STUSTU
RSTRST
QRSQRS
PQRPQR
OPQOPQ
NOPNOP
MNOMNO
LMNLMN
KLMKLM
JKLJKL
IJKIJK
HIJHIJ
GHIGHI
FGHFGH
EFGEFG
DEFDEF
ANS
try = @cipher.brute(@ciphertext)
assert_equal ans.gsub(/\n/, ''), @cipher.brute(@ciphertext).to_s
end

end

35 changes: 35 additions & 0 deletions test/tc_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,41 @@ def test_analysis
Y: ########### (11)
Z: ##################### (21)
28,14,5,17,20,20,34,10,7,11,20,17,21,5,12,21,9,25,20,9,15,9,12,7,11,21
ANS
assert_equal ans, cli_out
end

def test_brute_caesar
args = "-d caesar -b 'FOOBAR'"
cli_out = %x[#{@cli} #{args}]
puts cli_out if verbose?
ans = <<-ANS
ENNAZQ
DMMZYP
CLLYXO
BKKXWN
AJJWVM
ZIIVUL
YHHUTK
XGGTSJ
WFFSRI
VEERQH
UDDQPG
TCCPOF
SBBONE
RAANMD
QZZMLC
PYYLKB
OXXKJA
NWWJIZ
MVVIHY
LUUHGX
KTTGFW
JSSFEV
IRREDU
HQQDCT
GPPCBS
FOOBAR
ANS
assert_equal ans, cli_out
end
Expand Down

0 comments on commit a41eba4

Please sign in to comment.