Created
May 31, 2012 23:04
-
-
Save jjarmoc/2847053 to your computer and use it in GitHub Desktop.
XOR a file with a single byte key, save as file.xor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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