C# LockToKey without Microsoft.VisualBasic

Locked
Tyox80
Newbie
Posts: 1
Joined: 28 Mar 2013, 07:36

C# LockToKey without Microsoft.VisualBasic

Post by Tyox80 » 28 Mar 2013, 07:45

Hi,

i just look at the LockToKey code for C# on the svn and in fact Microsoft.VisualBasic is not required.
You just have to work on bytes like that:

Code: Select all

public static string DecodeLock(string aLock)
{
    byte[] key      =  new byte[aLock.Length];
    byte[] bLock    = Encoding.ASCII.GetBytes(aLock); // On System.Text

    for (int i = 1; i < aLock.Length; i++)
        key[i] = (byte)(bLock[i] ^ bLock[i - 1]);
    key[0] = (byte)(bLock[0] ^ bLock[bLock.Length - 1] ^ bLock[bLock.Length - 2] ^ 5);
    for (int i = 0; i < aLock.Length; i++)
        key[i] = (byte)(((key[i] << 4) & 240) | ((key[i] >> 4) & 15));

    return EscapeChars(key);
}
public static string EscapeChars(byte[] key)
{
    StringBuilder builder = new StringBuilder(key.Length);

    for (int index = 0; index < key.Length; index++)
    {
        if (key[index] == 0 || key[index] == 5 || key[index] == 36 || key[index] == 96 || key[index] == 124 || key[index] == 126)
            builder.AppendFormat("/%DCN{0:000}%/", key[index]);
        else
            builder.Append((char)key[index]);
    }

    return builder.ToString();
}

Pretorian
Site Admin
Posts: 214
Joined: 21 Jul 2009, 10:21

Re: C# LockToKey without Microsoft.VisualBasic

Post by Pretorian » 01 Apr 2013, 18:19

I have included this in the SVN.

It'd be good if someone could provide some example code to verify the algorithms...

Gargol
Newbie
Posts: 1
Joined: 06 May 2012, 20:45

Re: C# LockToKey without Microsoft.VisualBasic

Post by Gargol » 07 Nov 2013, 23:43

I wrote this in C# for CoreDC ages ago.
No Microsoft.VisualBasic reference in it.

Code: Select all

static string LockToKey(string lck)
        {
            Encoding encoding = Encoding.GetEncoding(1252);

            lck = lck.Replace("$Lock ", "");
            int iPos = lck.IndexOf(" Pk=", 1);
            if (iPos > 0) lck = lck.Substring(0, iPos);

            char[] arrChar = new char[lck.Length];
            int[] arrRet = new int[lck.Length];
            arrChar[0] = lck[0];
            for (int i = 1; i < lck.Length; i++)
            {
                byte[] test = encoding.GetBytes(new Char[] { lck[i] });
                arrChar[i] = (char)test[0];
                arrRet[i] = arrChar[i] ^ arrChar[i - 1];
            }
            arrRet[0] = arrChar[0] ^ arrChar[lck.Length - 1] ^ arrChar[lck.Length - 2] ^ 5;
            string sKey = "";
            for (int n = 0; n < lck.Length; n++)
            {
                arrRet[n] = ((arrRet[n] * 16 & 240)) | ((arrRet[n] / 16) & 15);
                int j = arrRet[n];
                switch (j)
                {
                    case 0:
                    case 5:
                    case 36:
                    case 96:
                    case 124:
                    case 126:
                        sKey += String.Format("/%DCN{0:000}%/", j);
                        break;
                    default:
                        sKey += encoding.GetChars(new byte[] { Convert.ToByte((char)j) })[0];
                        break;
                }
            }
            return sKey;
        }

Locked