public virtual bool EncryptData(byte[] data)
        {
            try
            {
                int ret_code;
                var dris = new DRIS();                         // initialise the DRIS with random values & set the header

                dris.size               = Marshal.SizeOf(dris);
                dris.function           = ENCRYPT_USER_DATA;    // standard protection check & encrypt data
                dris.flags              = USE_FUNCTION_ARGUMENT;
                dris.rw_length          = data.GetLength(0);
                dris.data_crypt_key_num = 1;

                ret_code = DinkeyPro.DDProtCheck(dris, data);

                if (ret_code != 0)
                {
                    throw new Exception(dris.DisplayError(ret_code, dris.ext_err));
                }

                return(true);
            }
            catch
            {
                throw;
            }
        }
Ejemplo n.º 2
0
        public bool AuthenticateModule(string Path)
        {
#if DEBUG
            _logger.Entry("Module debug authentication enabled");
            return(true);
#endif
#if !DEBUG
            string moduleName = GetModuleNameFromPath(Path);
            if (moduleName.Contains("ObjectModel"))
            {
                return(true);
            }

            int LocalErrorCode = -1;

            int  ret_code;
            DRIS dris = new DRIS(); // initialise the DRIS with random values & set the header

            dris.size             = Marshal.SizeOf(dris);
            dris.function         = DRIS.PROTECTION_CHECK; // standard protection check
            dris.flags            = 128;                   // no extra flags, but you may want to specify some if you want to start a network user or decrement execs,...
            dris.alt_licence_name = moduleName;

            LocalErrorCode = DinkeyPro.DDProtCheck(dris, null);

            if (LocalErrorCode != 0)
            {
                //DisplayError(ret_code, dris.ext_err);
                _logger.Entry("Module " + moduleName + "authentication failed - " + LocalErrorCode, Severity.Warning);
                return(false);
            }

            return(true);
#endif
        }
Ejemplo n.º 3
0
        public override byte[] DecryptData(byte[] data)
        {
            try
            {
                int ret_code, alg_ans;
                var dris = new DRIS();

                dris.size               = Marshal.SizeOf(dris);
                dris.function           = DECRYPT_USER_DATA;     // standard protection check & read data
                dris.flags              = USE_FUNCTION_ARGUMENT; // you have to do it like this in C#
                dris.rw_length          = data.GetLength(0);
                dris.data_crypt_key_num = 1;

                CryptDRIS(dris);                               // encrypt DRIS (!!!!you should separate from DDProtCheck for greater security)

                ret_code = DinkeyPro.DDProtCheck(dris, data);

                CryptDRIS(dris);                               // decrypt DRIS (!!!!you should separate from DDProtCheck for greater security)

                if (ret_code != 0)
                {
                    throw new Exception(dris.DisplayError(ret_code, dris.ext_err));
                }

                alg_ans = AlgorithmComputation();
                // decrypt data that was passed to us by the API.
                CryptApiData(dris, data, data.GetLength(0), alg_ans);

                return(data);
            }
            catch
            {
                throw;
            }
        }
        public override void CryptApiData(DRIS dris, byte[] data, int length, int alg_answer)
        {
            int  i, j, k;
            var  bigseed = new byte[256];
            var  S = new byte[256];
            byte temp, t;

            for (i = 0; i < 256; i += 8)
            {
                dris.Set4Bytes(bigseed, i, dris.seed1);
                dris.Set4Bytes(bigseed, i + 4, dris.seed2);
            }
            for (i = 0; i < 256; i++)
            {
                S[i] = (byte)i;
            }
            for (i = 0, j = 0; i < 256; i++)
            {
                j    = (j + S[i] + bigseed[i] + 7) % 256;
                temp = S[i];
                S[i] = S[j];
                S[j] = temp;
            }
            for (i = 0, j = 0, k = 0; k < length; k++)
            {
                i        = (i + 1) % 256;
                j        = (j + S[i] + 71) % 256;
                temp     = S[i];
                S[i]     = S[j];
                S[j]     = temp;
                t        = (byte)(S[i] + S[j] + 16);
                data[k] ^= S[t];
            }
        }
Ejemplo n.º 5
0
        public override byte[] ReadData(int dataLength, int dataOffset)
        {
            int ret_code, alg_ans;
            var dris     = new DRIS();                     // initialise the DRIS with random values & set the header
            var dataRead = new byte[dataLength];

            dris.size      = Marshal.SizeOf(dris);
            dris.function  = READ_DATA_AREA;                // standard protection check & read data
            dris.flags     = USE_FUNCTION_ARGUMENT;         // you have to do it like this in C#
            dris.rw_offset = dataOffset;
            dris.rw_length = dataRead.GetLength(0);

            // calculate r/w algorithm answer - NB need to replace MyRWAlgorithm code with code for r/w algorithm
            alg_ans = AlgorithmComputation();

            CryptDRIS(dris);                                // encrypt DRIS (!!!!you should separate from DDProtCheck for greater security)

            ret_code = DinkeyPro.DDProtCheck(dris, dataRead);

            CryptDRIS(dris);                                // decrypt DRIS (!!!!you should separate from DDProtCheck for greater security)

            if (ret_code != 0)
            {
                throw new Exception(dris.DisplayError(ret_code, dris.ext_err));
            }

            // decrypt data that was read.
            CryptApiData(dris, dataRead, dataRead.GetLength(0), alg_ans);

            return(dataRead);
        }
        protected const int MATCH_DONGLE_NUMBER     = 512; // restrict the search to match the dongle number specified in the DRIS

        public virtual bool WriteData(byte[] dataToWrite, int dataOffset)
        {
            try
            {
                int ret_code;
                var dris = new DRIS();                         // initialise the DRIS with random values & set the header

                dris.size      = Marshal.SizeOf(dris);
                dris.function  = WRITE_DATA_AREA;               // standard protection check & write data to dongle
                dris.flags     = USE_FUNCTION_ARGUMENT;         // you have to do it like this in C#
                dris.rw_offset = dataOffset;
                dris.rw_length = dataToWrite.GetLength(0);

                ret_code = DinkeyPro.DDProtCheck(dris, dataToWrite);

                if (ret_code != 0)
                {
                    throw new Exception(dris.DisplayError(ret_code, dris.ext_err));
                }

                return(true);
            }
            catch
            {
                throw;
            }
        }
        public virtual byte[] ReadData(int dataLength, int dataOffset)
        {
            try
            {
                int ret_code;
                var dris       = new DRIS();                   // initialise the DRIS with random values & set the header
                var dataToRead = new byte[dataLength];

                dris.size      = Marshal.SizeOf(dris);
                dris.function  = READ_DATA_AREA;                // standard protection check & read data
                dris.flags     = USE_FUNCTION_ARGUMENT;         // you have to do it like this in C#
                dris.rw_offset = dataOffset;
                dris.rw_length = dataToRead.GetLength(0);

                ret_code = DinkeyPro.DDProtCheck(dris, dataToRead);

                if (ret_code != 0)
                {
                    throw new Exception(dris.DisplayError(ret_code, dris.ext_err));
                }

                return(dataToRead);
            }
            catch
            {
                throw;
            }
        }
        public virtual byte[] DecryptData(byte[] data)
        {
            try
            {
                int ret_code;
                var dris = new DRIS();

                dris.size               = Marshal.SizeOf(dris);
                dris.function           = DECRYPT_USER_DATA;     // standard protection check & read data
                dris.flags              = USE_FUNCTION_ARGUMENT; // you have to do it like this in C#
                dris.rw_length          = data.GetLength(0);
                dris.data_crypt_key_num = 1;

                ret_code = DinkeyPro.DDProtCheck(dris, data);

                if (ret_code != 0)
                {
                    throw new Exception(dris.DisplayError(ret_code, dris.ext_err));
                }

                return(data);
            }
            catch
            {
                throw;
            }
        }
        public virtual bool CheckProtection()
        {
            try
            {
                int ret_code;
                var dris = new DRIS();                         // initialise the DRIS with random values & set the header

                dris.size     = Marshal.SizeOf(dris);
                dris.function = PROTECTION_CHECK;               // standard protection check
                dris.flags    = 0;                              // no extra flags, but you may want to specify some if you want to start a network user or decrement execs,...

                ret_code = DinkeyPro.DDProtCheck(dris, null);

                if (ret_code != 0)
                {
                    throw new Exception(dris.DisplayError(ret_code, dris.ext_err));
                }

                return(true);
            }
            catch
            {
                throw;
            }
        }
Ejemplo n.º 10
0
    // calls the DDProtCheck function in the appropriate DLL
    public static int DDProtCheck(DRIS dris, byte[] data)
    {
        int ret_code = -1;

        if (IntPtr.Size == 4)
        {
            try
            {
                ret_code = DDProtCheck32.DDProtCheck(dris, data);
            }
            catch (DllNotFoundException)
            {
                MessageBox.Show("Error! Cannot find dpwin32.dll. This should be in the same folder as DllTest.", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
        else
        {
            try
            {
                ret_code = DDProtCheck64.DDProtCheck(dris, data);
            }
            catch (DllNotFoundException)
            {
                MessageBox.Show("Error! Cannot find dpwin64.dll. This should be in the same folder as DllTest.",
                                "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
        return(ret_code);
    }
Ejemplo n.º 11
0
        public bool VerifyLicense(string LicenseName)
        {
#if DEBUG
            _logger.Entry("Module debug authentication enabled");
            return(true);
#endif
#if !DEBUG
            int LocalErrorCode = -1;

            int  ret_code;
            DRIS dris = new DRIS(); // initialise the DRIS with random values & set the header

            dris.size             = Marshal.SizeOf(dris);
            dris.function         = DRIS.PROTECTION_CHECK; // standard protection check
            dris.flags            = 128;                   // no extra flags, but you may want to specify some if you want to start a network user or decrement execs,...
            dris.alt_licence_name = LicenseName;

            LocalErrorCode = DinkeyPro.DDProtCheck(dris, null);

            if (LocalErrorCode != 0)
            {
                //DisplayError(ret_code, dris.ext_err);
                _logger.Entry("Module " + LicenseName + " license not found - " + LocalErrorCode, Severity.Debug);
                return(false);
            }

            return(true);
#endif
        }
Ejemplo n.º 12
0
        public bool Authenticated()
        {
#if DEBUG
            _logger.Entry("Debug authentication enabled");
            return(true);
#endif
#if !DEBUG
            ErrorCode = -1;

            int  ret_code;
            DRIS dris = new DRIS(); // initialise the DRIS with random values & set the header

            dris.size     = Marshal.SizeOf(dris);
            dris.function = DRIS.PROTECTION_CHECK; // standard protection check
            dris.flags    = 4;                     // no extra flags, but you may want to specify some if you want to start a network user or decrement execs,...

            ErrorCode = DinkeyPro.DDProtCheck(dris, null);

            if (ErrorCode != 0)
            {
                //DisplayError(ret_code, dris.ext_err);
                _logger.Entry("Authentication failed - " + ErrorCode, Severity.Warning);
                return(false);
            }

            return(true);
#endif
        }
Ejemplo n.º 13
0
    // calls the DDProtCheck function in the appropriate DLL
    public static int DDProtCheck(DRIS dris, byte[] data)
    {
        int ret_code = -1;

        if (IntPtr.Size == 4)
        {
            try
            {
                ret_code = DDProtCheck32.DDProtCheck(dris, data);
            }
            catch (DllNotFoundException)
            {
                Console.WriteLine("Error! Cannot find dpwin32.dll. This should be in the same folder as DllTest.");
            }
        }
        else
        {
            try
            {
                ret_code = DDProtCheck64.DDProtCheck(dris, data);
            }
            catch (DllNotFoundException)
            {
                Console.WriteLine("Error! Cannot find dpwin64.dll. This should be in the same folder as DllTest.");
            }
        }
        return(ret_code);
    }
Ejemplo n.º 14
0
    // converts a byte array to the DRIS structure (so we can do encryption)
    public void ByteArrayToDris(byte[] data, DRIS dris)
    {
        GCHandle MyGC = GCHandle.Alloc(data, GCHandleType.Pinned);

        Marshal.PtrToStructure(MyGC.AddrOfPinnedObject(), dris);
        MyGC.Free();
        return;
    }
Ejemplo n.º 15
0
    // converts to DRIS structure to a byte array (so we can do encryption)
    public void DrisToByteArray(DRIS dris, byte[] data)
    {
        GCHandle MyGC = GCHandle.Alloc(data, GCHandleType.Pinned);

        Marshal.StructureToPtr(dris, MyGC.AddrOfPinnedObject(), false);
        MyGC.Free();
        return;
    }
Ejemplo n.º 16
0
        public override bool CheckProtection(int flags = 0, int alg_num = 1)
        {
            try
            {
                int ret_code;
                var dris = new DRIS();                     // initialise the DRIS with random values & set the header

                dris.size       = Marshal.SizeOf(dris);
                dris.function   = EXECUTE_ALGORITHM;        // standard protection check & execute algorithm
                dris.flags      = flags;                    // no extra flags, but you may want to specify some if you want to start a network user or decrement execs,...
                dris.alg_number = alg_num;                  // execute algorithm 1 (you do not need to specify this field if you are only using Lite models).
                                                            // you should remove these entries if you are using Dinkey Lite so that algorithm arguments are random
                dris.var_a = dris_alg_val_a;
                dris.var_b = dris_alg_val_b;
                dris.var_c = dris_alg_val_c;
                dris.var_d = dris_alg_val_d;
                dris.var_e = dris_alg_val_e;
                dris.var_f = dris_alg_val_f;
                dris.var_g = dris_alg_val_g;
                dris.var_h = dris_alg_val_h;

                CryptDRIS(dris);                            // encrypt DRIS (!!!!you should separate from DDProtCheck for greater security)

                ret_code = DinkeyPro.DDProtCheck(dris, null);

                CryptDRIS(dris);                            // decrypt DRIS (!!!!you should separate from DDProtCheck for greater security)

                if (ret_code != 0)
                {
                    throw new Exception(dris.DisplayError(ret_code, dris.ext_err));
                }

                // later in your code you can check other values in the DRIS...
                if (dris.sdsn != MY_SDSN)
                {
                    throw new Exception("Incorrect SDSN! Please modify your source code so that MY_SDSN is set to be your SDSN.");
                }

                // later on in your program you can check the return code again
                if (dris.ret_code != 0)
                {
                    throw new Exception("Dinkey Dongle protection error");
                }

                // if you want you can check the algorithm answer - however if algorithm is from your code then best to just use the answer in your code
                // !!!! Make sure you have replaced the MyAlgorithm routine with the algorithm you have specified in DinkeyAdd
                if (AlgorithmComputation() != dris.alg_answer)
                {
                    throw new Exception("Dinkey protection error!\nYou have not patched your algorithm in the MyAlgorithm routine");
                }
                return(true);
            }
            catch
            {
                throw;
            }
        }
        public override void CryptDRIS(DRIS dris)
        {
            int  i, j, k;
            var  bigseed = new byte[256];
            var  S = new byte[256];
            byte temp, t;
            var  dris_bytes = new byte[Marshal.SizeOf(dris)];

            dris.DrisToByteArray(dris, dris_bytes); // convert DRIS to byte array so we can encrypt it

            for (i = 0; i < 256; i += 8)
            {
                dris.Set4Bytes(bigseed, i, dris.seed1);
                dris.Set4Bytes(bigseed, i + 4, dris.seed2);
            }
            for (i = 0; i < 256; i++)
            {
                S[i] = (byte)i;
            }
            for (i = 0, j = 0; i < 256; i++)
            {
                j    = (j + S[i] + bigseed[i] + 11) % 256;
                temp = S[i];
                S[i] = S[j];
                S[j] = temp;
            }
            for (i = 0, j = 0, k = 16; k < Marshal.SizeOf(dris); k++)
            {
                i              = (i + 1) % 256;
                j              = (j + S[i] + 37) % 256;
                temp           = S[i];
                S[i]           = S[j];
                S[j]           = temp;
                t              = (byte)(S[i] + S[j] + 17);
                dris_bytes[k] ^= S[t];
            }
            dris.ByteArrayToDris(dris_bytes, dris); // convert it back again
        }
Ejemplo n.º 18
0
        public override bool WriteData(byte[] dataToWrite, int dataOffset)
        {
            try
            {
                int ret_code, alg_ans;
                var dris = new DRIS();                         // initialise the DRIS with random values & set the header

                dris.size      = Marshal.SizeOf(dris);
                dris.function  = WRITE_DATA_AREA;               // standard protection check & write data to dongle
                dris.flags     = USE_FUNCTION_ARGUMENT;         // you have to do it like this in C#
                dris.rw_offset = dataOffset;
                dris.rw_length = dataToWrite.GetLength(0);

                alg_ans = AlgorithmComputation();

                // encrypt data we want to write.
                CryptApiData(dris, dataToWrite, dataToWrite.GetLength(0), alg_ans);

                CryptDRIS(dris);                                // encrypt DRIS (!!!!you should separate from DDProtCheck for greater security)

                ret_code = DinkeyPro.DDProtCheck(dris, dataToWrite);

                CryptDRIS(dris);                                // decrypt DRIS (!!!!you should separate from DDProtCheck for greater security)

                if (ret_code != 0)
                {
                    throw new Exception(dris.DisplayError(ret_code, dris.ext_err));
                }

                return(true);
            }
            catch
            {
                throw;
            }
        }
Ejemplo n.º 19
0
        public override bool EncryptData(byte[] data)
        {
            try
            {
                int ret_code, alg_ans;
                var dris = new DRIS();                         // initialise the DRIS with random values & set the header

                dris.size               = Marshal.SizeOf(dris);
                dris.function           = ENCRYPT_USER_DATA;    // standard protection check & encrypt data
                dris.flags              = USE_FUNCTION_ARGUMENT;
                dris.rw_length          = data.GetLength(0);
                dris.data_crypt_key_num = 1;

                alg_ans = AlgorithmComputation();

                // encrypt data we pass to our API.
                CryptApiData(dris, data, data.GetLength(0), alg_ans);

                CryptDRIS(dris);                                // encrypt DRIS (!!!!you should separate from DDProtCheck for greater security)

                ret_code = DinkeyPro.DDProtCheck(dris, data);

                CryptDRIS(dris);                                // decrypt DRIS (!!!!you should separate from DDProtCheck for greater security)

                if (ret_code != 0)
                {
                    throw new Exception(dris.DisplayError(ret_code, dris.ext_err));
                }

                return(true);
            }
            catch
            {
                throw;
            }
        }
Ejemplo n.º 20
0
 public abstract void CryptDRIS(DRIS dris);
Ejemplo n.º 21
0
 public abstract void CryptApiData(DRIS dris, byte[] data, int length, int alg_answer);
Ejemplo n.º 22
0
 public static extern int DDProtCheck([In, Out, MarshalAs(UnmanagedType.LPStruct)] DRIS dris, byte[] data);