Identify This Cipher

Using cryptographic analysis on a cipher produced by an existing software program, I was able to write my own compatible algorithm for re-producing the cipher. The only trouble is I have no idea which cipher it is. Perhaps combinations of different ciphers or maybe even a new cipher all together? No, I won’t say which software program uses this cipher, but I wonder if anyone can identify and/or classify it. I believe it to be a symmetric key stream cipher–albeit, very weak and very easy to analyze.

Disclaimer: I invoke my right to free speech to post cryptographic source code. See Bernstein v. United States.

Usage Examples:

Console.WriteLine(Convert.ToBase64String("mypassword".Cipher("378518030611953")));
// or
Console.WriteLine("mypassword".Cipher("378518030611953").ToHexString());
// or
Console.WriteLine("mypassword".Cipher(ASCIIEncoding.ASCII.GetBytes("somekey")).ToHexString());

Corresponding Output Examples:

// y8kJikhp6ItISg==
// or
// cbc9098a4869e88b484a
// or
// c5c4a58605c4c785a527

The Code:

using System;
using System.Text;

namespace MyExtensions
{
    public static class Extensions
    {
        public static byte[] Cipher(this string password, string key)
        {
            byte[] cipher = System.Text.ASCIIEncoding.ASCII.GetBytes(password);
            return cipher.Cipher(key);
        }

        public static byte[] Cipher(this string password, byte[] key)
        {
            byte[] cipher = System.Text.ASCIIEncoding.ASCII.GetBytes(password);
            return cipher.Cipher(key);
        }

        public static byte[] Cipher(this byte[] password, string key)
        {
            try
            {
                byte[] k = key.ParseBytes();
                return password.Cipher(k);
            }
            catch (Exception e)
            {
                throw new Exception("The key can consist only of a string of numbers. No letters or special characters.", e);
            }
        }

        public static byte[] Cipher(this byte[] password, byte[] key)
        {
            byte[] cipher = password;
            for (int i = 0; i < cipher.Length; i++)
            {
                int first = 0x09;
                int last = 0xE9;
                int rounds = cipher[i] ^ key[i % key.Length];

                for (int y = 0; y <= rounds; y++)
                {
                    last += 32;
                    if (last > 255) last = first = first + (y % 16 / 2) - 3;
                    if (last < 0) last = first = 14;
                }
                cipher[i] = (byte)last;
            }
            return cipher;
        }

        public static byte[] ParseBytes(this string s)
        {
            return s.ToCharArray().ParseBytes();
        }

        public static byte[] ParseBytes(this char[] data)
        {
            byte[] p = new byte[data.Length];
            for (int i = 0; i < data.Length; i++)
            {
                byte parsed;
                if (!byte.TryParse(data[i].ToString(), out parsed))
                    throw new Exception("The input can only consist of numbers. No letters or special characters.");
                p[i] = parsed;
            }
            return p;
        }

        public static string ToHexString(this byte[] data)
        {
            string s = string.Empty;
            for (int i = 0; i < data.Length; i++)
            {
                s += data[i].ToHexString();
            }
            return s;
        }

        public static string ToHexString(this byte b)
        {
            return Convert.ToString(b, 16).PadLeft(2, "0");
        }

        public static string PadLeft(this string s, int totalWidth, string padding)
        {
            return s.PadLeft(totalWidth, char.Parse(padding));
        }

        public static string PadRight(this string s, int totalWidth, string padding)
        {
            return s.PadRight(totalWidth, char.Parse(padding));
        }
    }
}

Leave a Reply

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