Artem's blog

Mainly .NET (C#, ASP.NET) and my projects

Archives for C#

Two new videos about Serial Key Manager

This week I was able to record two new videos that describe the process of external validation of a license key using Serial Key Manager. Now, my final aim is to record a third, last video about the way the local time can be synced with a server to prevent user from gaining more days than allowed by a license.

Below are the videos:

An article about Licensing systems

This is an article about three different licensing systems, using C#.NET environment.

Title: Three different algorithms for constructing licensing systems, their advantages and disadvantages using C# .NET environment.

Abstract: A key validation algorithm is one of the important parts in the protection of a computer application. Even if an already existing API is to be used, it is important to understand its weaknesses in order to compare it with alternative ones. Therefore, in this article, three different categories will be described with clear definitions that will make it possible to distinguish between them and allow an analysis of currently existing APIs. Every category is accompanied with examples and in some cases suggestions for further development. The categories described in this article are Checksum based key validation, Pattern based key validation, and Information based key validation. It is going to be found that the choice of a key validation system depends on the information that is to be stored in the key. It is also concluded that at this point it would be better to use online key validation instead.

SKGL 2.0.5.3

Released a new patch for SKGL (now 2.0.5.3) which fixes the MachineCode error:

Release notes:

fixed bug https://skgl.codeplex.com/workitem/2166. Now, SKGL 2.0.5.3 although it will be find in the 2.0.5.2 solution. thanks to dprotopopov for bug report.

Summary: there was an issue with machine code calculation because of permission settings.

EDIT: fixed it for Virtual Box environment too.

Fix can be downloaded from: https://skgl.codeplex.com/releases/view/121143

NuGet users should have also received the update in the package manager.

Edit to the last section in Number Games

In my booklet, Algorithms via C#, a way to make the computer guess the numbers more efficiently is presented (see pp. 12-13). Let’s quickly remind ourselves about the problem: we want to make the computer guess a number, between 0 to 100, with minimal amount of questions. It is concluded that we should start with 50, then 25, then 12, etc., that is, divide 100 by 2^k  and depending on if the number is less than what computer guessed, we should either add or subtract this factor.

Everything is correct, but I would like to emphasize that this value can be computed without division.

⌊100/2^1⌋ = 50 = (110010)2
⌊100/2^2⌋ = 25 = (11001)2
⌊100/2^3⌋= 12 = (1100)2
⌊100/2^4⌋= 6 = (110)2
⌊100/2^5⌋= 3 = (11)2
⌊100/2^6⌋= 1 = (1)2

As you can see, in base 2, you simply remove the least significant bit each time. This might save time if you work in radix 2.

Vigenère cipher, programming contest

Yesterday I was solving an interesting question that has been used on the Swedish Computer Science contest – a programming contest, in 2010, which probably is simple, but interesting because it emphasises sequences, et cetera.

The question introduced the Vigenére cipher in a classical way, with two disks containing letters of the English alphabet. In the beginning, we know several things about the encrypted text, which are to help us when we are going to crack the message.
bokstavssnurra

  • The disks might have different number configurations, i.e. there is still a shift, which is unknown.
  • In order to “make it harder” for the enemy, each time we have obtained a letter, we continue by using a new shift, and this shift is increased by a constant value.
  • Fortunately, we get to know that each message contains the word “HEJ” – a Swedish word for “hello”, which might be used as  “bye”, I think.

Also, we get some examples as well, for instance:

Example 1:
Encrypted text: LRIJOUZRIYAQIRAG
Message: HEJVITARENFIKANU – means let’s take a “fika”

Example 2:
Encrypted text: GCGDJJI
Message: HEJHOPP – hello in an optimistic manner

There is also an example of this sequence,in Example 1, which is tells us that the difference is 9, 12, 15, 18… (common difference 3).

I’m solving this using a recurrence, and I’m not simplifying this arithmetic progression in any way, I just add d all the time.

The code below has passed all their tests, which are located at the end of this post.

        static void Main(string[] args)
        {
            /* 
             * Copyright (C) 2013 Artem Los,
             * All rights reserved.
             * 
             * This code-snippet can be found at: 
             *      http://clizware.net/
             */

            string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

            Console.Write("Enter the message: ");

            string message = Console.ReadLine();

            int H = alphabet.IndexOf("H");
            int E = alphabet.IndexOf("E");
            int J = alphabet.IndexOf("J");

            int dA = mod(alphabet.IndexOf(message[0]) - H, alphabet.Length);
            int dB = mod(alphabet.IndexOf(message[1]) - E, alphabet.Length);
            int dC = mod(alphabet.IndexOf(message[2]) - J, alphabet.Length);

            int init1 = dB - dA;
            int init2 = dC - dB;

            int dConst = init2 - init1;
            int dPrev = dC;

            Console.Write("Output message: HEJ");
            for (int i = 3; i < message.Length; i++)
            {
                init2 += dConst;
                dPrev = mod(dPrev + init2, alphabet.Length);

                int X = alphabet.IndexOf(message[i]);
                Console.Write(alphabet[mod(X - dPrev, alphabet.Length)]);
            }

            Console.ReadLine();
        }

        static int mod(int n, int b)
        {
            return n - b * (int)Math.Floor((double)n / b);
        }
    }

Example 1
RIPIWDUXRS
HEJSOVERNI

Example 2
IGNUFAKPXYVSF
HEJNUKOMMERDE

Example 3
PAJESQBIPOOPKAP
HEJKOMHITGENAST

Reference:
http://www.progolymp.se/Oldpage/arkiv/kval10.pdf
http://www.progolymp.se/Oldpage/arkiv/kvalsvar10.pdf

A new feature is coming to SKGL Project

In order to decrease the distribution of serial keys over ‘bad’ hacking sites, I would like to present a feature that probably will be released in SKGL 2.1.1.0 – Machine code locking. This means that each serial key will have support for machine code storage, so that the serial key is bounded to a single computer. As you know, the project is open source, and you can join it whenever you want to (http://skgl.codeplex.com/). If you think I should change something, please write to me or comment this post. Good Luck!

       static string getMachineCode()
        {
            /* 
             * Copyright (C) 2012 Artem Los, All rights reserved.
             * 
             * This code will generate a 5 digits long key, finger print, of the system
             * where this method is being executed. However, that might be changed in the
             * hash function "GetStableHash", by changing the amount of zeroes in
             * MUST_BE_LESS_OR_EQUAL_TO to the one you want to have. Ex 1000 will return 
             * 3 digits long hash.
             * 
             * Please note, that you might also adjust the order of these, but remember to
             * keep them there because as it is stated at 
             * (http://www.codeproject.com/Articles/17973/How-To-Get-Hardware-Information-CPU-ID-MainBoard-I)
             * the processorID might be the same at some machines, which will generate same
             * hashes for several machines.
             * 
             * The function will probably be implemented into SKGL Project at http://skgl.codeplex.com/
             * and Software Protector at http://softwareprotector.codeplex.com/, so I 
             * release this code under the same terms and conditions as stated here:
             * http://skgl.codeplex.com/license
             * 
             * Any questions, please contact me at
             *  * artem@artemlos.net
             */
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_Processor");
            string collectedInfo = ""; // here we will put the informa

            foreach (ManagementObject share in searcher.Get())
            {
                // first of all, the processorid
                collectedInfo += share.GetPropertyValue("ProcessorId").ToString ();
            }

            searcher.Query = new ObjectQuery("select * from Win32_BIOS");
            foreach (ManagementObject share in searcher.Get())
            {
                //then, the serial number of BIOS
                collectedInfo +=share.GetPropertyValue("SerialNumber").ToString ();
            }

            searcher.Query = new ObjectQuery("select * from Win32_BaseBoard");
            foreach (ManagementObject share in searcher.Get())
            {
                //finally, the serial number of motherboard
                collectedInfo+= share.GetPropertyValue("SerialNumber").ToString();
            }
            return GetStableHash(collectedInfo ).ToString ();
        }

        static public int GetStableHash(string s)
        {
            /*
             * modification of code from:
             * http://stackoverflow.com/questions/548158/fixed-length-numeric-hash-code-from-variable-length-string-in-c-sharp
             *
             * modified by Artem Los
             *
             */
            const int MUST_BE_LESS_OR_EQUAL_TO = 100000;
            uint hash = 0;

            foreach (byte b in System.Text.Encoding.Unicode.GetBytes(s))
            {
                hash += b;
                hash += (hash << 10);
                hash ^= (hash >> 6);
            }

            hash += (hash << 3);
            hash ^= (hash >> 11);
            hash += (hash << 15);

            int result = (int)(hash % MUST_BE_LESS_OR_EQUAL_TO);
            int check = MUST_BE_LESS_OR_EQUAL_TO / result;

            if (check > 1)
            {
                result *= check;
            }

            return result;
        }

Hash code generator

Dear Readers,

The last time I have spent on the development of SKGL API, which is a library for those of you who want to secure, protect your hard-coded .NET application. I meanwhile, I have also spent sometime on a code, (basically half an hour), that produces hash code. It is not the best way of doing it, however, so it would be a pleasure to see another version of this code. Much appreciated!

        static string twentyfiveByteHash (string s)
        {
            int amountOfBlocks = s.Length / 5;
            string[] preHash = new string[amountOfBlocks];

            if (s.Length <= 5)
            {
                //if the input string is shorter than 5, no need of blocks!
                preHash[0] = GetStableHash(s).ToString ();
            }
            else if (s.Length > 5)
            {
                //if the input is more than 5, there is a need of dividing it into blocks.
                for (int i = 0; i < amountOfBlocks-1; i++)
                {
                    preHash[i] = GetStableHash (s.Substring(i*5,5)).ToString ();
                }
                preHash[preHash.Length-1] = GetStableHash (s.Substring((preHash.Length-1) * 5, s.Length - (preHash.Length - 1) * 5)).ToString ();

            }
            return String.Join ("",preHash);
        }

        static public int GetStableHash(string s)
        {
            /*
             * modification of code from:
             * http://stackoverflow.com/questions/548158/fixed-length-numeric-hash-code-from-variable-length-string-in-c-sharp
             *
             * modified by Artem Los
             *
             */
            const int MUST_BE_LESS_OR_EQUAL_TO = 100000000;
            uint hash = 0;

            foreach (byte b in System.Text.Encoding.Unicode.GetBytes(s))
            {
                hash += b;
                hash += (hash << 10);
                hash ^= (hash >> 6);
            }

            hash += (hash << 3);
            hash ^= (hash >> 11);
            hash += (hash << 15);

            int result = (int)(hash  % MUST_BE_LESS_OR_EQUAL_TO);
            int check = MUST_BE_LESS_OR_EQUAL_TO / result;

            if (check > 1)
            {
                result *= check;
            }

            return result;
        }

MsgBox function for C#

        void MsgBox(string _text,string _title = "Message Box")
        {
            //my msgbox. from Visual Basic .NET!!!
            MessageBox.Show(_text,_title );
        }
Page 2 of 2:« 1 2