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));
        }
    }
}

Install exFAT FUSE Module for Ubuntu with Read/Write Support

http://code.google.com/p/exfat/

sudo apt-get install subversion scons libfuse-dev gcc
cd ~
svn co http://exfat.googlecode.com/svn/trunk/ exfat-read-only
cd exfat-read-only
scons
sudo scons install
cd ..
rm -rf exfat-read-only
sudo mkdir [mountpoint]
sudo mount -t exfat-fuse [device_path] [mountpoint]

Replace [device_path] with the device path to your exfat partition, for example: /dev/sdb1. Replace [mountpoint] with the path to your mount point, for example: /media/disk

Bulk Convert XLSX to XLS

Recently at work we had to need to convert XLSX files to XLS files in bulk. My boss discovered that having to convert many files individually is not a fun process so I wrote this little program to automate the process. Here’s a screen shot:

Click here to download the program. It requires that you have Microsoft Office 2007 or newer installed and the Microsoft .Net Framework 3.5.

Convert NRG to ISO using DD

Earlier this week I was on a mission to convert an NRG image file to an ISO image file. I didn’t want to download a program that someone had already written like nrg2iso and instead preferred to use something like the linux dd command or write my own program that could do. I started digging into the NRG file format and found that it’s actually an IFF file. The documentation on how Nero implemented the IFF format into their NRG files isn’t very clear at all and is incorrect so with the information I was able to find I had to reverse engineer the format myself. Here’s how to convert an NRG image to ISO using the dd command (if you’re on windows you can install Cygwin to gain access to the dd command).

An NRG image is a CD-ROM image followed by Nero’s “footer” about the image, it’s cue sheets, cd text, and so on. The last twelve bytes of an NRG image file contain the NER5 header and an offset to the first CUE sheet of the image. Even though this offset is just a pointer to the first CUE sheet, you can use it determine the actual length of the image. Here’s what to do with dd.

  1. First, take the filesize of the NRG file and subtract 12 from it. Here, my NRG is 964,984,988 bytes. So I’m going to use 964984976 for the skip parameter of the dd command like so:
    dd ibs=1 skip=964984976 if=Image.nrg of=footer.dat
  2. That will give you a 12 byte file named footer.dat. Open footer.dat in your favorite HEX Editor (I use UltraEdit-32).
  3. The first four bytes you should see is NER5. The next eight bytes is a 64-bit number that points to the first CUE sheet. Open Calculator. Set it for Scientific view (Programmer view in Windows 7, maybe Windows Vista too?). Change to Hex mode and type in exactly what you see for those eight bytes. My Hex heditor shows 00 00 00 00 39 84 80 00. So in Calculator I type in 39848000 (you can drop all leading zeros since this is a little-endian number).
  4. Now switch back to Decimal mode. The Hex number you typed in will change to a Decimal number (964984832 in my case), this is the offset at which the first CUE sheet exists in the NRG image. It also happens to be (in most cases) the length of the cd image. Since the cd image is the first thing in the NRG file, all you have to do now is extract the contents of the image into your ISO file.
  5. Divide your decimal offset number by 2048. For me, 964984832 / 2048 = 471184. That’s how many blocks (a block being 2048 bytes for us) we’re going to copy from the NRG file to the ISO file. Now just run the DD command:
    dd ibs=2048 count=471184 if=Image.nrg of=Image.iso

That’s it.. now mount your ISO image to a virtual cd/dvd/bd drive like Virtual CloneDrive (or burn it) and make sure it works before deleting your NRG file.