0

Trying to run an rc4 algorithm and it's not recognizing the XOR method? Or is something else going on? I get the error when it gets to def process(text).

Error:

rc4.rb:26:in block in process': undefined method^' for "4":String (NoMethodError) from rc4.rb:26:in upto' from rc4.rb:26:inprocess' from rc4.rb:21:in encrypt' from rc4.rb:48:in'

Code:

class Rc4

def initialize(str)
    @q1, @q2 = 0, 0
    @key = []
    str.each_byte {|elem| @key << elem} while @key.size < 256
    @key.slice!(256..@key.size-1) if @key.size >= 256
    @s = (0..255).to_a
    j = 0 
    0.upto(255) do |i| 
      j = (j + @s[i] + @key[i] )%256
      @s[i], @s[j] = @s[j], @s[i]
    end    
  end

  def encrypt!(text)
    process text
  end  

  def encrypt(text)
    process text.dup
  end 

  private

  def process(text)
    0.upto(text.length-1) {|i| text[i] = text[i] ^ round}
    text
  end

  def round
    @q1 = (@q1 + 1)%256
    @q2 = (@q2 + @s[@q1])%256
    @s[@q1], @s[@q2] = @s[@q2], @s[@q1]
    @s[(@s[@q1]+@s[@q2])%256]  
  end

end

puts "Enter key."
    keyInput = gets.chomp
    keyInput = keyInput.to_s
    encryptInstance = Rc4.new(keyInput)
    decryptInstance = Rc4.new(keyInput)

  puts "Enter plaintext."
    plainInput = gets.chomp
    plainInput = plainInput.to_s
    cipherText = encryptInstance.encrypt(plainInput)

  puts "Plaintext is: " + plainInput

  puts "Ciphertext is: " + cipherText

  decryptedText = decryptInstance.encrypt(cipherText)

  puts "Decrypted text is: " + decryptedText
Corey
  • 264
  • 2
  • 10
  • What is your question? – sawa Oct 07 '14 at 19:41
  • Why am I getting this error / Why won't it run? – Corey Oct 07 '14 at 19:46
  • I'd worked on RC4 before, you can see the code here: https://github.com/suryart/spree_ebsin/blob/master/lib/spree_ebsin/rc4.rb – Surya Oct 07 '14 at 19:56
  • `0.upto(text.length-1) {|i| text[i] = text[i] ^ round}` will not work as you're trying to use string: `text[i]` in expression: `text[i] ^ round` it should be `text[i].to_i ^ round` or something. – Surya Oct 07 '14 at 20:01
  • Tried that and started getting another error that I posted below in comments on other response. – Corey Oct 07 '14 at 20:03

1 Answers1

1

text[i] is a string here. Use text[i].to_i

This should work

0.upto(text.length-1) {|i| text[i] = (text[i].ord ^ round).chr}

Since you're doing encryption, converting "4" to 4 would be a big mistake. We operate on the encodings and convert it back.

rohit89
  • 5,745
  • 2
  • 25
  • 42
  • rc4.rb:27:in `[]=': no implicit conversion of Fixnum into String (TypeError) from rc4.rb:27:in `block in process' from rc4.rb:27:in `upto' from rc4.rb:27:in `process' from rc4.rb:21:in `encrypt' from rc4.rb:49:in `
    '
    – Corey Oct 07 '14 at 19:51
  • Took this from https://github.com/maxprokopiev/ruby-rc4 so I thought it should work – Corey Oct 07 '14 at 19:55
  • Ok, this version runs, now how do I make the ciphertext show as normal numbers? This is what i got: Plaintext is: 123 11 81 99 232 5 Ciphertext is: ƻ?~??K??????6? Decrypted text is: 123 11 81 99 232 5 – Corey Oct 07 '14 at 20:22
  • What are you giving as inputs? – rohit89 Oct 07 '14 at 20:44
  • Enter key. 1234 5678 Enter plaintext. 9876 5432 Plaintext is: 9876 5432 Ciphertext is: ??T:?A?@s Decrypted text is: 9876 5432 – Corey Oct 07 '14 at 20:49
  • Encrypted characters are not always printable ascii characters. If you want to display ciphertext, you will need to encode it in hex and print. – rohit89 Oct 07 '14 at 21:16