简单的异或红宝石 1.9.2
显然这曾经在 ruby 1.8.7 上工作,但不幸的是在 1.9.2 上不起作用
class String
def xor(key)
text = dup
text.length.times {|n| text[n] ^= key[n.modulo key.size] }
text
end
end
def encode(_original, _pass = 'testvendor')
_original.xor(_pass)
end
puts encode('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.')
#output=>
8
EE
DEBDREBDEVSR
TTTT
TNZV0D
SE E CRVSETENR D
TT
EKS0DSO VD
EVVTE S
RSREXE+E T
RR
T _TOEDE RO E
TTD
K
它返回
NoMethodError: undefined method `^' for "V":String
关于如何让它发挥作用有什么想法吗?
多谢
Apparently this used to work on ruby 1.8.7 but unfortunately not on 1.9.2
class String
def xor(key)
text = dup
text.length.times {|n| text[n] ^= key[n.modulo key.size] }
text
end
end
def encode(_original, _pass = 'testvendor')
_original.xor(_pass)
end
puts encode('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.')
#output=>
8
EE
DEBDREBDEVSR
TTTT
TNZV0D
SE E CRVSETENR D
TT
EKS0DSO VD
EVVTE S
RSREXE+E T
RR
T _TOEDE RO E
TTD
K
It returns
NoMethodError: undefined method `^' for "V":String
Any idea on how to get this working?
Thanks a lot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 1.8 中,
String#[ ]
方法返回一个 Fixnum,它是指定索引处的字节。在较新版本中,String#[]
返回一个字符串,因为字符串是由字符组成的,并且字符到字节的映射取决于编码。看起来你正在使用字符串作为字节缓冲区,所以你应该在数组而不是字符串中工作:然后使用它:
然后如果你真的想要一个字符串(它将包含一堆不可打印的控制字符和零字节和这样的事情),然后:
然后解压:
所有这些都应该始终保持 UTF-8,这就是你想要的。
如果需要,您可以将
xor
修补为 Enumerable 并跳过to_a
业务。您也可以调整它来修补字符串。您不应该再使用 String 作为字节缓冲区,最好使用 Fixnum 数组来进行显式编码处理。
In 1.8, the
String#[]
method returned a Fixnum which was the byte at the specified index. In newer version,String#[]
returns a String because strings are made of characters and the character-to-byte mapping depends on the encoding. Looks like you're using a String as a byte buffer so you should be working in Array instead of String:And then to use it:
Then if you really want a String (which will contain a bunch of unprintable control characters and zero bytes and such things), then:
And then to unpack:
All of this should maintain UTF-8 all the way through and that's what you want.
You could probably patch your
xor
into Enumerable and skip theto_a
business if desired. You could probably also adapt this to patch for String as well.You shouldn't be using String for byte buffers anymore, you're better off using arrays of Fixnum for that with explicit encoding handling.
调用
#ord
和#chr
方法将字符转换为数字表示形式,然后再转换回字符因此您的示例应该调用:
Call
#ord
and#chr
methods to convert from character to it's number representation and back to characterSo your example should call: