none
C# to C RRS feed

  • Question

  • Hello,

    Please help me I want to convert below C# class to C code

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Management;
    using System.Security.AccessControl;
    using System.Security.Cryptography;
    using System.Security.Principal;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace ZenSetGroupPolicy
    {
        public class SNGenerator
        {
            public enum
    SNKeyLength
            {
                SN16 = 16, SN20 = 20, SN24 = 24, SN28 = 28, SN32 = 32
            }
            public enum SNKeyNumLength
            {
                SN4 = 4, SN8 = 8, SN12 =
                12
            }
            public static class RandomSNKGenerator
            {
                private static string
                AppendSpecifiedStr(int length, string str, char[] newKey)
                {
                    string
                    newKeyStr = "";
                    int k = 0;
                    for (int i = 0; i < length; i++)
                    {
                        for
                        (k = i; k < 4 + i; k++)
                        {
                            newKeyStr += newKey[k];
                        }
                        if (k ==
                        length)
                        {
                            break;
                        }
                        else
                        {
                            i = (k) - 1;
                            newKeyStr +=
                            str;
                        }
                    }
                    return newKeyStr;
                }
                /// 
    
                /// Generate
                //standard serial key with alphanumaric format
                /// 
                ///
                //the supported length of the serial
                //    key
                /// returns formated serial
                //key
                public static string GetSerialKeyAlphaNumaric(SNKeyLength
                keyLength, string input)
                {
                    string newSerialNumber = "";
                     
                    using (MD5 md5 = MD5.Create())
                    {
                        byte[] hash = md5.ComputeHash(Encoding.Default.GetBytes(input));
                        Guid newguid = new Guid(hash);
                        string randomStr =
                        newguid.ToString("N");
                        string tracStr = randomStr.Substring(0,
                        (int)keyLength);
                        tracStr = tracStr.ToUpper();
                        char[] newKey =
                        tracStr.ToCharArray();
                        switch (keyLength
                        )
                        {
                            case SNKeyLength.SN16:
                                newSerialNumber = AppendSpecifiedStr(16,
                                "-", newKey);
                                break;
                            case SNKeyLength.SN20:
                                newSerialNumber =
                                AppendSpecifiedStr(20, "-", newKey);
                                break;
                            case
                        SNKeyLength.SN24:
                                newSerialNumber = AppendSpecifiedStr(24, "-",
                                newKey);
                                break;
                            case SNKeyLength.SN28:
                                newSerialNumber =
                                AppendSpecifiedStr(28, "-", newKey);
                                break;
                            case
                        SNKeyLength.SN32:
                                newSerialNumber = AppendSpecifiedStr(32, "-",
                                newKey);
                                break;
                        }
                    }
                    return newSerialNumber;
                }
                ///
    
                /// Generate serial key with only numaric
                ///
    
                /// the supported length of
                //the serial key
                /// returns formated serial
                //key
                public static string GetSerialKeyNumaric(SNKeyNumLength
                keyLength)
                {
                    Random rn = new Random();
                    double sd =
                    Math.Round(rn.NextDouble() * Math.Pow(10, (int)keyLength) + 4);
                    return
                    sd.ToString().Substring(0, (int)keyLength);
                }
            }
            public static void GrantAccess()
            {
              
            }
        }
    }
    

    Input : SNGenerator.RandomSNKGenerator.GetSerialKeyAlphaNumaric(SNGenerator.SNKeyLength.SN20, "Hello World");

    Output : B18D-0AB1-E064-4175-05B7

    Saturday, January 12, 2019 5:17 AM

All replies

  • No, you will not be able to translate this into C. Let's qualify that: it's not that you can't translate the C# code itself, which you could do with some effort. The problem is that the bulk of the work in that code is done by the MD5 class, which is part of the Framework Class Library available to C#. This same class is not available in C. I don't doubt that if you look around you might be able to find some source code for computing an MD5 hash in C, but it is no longer a matter of translating the code, but of finding another piece of code.
    • Proposed as answer by Stanly Fan Monday, January 14, 2019 6:05 AM
    Saturday, January 12, 2019 8:30 AM
    Moderator