Archive for the ‘/r/dailyprogrammer’ Category

/r/dailyprogrammer – Stream Cipher

Thursday, July 16th, 2015

This time around I thought the stream cipher programming challenge sounded like fun. The program encodes a character string and then decodes it back to the original text using the RC4 method. Below is an example input/output, followed by the code.

CipherOutput

class Program
{
    static void Main(string[] args)
    {
        const int KEY = 31337;
        string message = "High five, bro";

        Console.WriteLine("----Stream Cipher----\n");
        Console.WriteLine("Original Message: " + message + "\n");

        var cipherText = encrypt(message, KEY);
        Console.WriteLine("Cipher Text: " + cipherText + "\n");

        var plainText = decrypt(cipherText, KEY);
        Console.WriteLine("Plain Text: " + plainText);

        Console.ReadLine();
    }

    static string encrypt( string msg, int key )
    {
        int seed = key;
        int m = 255;
        int a = 124;
        int c = 32;
        byte[] bytes = Encoding.Default.GetBytes(msg);
        var value = ((seed * a) + c) % m;

        for (var b = 0; b < bytes.Length; ++b )
        {
            bytes[b] ^= (byte)value;
        }

        return Encoding.Default.GetString(bytes);
    }

    static string decrypt(string cipher, int key)
    {
        return encrypt(cipher, key);
    }
}

/r/dailyprogrammer – Poetry in a Haystack

Sunday, July 5th, 2015

I decided to start participating in some of the programming challenges in the dailyprogrammer subreddit. I'm not saying I'm going to do them every day, but maybe one a week; we'll see! The challenge from July 3 is the first one I've attempted. The challenge is to find 3 lines from a famous poem given a text file with 50000 lines in it. Of those 50000 lines, 49997 of them are gibberish. I will be solving the challenges in C# unless otherwise noted. (more…)

Subscribe to RSS feed FGS5 Badge