コード例 #1
0
        private void bActivation_Click(object sender, EventArgs e)
        {
            byte[] key = ASCIIEncoding.ASCII.GetBytes("DIAR");

            /*RC4 encoder = new RC4(key);
             * byte[] testBytes = ASCIIEncoding.ASCII.GetBytes(m_strCode);
             * byte[] result2 = encoder.Encode(testBytes, testBytes.Length);
             * string encryptedString = encoder.GetByteString(result2);// ASCIIEncoding.ASCII.GetString(result);*/

            RC4 decoder = new RC4(key);

            byte[] result          = decoder.SetByteString(teActivationKey.Text);
            byte[] decryptedBytes  = decoder.Decode(result, result.Length);
            string decryptedString = ASCIIEncoding.ASCII.GetString(decryptedBytes);

            if (decryptedString == m_strCode)
            {
                SQLiteConnection con = new SQLiteConnection(global::Condenser.Properties.Settings.Default.condenserConnectionString);
                try
                {
                    con.Open();
                    SQLiteCommand com = new SQLiteCommand(con);
                    com.CommandType = CommandType.Text;
                    com.CommandText = "INSERT INTO Licenses (Code) VALUES ('" + teActivationKey.Text + "')";
                    com.ExecuteNonQuery();
                    con.Close();
                }
                catch (SQLiteException ex)
                {
                    MyLocalizer.XtraMessageBoxShow("Ошибка при работе с базой данных. Описание: " + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                MyLocalizer.XtraMessageBoxShow("Активация успешно произведена.\nНеобходимо перезапустить программу.", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Close();
            }
            else
            {
                MyLocalizer.XtraMessageBoxShow("Неверный ключ активации.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
コード例 #2
0
        static bool LoadParamData(string strFileName)
        {
            StreamReader sr = StreamReader.Null;

            try
            {
                sr = new StreamReader(strFileName);
                bool    bReadKoef        = false;
                bool    bReadKIn         = false;
                string  strVal           = "";
                string  strSeparator     = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
                string  strCondenserType = "";
                long    KoefID           = 0;
                Decimal?fVal             = null;
                if (sr != StreamReader.Null)
                {
                    SQLiteConnection connection = new SQLiteConnection(global::Condenser.Properties.Settings.Default.condenserConnectionString);
                    connection.Open();

                    byte[] key     = Encoding.Unicode.GetBytes("ELCHROM");
                    RC4    decoder = new RC4(key);

                    while (!sr.EndOfStream)
                    {
                        string strData = sr.ReadLine();

                        // расшифровка
                        byte[] result         = decoder.SetByteString(strData);
                        byte[] decryptedBytes = decoder.Decode(result, result.Length);
                        strData = Encoding.Unicode.GetString(decryptedBytes);


                        if (strData == "Data")
                        {
                            bReadKoef = true;
                            bReadKIn  = false;
                            continue;
                        }
                        else
                        {
                            if (bReadKoef && strData == "KIn")
                            {
                                bReadKIn = true;
                                continue;
                            }

                            string[] vecData = strData.Split('\t');
                            if (bReadKoef)
                            {
                                try
                                {
                                    SQLiteCommand com = new SQLiteCommand(connection);
                                    com.CommandType = CommandType.Text;

                                    if (!bReadKIn)
                                    {
                                        Decimal?fKI1 = null, fkoefA = null, fkoefB = null, fkoefR2 = null;
                                        long    iFunctionType = 0;

                                        fKI1          = null;
                                        fkoefA        = null;
                                        fkoefB        = null;
                                        fkoefR2       = null;
                                        iFunctionType = 0;

                                        //if (vecData.GetLength(0) != 16) throw new Exception("некорректный формат файла.");
                                        for (int j = 0; j < vecData.GetLength(0); j += 2)
                                        {
                                            strVal = vecData[j + 1];

                                            if (vecData[j] == "CondenserType")
                                            {
                                                strCondenserType = vecData[j + 1];
                                            }
                                            if (vecData[j] == "KI1" && strVal != "")
                                            {
                                                strVal = strVal.Replace(".", strSeparator);
                                                strVal = strVal.Replace(",", strSeparator);
                                                fKI1   = Convert.ToDecimal(strVal);
                                            }
                                            if (vecData[j] == "KoefA" && strVal != "")
                                            {
                                                strVal = strVal.Replace(".", strSeparator);
                                                strVal = strVal.Replace(",", strSeparator);
                                                fkoefA = Convert.ToDecimal(strVal);
                                            }
                                            if (vecData[j] == "KoefB" && strVal != "")
                                            {
                                                strVal = strVal.Replace(".", strSeparator);
                                                strVal = strVal.Replace(",", strSeparator);
                                                fkoefB = Convert.ToDecimal(strVal);
                                            }
                                            if (vecData[j] == "KoefR2" && strVal != "")
                                            {
                                                strVal  = strVal.Replace(".", strSeparator);
                                                strVal  = strVal.Replace(",", strSeparator);
                                                fkoefR2 = Convert.ToDecimal(strVal);
                                            }
                                            if (vecData[j] == "FunctionType" && strVal != "")
                                            {
                                                iFunctionType = Convert.ToInt64(strVal);
                                            }
                                        }

                                        if (fKI1 == null)
                                        {
                                            throw new Exception("отсутствует значение в поле КЦ1.");
                                        }

                                        com.CommandText = "SELECT (SELECT CondenserTypeID FROM CondenserTypes WHERE CondenserTypeName = @tname) AS CondenserTypeID, " +
                                                          "(SELECT KoefID FROM CondenserTypeParameters WHERE CondenserTypeID = (SELECT CondenserTypeID FROM CondenserTypes WHERE CondenserTypeName = @tname)) AS KoefID";

                                        AddParam(com, "@tname", DbType.String, strCondenserType);

                                        SQLiteDataReader dr = com.ExecuteReader();

                                        long CondenserTypeID = 0;
                                        KoefID = 0;
                                        while (dr.Read())
                                        {
                                            if (dr["CondenserTypeID"] != DBNull.Value)
                                            {
                                                CondenserTypeID = Convert.ToInt64(dr["CondenserTypeID"]);
                                            }
                                            if (dr["KoefID"] != DBNull.Value)
                                            {
                                                KoefID = Convert.ToInt64(dr["KoefID"]);
                                            }
                                        }
                                        dr.Close();

                                        if (CondenserTypeID == 0)
                                        {
                                            continue;
                                        }

                                        com.Parameters.Clear();

                                        if (KoefID > 0)
                                        {
                                            com.CommandText = "UPDATE CondenserTypeParameters SET KI1 = @ki1, KoefA = @koefA, KoefB = @koefB, KoefR2 = @koefR2, FunctionType = @ftype WHERE CondenserTypeID = @ctypeid";
                                        }
                                        else
                                        {
                                            com.CommandText = "INSERT INTO CondenserTypeParameters (CondenserTypeID, KI1, KoefA, KoefB, KoefR2, FunctionType) VALUES (@ctypeid, @ki1, @koefA, @koefB, @koefR2, @ftype)";
                                        }

                                        AddParam(com, "@ctypeid", DbType.Int64, CondenserTypeID);

                                        AddParam(com, "@ki1", DbType.Decimal, fKI1);
                                        AddParam(com, "@koefA", DbType.Decimal, fkoefA);
                                        AddParam(com, "@koefB", DbType.Decimal, fkoefB);
                                        AddParam(com, "@koefR2", DbType.Decimal, fkoefR2);
                                        AddParam(com, "@ftype", DbType.Int64, iFunctionType);

                                        com.ExecuteNonQuery();

                                        if (KoefID == 0)
                                        {
                                            com.CommandText = "select seq from sqlite_sequence where name = 'CondenserTypeParameters'";
                                            com.Parameters.Clear();
                                            SQLiteDataReader dr_ = com.ExecuteReader();

                                            while (dr_.Read())
                                            {
                                                KoefID = Convert.ToInt64(dr_["seq"]);
                                            }
                                            dr_.Close();
                                        }
                                        else
                                        {
                                            com.CommandText = "DELETE FROM ParameterRecommendations where KoefID = @koefID";
                                            com.Parameters.Clear();
                                            AddParam(com, "@koefID", DbType.Int64, KoefID);
                                            com.ExecuteNonQuery();
                                        }
                                    }
                                    else
                                    {
                                        long    iPosition         = 0;
                                        Decimal?fValue            = null;
                                        string  strRecommendation = "";
                                        string  strConclusion     = "";

                                        com.CommandText = "INSERT INTO ParameterRecommendations (KoefID, Position, Value, Recommendation, Conclusion) VALUES (@koefID, @pos, @val, @recom, @concl)";

                                        for (int j = 0; j < vecData.GetLength(0); j += 2)
                                        {
                                            strVal = vecData[j + 1];

                                            if (vecData[j] == "Pos")
                                            {
                                                iPosition = Convert.ToInt64(vecData[j + 1]);
                                            }
                                            if (vecData[j] == "Val" && strVal != "")
                                            {
                                                strVal = strVal.Replace(".", strSeparator);
                                                strVal = strVal.Replace(",", strSeparator);
                                                fValue = Convert.ToDecimal(strVal);
                                            }
                                            if (vecData[j] == "Recommendation")
                                            {
                                                strRecommendation = strVal;
                                            }
                                            if (vecData[j] == "Conclusion")
                                            {
                                                strConclusion = strVal;
                                            }
                                        }

                                        com.Parameters.Clear();
                                        AddParam(com, "@koefID", DbType.Int64, KoefID);
                                        AddParam(com, "@pos", DbType.Int64, iPosition);
                                        AddParam(com, "@val", DbType.Decimal, fValue);
                                        AddParam(com, "@recom", DbType.String, strRecommendation);
                                        AddParam(com, "@concl", DbType.String, strConclusion);

                                        com.ExecuteNonQuery();
                                    }
                                }
                                catch (SQLiteException ex)
                                {
                                    throw ex;
                                }
                            }
                            else
                            {
                                fVal = null;

                                if (vecData.GetLength(0) != 2)
                                {
                                    throw new Exception("некорректный формат файла.");
                                }
                                if (vecData[0] == "ПЧХ" && vecData[1] != "")
                                {
                                    strVal = vecData[1];
                                    strVal = strVal.Replace(".", strSeparator);
                                    strVal = strVal.Replace(",", strSeparator);

                                    fVal = Convert.ToDecimal(strVal);
                                    if (fVal == null)
                                    {
                                        throw new Exception("отсутствует значение в поле ПЧХ.");
                                    }
                                }

                                try
                                {
                                    SQLiteCommand com = new SQLiteCommand(connection);
                                    com.CommandText = "UPDATE CommonParameters SET ParameterValue = @pval WHERE ParameterName = @pname";
                                    com.CommandType = CommandType.Text;

                                    AddParam(com, "@pval", DbType.String, fVal.ToString());
                                    AddParam(com, "@pname", DbType.String, vecData[0]);

                                    com.ExecuteNonQuery();
                                }
                                catch (SQLiteException ex)
                                {
                                    throw ex;
                                }
                            }
                        }
                    }
                    connection.Close();
                }
            }
            catch (Exception ex)
            {
                if (sr != StreamReader.Null)
                {
                    sr.Close();
                }
                MyLocalizer.XtraMessageBoxShow("Ошибка при загрузке коэффициентов аппроксимации: " + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            if (sr != StreamReader.Null)
            {
                sr.Close();
            }
            return(true);
        }