gpt4 book ai didi

Ruby XOR 按位操作密码学练习

转载 作者:太空宇宙 更新时间:2023-11-03 15:59:50 25 4
gpt4 key购买 nike

我正在尝试将消息转换为 ASCII 十六进制值字符串,然后再转换回来。但是,我的 ^ 位明智的 XOR 运算符遇到了很多麻烦。我花了最后 4 个小时搜索 stackoverflow 关于 XOR 和位操作的类似问题,但我还没有看到任何建议可以解决这个问题。

我有一个 RakeTest 文件,由以下内容组成测试:

def test_xor
key = 'hi'
msg = 'Hello There, how are you?'
key_trunc = key
key_trunc << key while key.length < msg.length
key_trunc = Decrypt.truncate_key key_trunc, msg.length

ct = Decrypt.xor msg, key_trunc
assert_equal('200c040507493c010d1b0d454801071e48081a0c4810071c57', ct)
end

我已经手工计算(并使用在线十六进制转换器验证)正确的 ASCII 十六进制结果应该是上面的结果。这是我的解密模块:

module Decrypt

# Returns an array of ASCII Hex values
def self.to_hex_array(str_hex)
raise ArgumentError 'Argument is not a string.' unless str_hex.is_a? String

result = str_hex.unpack('C*').map { |e| e.to_s 16 }

result
end

def self.to_str_from_hex_array(hex_str)
return [hex_str].pack 'H*'
end

def self.xor(msg, key)

msg = self.to_hex_array msg
key = self.to_hex_array key
xor = []

(0...msg.length).each do |i|
xor.push msg[i] ^ key[i]
end

return xor.join
end

def self.truncate_key(str, len)
str = str.to_s[0...len] if str.length > len

return str

end

end

我已经在两个单独的 rake 测试函数中确认 to_hex_arrayto_str_from_hex_array 工作正常。当我运行上面的 rake 测试时,我得到一个 'NoMethodError: undefined method '^' for "48":String。 48 是十六进制的起始值,显然字符串不能进行按位运算,但我已经尝试了所有我能找到的方法来转换值,以便“^”能够正确运行。

我能得到的最接近的(没有抛出错误)是将循环内的操作更改为 msg[i].hex ^ key[i].hex,但这会输出一个 ASCII dec 值.有人能帮帮我吗?


编辑:感谢以下建议,我能够成功运行以下测试:

def test_xor
key = 'hi'
msg = 'Hello There, how are you?'
key_trunc = key
key_trunc << key while key.length < msg.length
key_trunc = Decrypt.truncate key_trunc, msg.length

ct = Decrypt.xor msg, key_trunc
assert_equal(['200c040507493c010d1b0d454801071e48081a0c4810071c57'], ct)
end

def test_decrypt
msg = 'attack at dawn'
key = '6c73d5240a948c86981bc294814d'
key = [key].pack('H*')

new_key = Decrypt.xor msg, key
assert_equal(['0d07a14569fface7ec3ba6f5f623'], new_key)
ct = Decrypt.xor 'attack at dusk', new_key.pack('H*')
assert_equal(['6c73d5240a948c86981bc2808548'], ct)
end

对于那些感兴趣的人,这里是成功的解密模块:

module Decrypt

# Returns an array of ASCII Hex values
def self.to_dec_array(str_hex)
raise ArgumentError, 'Argument is not a string!' unless str_hex.is_a? String

dec_array = str_hex.unpack('C*')

dec_array
end

def self.to_str_from_dec_array(dec_array)
raise ArgumentError, 'Argument is not an array!' unless dec_array.is_a? Array

return dec_array.pack 'C*'
end

def self.print_dec_array(dec_array)
return [dec]
end

def self.xor(msg, key)

msg = self.to_dec_array msg
key = self.to_dec_array key
xor = []

(0...msg.length).each do |i|
xor.push msg[i] ^ key[i]
end

xor = xor.pack('C*').unpack('H*')

xor
end

def self.truncate(str, len)
str = str.to_s[0...len] if str.length > len

return str

end

end

最佳答案

在您的 to_hex_array 方法中,您不应该将字节转换为字符串(您对 to_s 16 的调用)——这就是为什么您最终尝试异或字符串而不是整数

这确实意味着您的xor 方法将需要一个额外的步骤来将结果从整数数组转换为字符串——类似于

array_of_integers.map {|int| int.to_s(16)}.join

关于Ruby XOR 按位操作密码学练习,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27814716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com