From 6a74c38707eb109c936ad5254cfd3fbaab2ff6bd Mon Sep 17 00:00:00 2001 From: Snow Helsing Date: Thu, 16 Jun 2016 01:36:33 +0800 Subject: [PATCH] FIX: support utf-8 string --- lib/rc4.rb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/rc4.rb b/lib/rc4.rb index cbcd2d8..534d5e7 100644 --- a/lib/rc4.rb +++ b/lib/rc4.rb @@ -8,15 +8,12 @@ def initialize(str) end def encrypt!(text) - index = 0 - while index < text.length + text.force_encoding('utf-8').unpack('U*').map do |code| @q1 = (@q1 + 1) % 256 @q2 = (@q2 + @state[@q1]) % 256 @state[@q1], @state[@q2] = @state[@q2], @state[@q1] - text.setbyte(index, text.getbyte(index) ^ @state[(@state[@q1] + @state[@q2]) % 256]) - index += 1 - end - text + code ^ @state[(@state[@q1] + @state[@q2]) % 256] + end.pack 'U*' end alias_method :decrypt!, :encrypt!