XOR Encryption using Ruby

Recently i have a college assignment, to create an XOR encryption and decryption apps. I decided to use ruby to finish this assignment.

there is the source code :

class XOREncrypt
  def initialize(inputfile, password, outputfile)
    input = File.open(inputfile,"r")
    pass_array = password.split(//)
    output = File.new(outputfile,"w")
    i = 0
    while c = input.getc
      pass_char = pass_array[i]
      xor = c.chr[0] ^ pass_char[0]
      output.print(xor.chr)
      i+=1
      if i == (pass_array.size - 1)
        i = 0
      end
    end
    input.close
    output.close
  end
end

puts "Filename for Input : "
inputfile = gets
puts "Insert Password : "
password = gets
puts "Filename for Output : "
outputfile = gets
XOREncrypt.new(inputfile.chomp, password.chomp, outputfile.chomp)

* note :
this apps can by used for encypting and decryting too.
this apps tested and created using ruby 1.8.7

* update :
to use it with ruby 1.9 change the line 9 to

xor = c.ord ^ pass_char.ord
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s