I have been trying to implement a little RSA algorithm to get used to ruby classes. I am getting the following error in terminal:
RSA.rb:22:in `encrypt': undefined method `each' for 42:Fixnum (NoMethodError) from RSA.rb:50
The catch is that I never call an "each" method, and even when I was using "array.each" my resources ( like rubymonk and Pragmatic Ruby) both indicate that this is correct.
I also read that you can override the "each" method in my class, however I did not think this was necessary. Do I need to do this? If so how?
Is there a better way to write any/all of this code? Is there anything stupid or javaish in this code (like maybe all those @ signs, do I really need them all)?
If you have questions about the algorithm you can find it here
class RSA def initialize( pIn, qIn, eIn, msg) @p = pIn @q = qIn @e = eIn @n = (@p * @q) @phiN = (@p-1) * (@q-1) @d = (@e ** (-1)) % @phiN @data = msg end def encrypt(plainText) #plainText.each do |thing| for i in plainText plainText.push( (data**(e))% N ) end print plainText return plainText end def decrypt(cipherText) #cipherText.each do |thing| for i in cipherText cipherText.push( (cipherText**(e))% N ) end print cipherText return cipherText end end # class message = RSA.new(51,37,87,[ 42, 24]) var = message.encrypt(42) message.decrypt(var)
Thanks.
*edited for copy
This post has been edited by muricula: 11 February 2012 - 11:46 PM

New Topic/Question
Reply



MultiQuote






|