Skip to content

Instantly share code, notes, and snippets.

@jjarmoc jjarmoc/xorfile.rb
Created May 31, 2012

Embed
What would you like to do?
XOR a file with a single byte key, save as file.xor
# XOR an input file with a single byte, save as input.xor
# xorfile(0xff, input)
def xorfile(key, file)
File.open("#{file}.xor", 'w') {|f| f.write(File.open("#{file}","rb") {|io| io.read}.unpack('C*').map{|x| x ^ key}.pack('C*')) }
end
# string pack/unpack w/ XOR
"ABCD".unpack('C*').collect{|x| (x ^ 0xa2).chr}.join
=> "\xE3\xE0\xE1\xE6"
"E3E0E1E6".scan(/../).collect{|x| (x.to_i(16) ^ 0xa2).chr}.join
=> "ABCD"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.