Skip to content

Instantly share code, notes, and snippets.

@OrigamiTech
Created March 8, 2011 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OrigamiTech/860816 to your computer and use it in GitHub Desktop.
Save OrigamiTech/860816 to your computer and use it in GitHub Desktop.
XOR a file with a given key.
using System;
using System.Text;
using System.IO;
namespace XOR
{
class Program
{
static void Main(string[] args)
{
if (args.Length < 2)
return;
byte[] key = Encoding.UTF8.GetBytes(args[0]);
for (int i = 1; i < args.Length; i++)
{
if (!File.Exists(args[i]))
continue;
using (FileStream fs = new FileStream(args[i], FileMode.Open, FileAccess.ReadWrite))
{
long offset = 0;
byte[] buffer = new byte[1024];
int count = buffer.Length;
while (count==buffer.Length)
{
count = fs.Read(buffer, 0, buffer.Length);
for (int j = 0; j < buffer.Length; j++)
buffer[j] ^= key[j % key.Length];
fs.Seek(offset, SeekOrigin.Begin);
fs.Write(buffer, 0, count);
offset += count;
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment