Skip to content

Instantly share code, notes, and snippets.

@jjarmoc
Created May 31, 2012 23:04
Show Gist options
  • Save jjarmoc/2847053 to your computer and use it in GitHub Desktop.
Save jjarmoc/2847053 to your computer and use it in GitHub Desktop.
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