The % is modulus (remainder) operator and ^ is bitwise. In this context, we take a file, and go through each character and encrypt it. But why are the ^ and % operators used here: def encrypt(reader, writer) key_index = 0 while not reader.eof? clear_char = reader.getc encrypted_char = clear_char ^ @key[key_index] writer.putc(encrypted_char) key_index = (key_index + 1) % @key.size end end thanks for response -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
John Merlino wrote in post #1044105:> The % is modulus (remainder) operator and ^ is bitwise. In this > context, we take a file, and go through each character and encrypt it. > But why are the ^ and % operators used here: > > def encrypt(reader, writer) > key_index = 0 > while not reader.eof? > clear_char = reader.getc > encrypted_char = clear_char ^ @key[key_index] > writer.putc(encrypted_char) > key_index = (key_index + 1) % @key.size > end > end > > thanks for responseThe use of XOR is a common operation in cryptography. I''m not sure how it all exactly works, but I have serious doubts about the security of this particular encrypt method. I would highly recommend using an actual encryption library, and never try to do it yourself. That is if you want any sense of real security at all. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
This is not actually a piece of code I would use, but rather something from a book thats a mental exercise. On Feb 4, 7:20 pm, Robert Walker <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> John Merlino wrote in post #1044105: > > > The % is modulus (remainder)operatorand ^ is bitwise. In this > >context, we take a file, and go through each character and encrypt it. > > But why are the ^ and % operators used here: > > > def encrypt(reader, writer) > > key_index = 0 > > while not reader.eof? > > clear_char = reader.getc > > encrypted_char = clear_char ^ @key[key_index] > > writer.putc(encrypted_char) > > key_index = (key_index + 1) % @key.size > > end > > end > > > thanks for response > > The use of XOR is a common operation in cryptography. I''m not sure how > it all exactly works, but I have serious doubts about the security of > this particular encrypt method. I would highly recommend using an actual > encryption library, and never try to do it yourself. That is if you want > any sense of real security at all. > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sat, Feb 4, 2012 at 18:43, John Merlino <stoicism1-YDxpq3io04c@public.gmane.org> wrote:> The % is modulus (remainder) operator and ^ is bitwise.Specifically, bitwise XOR. Very important to know what operation is being done.... :-)> But why are the ^ and % operators used here: > > def encrypt(reader, writer) > key_index = 0 > while not reader.eof? > clear_char = reader.getc > encrypted_char = clear_char ^ @key[key_index] > writer.putc(encrypted_char) > key_index = (key_index + 1) % @key.size > end > endYou''re using ^ because you are encrypting each char by doing a bitwise XOR with the corresponding byte of the key. You''re using % because the the key, in this case, is of finite length. If your plaintext is longer, then you have to repeat the key, i.e., start over at the beginning of the key but keep going within the plaintext. (Or of course come up with a new key, and find some way to tell the correspondent what the new key is, but that''s a whole ''nother problem.) Google things like basic cryptography, introduction to cryptography, cryptography tutorial, etc. for more info. -Dave -- Dave Aronson: Available Cleared Ruby on Rails Freelancer (NoVa/DC/Remote) -- see www.DaveAronson.com, and blogs at www.Codosaur.us, www.Dare2XL.com, www.RecruitingRants.com -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.