简单的异或红宝石 1.9.2

发布于 2024-12-02 23:00:06 字数 985 浏览 4 评论 0原文

显然这曾经在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゝ偶尔ゞ 2024-12-09 23:00:06

在 1.8 中,String#[ ] 方法返回一个 Fixnum,它是指定索引处的字节。在较新版本中,String#[] 返回一个字符串,因为字符串是由字符组成的,并且字符到字节的映射取决于编码。看起来你正在使用字符串作为字节缓冲区,所以你应该在数组而不是字符串中工作:

class Array
  def xor(key)
     a = dup
     a.length.times { |n| a[n] ^= key[n % key.size] }
     a
  end
end

然后使用它:

mangled_array = string.codepoints.to_a.xor(key.codepoints.to_a)

然后如果你真的想要一个字符串(它将包含一堆不可打印的控制字符和零字节和这样的事情),然后:

mangled_string = mangled_array.inject('') { |s,c| s << c }

然后解压:

mangled_string.
  codepoints.
  to_a.
  xor(key.codepoints.to_a).
  inject('') { |s,c| s << c }

所有这些都应该始终保持 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:

class Array
  def xor(key)
     a = dup
     a.length.times { |n| a[n] ^= key[n % key.size] }
     a
  end
end

And then to use it:

mangled_array = string.codepoints.to_a.xor(key.codepoints.to_a)

Then if you really want a String (which will contain a bunch of unprintable control characters and zero bytes and such things), then:

mangled_string = mangled_array.inject('') { |s,c| s << c }

And then to unpack:

mangled_string.
  codepoints.
  to_a.
  xor(key.codepoints.to_a).
  inject('') { |s,c| s << c }

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 the to_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.

我也只是我 2024-12-09 23:00:06

调用 #ord#chr 方法将字符转换为数字表示形式,然后再转换回字符

因此您的示例应该调用:

text.length.times {|n| text[n] = (text[n].ord ^ key[n.modulo key.size].ord).chr }

Call #ord and #chr methods to convert from character to it's number representation and back to character

So your example should call:

text.length.times {|n| text[n] = (text[n].ord ^ key[n.modulo key.size].ord).chr }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文