0

i found one lib which is here
https://github.com/juggler/ruby-rc4/blob/master/lib/rc4.rb

But its not working for ruby 1.9.x
it throws an error undefined method ^' for "\x1A":String__

is there any work around in ruby 1.9.x?

after googling i came to know that "In Ruby 1.9 string[index] returns character not code of the character (like it was in 1.8)." (https://rails.lighthouseapp.com/projects/8994/tickets/3144-undefined-method-for-string-ror-234)

Thanks in advance

Krishna Prasad Varma
  • 4,670
  • 5
  • 29
  • 41

2 Answers2

1

If that (one operation) is the only problem, couldn't you just modify the library a bit?

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
  • i dont know how this RC4 decryption works. i tried it text[i].ord but still its throwing error .TypeError: can't convert Fixnum into String . – Krishna Prasad Varma Feb 05 '11 at 05:16
  • what line are you getting an error on. If you are getting that error, you are not dealing with a string, it is a number. Looking at the code, you only need to change 0.upto(text.length-1) {|i| text[i] = text[i] ^ round} The initializer is ok for 1.9 as it uses each_byte – Michael Papile Feb 05 '11 at 05:23
1

https://github.com/dotemacs/ruby-rc4/tree/master/lib This guy has forked it, and added some tests. The original is not maintained it looks like. This library is extremely simple and to fix that you just have to use the ord method on String.

 s = "foo"
 s[0].ord

So clone that guys git, fix it, and send him a pull request, it is the open source way :) if he does not respond then keep your fork and people will then use that.

EDIT

change line 27 to:

  0.upto(text.length-1) {|i| text[i] = [text[i].ord ^round].pack('c')}
Michael Papile
  • 6,836
  • 30
  • 30
  • The error is gone but its still not working for me . did u try the lib with ruby 1.9.2 . am not able to descrypt the data – Krishna Prasad Varma Feb 05 '11 at 13:45
  • It worked fine for me. I used the encrypt then the decrypt and it round tripped. – Michael Papile Feb 05 '11 at 17:27
  • This fork worked fine for me with Ruby 1.9.3: https://github.com/benolee/ruby-rc4 I forked that to fix an issue where each RC4 instance could only be used once: https://github.com/henrik/ruby-rc4 – Henrik N Jan 14 '12 at 11:56