Archive for October, 2006

31st Oct 2006

More on ROT13

Thanks to ThisService, I’ve turned the previously mentioned ROT13 ruby script into a system-wide Mac OS X service. Simply select the text you want encrypted/decrypted, hit CMD-Shift-R or select ROT13 from the service menu, and your text will be ROT13-ified.

Thanks, Jesper!

Dowload it here: ROT13service.zip

Posted by Posted by patrick under Filed under ROT13, apple, code, crypto, open-source Comments 1 Comment »

05th Oct 2006

N yvggyr Ehol…

I’ve been asked to lead a Ruby session at my school for a small group of students interested in learning the language. As such, I’ve been brushing up on it lately, and I came up with a fun little script I thought I’d share.

Our topic is cryptography, and in the spirit of fun I thought I would show how easy it is to write a ROT13 cipher in Ruby. Here it is:

def uppercase_swap(byte)
  if /[A-M]/ === byte.chr then byte += 13
  else byte -= 13 end
end

def lowercase_swap(byte)
  if /[a-m]/ === byte.chr then byte += 13
  else byte -= 13 end
end

ARGV.shift.each_byte do |byte|
  byte = uppercase_swap byte if /[A-Z]/ =~ byte.chr
  byte = lowercase_swap byte if /[a-z]/ =~ byte.chr
  print byte.chr.to_s
end
print "\n"

As you can see, it’s not very big. Ignoring the 2 function definitions at the top momentarily, all we are doing is taking the first argument (ARGV.shift), and iterating through each byte –each character of the string. With each byte, we are checking to see if it is an upper or lowercase letter and calling the appropriate function to swap it with a different letter if it is.

Simple. Nice.

If anyone has any input on how to improve this code, please let me know. UPDATE: I made it better, here’s how:

def swapLetter(byte)
  if /[A-M]|[a-m]/ === byte.chr then byte += 13
  else byte -= 13 end
end

ARGV.shift.each_byte do |byte|
  byte = swapLetter(byte) if /[A-Z]|[a-z]/ =~ byte.chr
  print byte.chr.to_s
end
print "\n"

Merging functions is goood.

Posted by Posted by patrick under Filed under crypto, ruby Comments 2 Comments »