academic

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));
        }
    }
}
Saturday, March 12th, 2011 academic, algorithms, c#, coding, computers, data No Comments

The Travelling Salesman Problem

Tanner Jepsen
Professor Alexander Vaughan Ames
English Composition I
July 12, 2010

Assignment 4.4: The Travelling Salesman Problem

The Travelling Salesman Problem is a complex problem in Computer Science, however developing a computer algorithm to overcome the problem in the most efficient way possible is not an unmanageable or difficult task. The problem states that given a list of cities and the distances between each pair of cities, finding the shortest possible route that visits each city only once is computationally difficult. For Humans, you might say this is an easy problem. Just look at a map and visually find the shortest route. For computers, it is a far more complex problem. Imagine that the list of cities includes hundreds, thousands or even tens of thousands of destinations spread across a large geographical area. Perhaps it’s not cities at all, but countries or a list of coordinates. In the future it may even be planets, solar systems, galaxies etcetera. The problem can quickly become far too much for a Human to solve. Even for a computer, as the list grows larger it will take the computer longer and require more resources to find an efficient solution. The makers of a popular space game, Eve-Online by CCP, graciously make their entire game database available to the public which is perfect for exploring this problem.

The Eve-Online universe includes 5431 solar systems spread across 1109 constellations and 97 regions. The only way to travel between solar systems is by using jump gates that propel a space ship to faster than light speeds. Attempting to travel between solar systems without a jump gate would take several lifetimes and is simply unfeasible and so using the jump gates is considered to be a constraint. Constraints are a good thing because they limit the number of options the computer can take to travel, which means less for the computer to look at. Finding the quickest route between two solar systems is a fairly simple process using a breadth first search. That is, starting from solar system “A”, check to see if any of the connected solar systems is the final destination. If not, do the same thing with each connected solar system and continue the process until the destination solar system “B” is found. Throughout the process, the computer algorithm needs to keep track of where it has already been so that it doesn’t visit the same place twice. Depending on how many solar systems need to be travelled through to get to the destination, this search algorithm, although extremely simple, could take a very long time to find the destination and requires large amounts of computer memory to keep track of the solar systems that were already visited. There are a few heuristic measures that can be taken to minimize the amount of time it takes and the amount of memory required to find the quickest route.

The Eve-Online database does not directly provide the distance between two solar systems. It does, however, provide the x, y and z coordinates in 3D space of where all the solar systems lie. Using the x, y and z coordinates of two solar systems we can use Pythagorean’s Theorem to determine the distance between them which, in effect, is another constraint. Pythagorean’s Theorem states that the length of a hypotenuse on a right triangle is equal to the square root of the length of the adjacent squared plus the length of the opposite squared, or c = sqrt(a2 + b2). We can further this in 3D space so that the distance between two solar systems would be calculated thusly:

The original algorithm can now be modified to take into account the distance calculated between two solar systems. The algorithm can be programmed so that it does not travel through a solar system that takes it further away from the destination unless all other options have been exhausted. Although this is great, and the computer is finding the route faster, there is still more that can be done.

As previously explained, the Eve-Online database places each solar system into constellations and regions, just as cities are placed into counties and states. As the algorithm travels between solar systems it will cross constellational and regional boundaries, just as in real life, when a person travels between cities he will cross county and state lines to get to his destination. This is another constraint. Before finding the route between solar system “A” and solar system “B”, the algorithm can find the route between the constellations that each solar system lies within. Once it has the route between constellations “A” and “B”, the algorithm can then eliminate adjacent solar systems from the search path by determining if said solar system lies within one of the constellations that have already been found to be along the route. If it doesn’t, the solar system is ignored, potentially removing a very large chunk of search paths from the search algorithm. The same concept can be implemented for regions as well, however depending on the size of the universe, as in the Eve-Online example, it may not be beneficial.

At this point the algorithm is very fast and can be used in a production environment, but there are other constraints that can be added as a convenience to the user. The database includes a security status for each solar system. On a scale from 0.00, indicating no security, to 1.00, indicating complete security, this constraint can be added to the search algorithm so that the user may avoid low security solar systems, thereby avoiding pirates and certain death if the proper arrangements are not made to travel with a skilled security detail.

Exploring the Eve-Online game universe it has been found that the travelling salesman problem is not an unmanageable problem at all on today’s powerful computers. At the hands of a skillful computer programmer, using constraints to her advantage it is possible to minimize the amount of required processor and memory resources to quickly find a solution to the most common routing problems. Many search algorithms are in use today, perhaps some can be made more efficient, reducing operating costs for businesses such as UPS, FedEx, USPS, and DHL to the local courier service, airport shuttle service, and even the pizza delivery man.

Bibliography

CCP. “Eve Online | Data Export.” 1997-2007. Eve Online. 2010 <http://www.eveonline.com/community/toolkit.asp>.

Wikimedia Foundation, Inc. “Pythagorean theorem.” 8 July 2010. Wikipedia. 12 July 2010 <http://en.wikipedia.org/wiki/Pythagorean_theorem>.

—. “Travelling salesman problem.” 10 July 2010. Wikipedia. 12 July 2010 <http://en.wikipedia.org/wiki/Travelling_Salesman_Problem>.

Monday, July 12th, 2010 academic, theory No Comments

Color Wheel

A few years ago I was asked to help someone create a color wheel using Photoshop. With this project I learned a lot about color theory and computationally converting from RGB to HSV and vice versa. I decided to post the color wheel here for my own future reference.

colorcircle

And here’s the PSD file.

Friday, December 18th, 2009 academic, theory No Comments