1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#Input string msg = "I wonder if this code can be better." #Randomly-generated key key = "s7eOhg2fqgPRWXtp303B67551G5zpEyHe78q5NR5eWKP9eq9fk54D23" #This is the only way I can figure out how to store the result of the .each loops ciphered = "" deciphered = "" beg = 32 #Equivalent to ASCII's {space} in decimal fin = 91 #Equivalent to the distance between ASCII {space} and ASCII {z} (0..msg.length-1).each do |i| foo = (msg[i] - beg) + (key[i] - beg) foo %= fin if foo > fin foo += beg ciphered << foo end (0..msg.length-1).each do |i| foo = (ciphered[i] - beg) - (key[i] - beg) foo += fin if foo < 0 foo += beg deciphered << foo end #msg should now be the same as deciphered puts msg puts ciphered puts deciphered
Refactorings
No refactoring yet !
amoeba.myopenid.com
November 6, 2007, November 06, 2007 03:31, permalink
Some optimizations.
+Use of .times method
+Simplified math
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
msg = "I wonder if this code can be better." key = "s7eOhg2fqgPRWXtp303B67551G5zpEyHe78q5NR5eWKP9eq9fk54D23" ciphered = "" deciphered = "" beg = 32 fin = 91 msg.length.times do |i| foo = msg[i] + key[i] - 2 * beg foo %= fin if foo > fin foo += beg ciphered << foo end msg.length.times do |i| foo = ciphered[i] - key[i] foo += fin if foo < 0 foo += beg deciphered << foo end puts msg puts ciphered puts deciphered
amoeba.myopenid.com
November 6, 2007, November 06, 2007 03:40, permalink
Wow.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
msg = "I wonder if this code can be better." key = "s7eOhg2fqgPRWXtp303B67551G5zpEyHe78q5NR5eWKP9eq9fk54D23" ciphered = "" deciphered = "" offset = 32 range = 91 msg.length.times do |i| ciphered << (msg[i] + key[i] - 2*offset) % range + offset end msg.length.times do |i| deciphered << (ciphered[i] - key[i]) % range + offset end puts msg puts ciphered puts deciphered
rpheath
November 6, 2007, November 06, 2007 23:16, permalink
Maybe I'm not thinking clearly, but why are there two separate loops? Couldn't that be combined?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
msg = "I wonder if this code can be better." key = "s7eOhg2fqgPRWXtp303B67551G5zpEyHe78q5NR5eWKP9eq9fk54D23" ciphered, deciphered = "", "" offset, range = 32, 91 msg.length.times do |i| ciphered << (msg[i] + key[i] - 2*offset) % range + offset deciphered << (ciphered[i] - key[i]) % range + offset end puts msg puts ciphered puts deciphered
Jason Dew
November 7, 2007, November 07, 2007 03:46, permalink
this code will be much cleaner in Ruby 1.9 when we get external iterators.... but, i formed the code into a class, cleaned up the math a bit, and made the cipher reuse the key if the string to be encoded/decoded is longer than the key. i also added a very simple spec (using RSpec)
class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
class Cipher Offset = ?\ # ascii value of a space Range = ?z - ?\ # difference between ascii values for z and space def initialize(key) @key = key @key_length = key.size end def encode( clear_text ) cipher_text = String.new index = 0 clear_text.each_byte do |byte| cipher_text << (byte + @key[index % @key_length] - 2*Offset) % Range + Offset index += 1 end cipher_text end def decode( cipher_text ) clear_text = String.new index = 0; cipher_text.each_byte do |byte| clear_text << (byte - @key[index % @key_length] + Range) % Range + Offset index += 1 end clear_text end end
spec
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
require 'cipher' describe Cipher, "with a sufficient key size" do before do @cipher = Cipher.new( "s7eOhg2fqgPRWXtp303B67551G5zpEyHe78q5NR5eWKP9eq9fk54D23" ) end it "should correctly encode a string" do @cipher.encode( "I wonder if this code can be better." ).should == 'B7bD\Qw^qV<RQFci3s(,!7xv%Gwep-dB_"0%' end it "should correctly decode a string" do @cipher.decode( 'B7bD\Qw^qV<RQFci3s(,!7xv%Gwep-dB_"0%' ).should == "I wonder if this code can be better." end end describe Cipher, "without a sufficient key size" do before do @cipher = Cipher.new( "s3EHqP9f23" ) end it "should correctly encode a string" do @cipher.encode( "I wonder if this code can be better." ).should == "B3B=e:$^2\"_3?6`I9O'w^3./eP!Q2u^-?3i^" end it "should correctly decode a string" do @cipher.decode( "B3B=e:$^2\"_3?6`I9O'w^3./eP!Q2u^-?3i^" ).should == "I wonder if this code can be better." end end
Sam
November 10, 2007, November 10, 2007 01:28, permalink
getting rid of 'ciphered, deciphered = "", ""'
1 2 3 4 5 6 7 8 9 10 11 12
msg = "I wonder if this code can be better." key = "s7eOhg2fqgPRWXtp303B67551G5zpEyHe78q5NR5eWKP9eq9fk54D23" offset, range = 32, 91 msg.length.times do |i| (ciphered = '') << (msg[i] + key[i] - 2*offset) % range + offset (deciphered = '') << (ciphered[i] - key[i]) % range + offset end puts msg puts ciphered puts deciphered
Simple cipher program. Modifies an initial message by some key, using their ASCII values to perform the math.
Intended to be easily converted to other programming languages.
Input can contain A-z, numbers, apostrophes and spaces (and nothing else).
Ciphered text needs to be sent via HTTP GET request (i.e. http://mysite.com/#{ciphered})