Friday night coding

This is how I spent my Friday night – I was sipping some delicious liquor and thinking about how crappy XOR encryption is unless the key length is high. Then I thought to myself “What if there was a dynamic xor key for each shift?”. Then I thought “I really should be out meeting the ladies instead of wasting away my Friday nights doing code and encryption”.

This is the result. Maybe it would be stronger if the key was random? Then again, if it was random, how would you decode it? Brute force your own encryption? Bah.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;


namespace JoeCrypt
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("Usage is " + System.AppDomain.CurrentDomain.FriendlyName + " [file-to-crypt] [seed]");
                return;
            }

            int modval = Convert.ToInt32(args[1]);
            if (modval < 1)
            {
                Console.WriteLine("Seed must be larger than 1");
                return;
            }
            
            if(!File.Exists(args[0]))
            {
                Console.WriteLine("Could not find file " + args[1]);
                return;
            }
            
            byte[] buffer1 = File.ReadAllBytes(args[0]);
            byte[] resultBuffer = new byte[buffer1.Length];

            for (int i = 0; i < buffer1.Length; i++)
            {
                resultBuffer[i] = (byte)(buffer1[i] ^ ((buffer1.Length - i) % modval));
            }

            File.WriteAllBytes(args[1] + "_encrypted", resultBuffer);
            Console.WriteLine("Encrypted file saved to " + Directory.GetCurrentDirectory() + "\\" + args[0] + "_encrypted");
            return;
        }
    }
}

Running the app against an already encrypted file will decrypt it assuming you put in the right seed (key?). That said, I don’t need to specify a separate method / class for decryption.

Some optimization comes to mind on speeding this up ie; chunking and multiple threads. Maybe some other time.

1407086268646

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.