Esempio n. 1
0
        public static void LogException(Exception e, string extMsg, bool showMsgBox)
        {
            try
            {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.AppendFormat("应用程序出现了异常:\r\n{0}\r\n", e.Message);

                stringBuilder.AppendFormat("\r\n 额外信息: {0}", extMsg);
                if (null != e)
                {
                    if (e.InnerException != null)
                    {
                        stringBuilder.AppendFormat("\r\n {0}", e.InnerException.Message);
                    }
                    stringBuilder.AppendFormat("\r\n {0}", e.StackTrace);
                }

                //记录异常日志文件
                //LogManager.WriteException(stringBuilder.ToString());
                if (showMsgBox)
                {
                    //弹出异常日志窗口
                    System.Console.WriteLine(stringBuilder.ToString());
                }
            }
            catch (Exception ex)
            {
                DebugMod.LogException(ex);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 输入明文和密钥,输出密文
        /// </summary>
        /// <param name="plainText"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Encrypt(string plainText, string passwd, string saltValue)
        {
            if (string.IsNullOrEmpty(plainText))
            {
                return(null);
            }

            byte[] bytesData = null;
            try
            {
                bytesData = new UTF8Encoding().GetBytes(plainText);
            }
            catch (Exception e)
            {
                DebugMod.LogException(e);
                return(null);
            }

            byte[] bytesResult = null;

            try
            {
                bytesResult = AesHelper.AesEncryptBytes(bytesData, passwd, saltValue);
            }
            catch (Exception e)
            {
                DebugMod.LogException(e);
                return(null);
            }

            return(ByteHelper.Bytes2HexString(bytesResult));
        }
Esempio n. 3
0
        /// <summary>
        /// 将日期时间字符串转为整数表示
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static long ConvertToTicks(string str, long defVal)
        {
            if ("*" == str)
            {
                return(defVal);
            }

            str = str.Replace('$', ':');

            try
            {
                DateTime dt;
                if (!DateTime.TryParse(str, out dt))
                {
                    return(0L);
                }

                return(dt.Ticks / 10000);
            }
            catch (Exception e)
            {
                DebugMod.LogException(e);
            }

            return(0L);
        }
Esempio n. 4
0
        public int      PutData(byte[] byteData, int offset, int len)
        {
            if (len <= 0 || len > _maxLen)
            {
                //PRINT("CCircularBuffer::PutData len is <=0\n");
                return(0);
            }

            while (IsOverFlowCondition(len))
            {
                BufferResize();
            }

            m_nValidCount += len;

            try
            {
                if (IsIndexOverFlow(len))
                {
                    int FirstCopyLen  = m_iBufSize - m_iTailPos;
                    int SecondCopyLen = len - FirstCopyLen;
                    Tools.Assert.Check(FirstCopyLen > 0);
                    Array.Copy(byteData, offset, m_Buffer, m_iTailPos, FirstCopyLen);
                    if (SecondCopyLen > 0)
                    {
                        Array.Copy(byteData, offset + FirstCopyLen, m_Buffer, 0, SecondCopyLen);
                        m_iTailPos = SecondCopyLen;
                    }
                    else
                    {
                        m_iTailPos = 0;
                    }
                }
                else
                {
                    Array.Copy(byteData, offset, m_Buffer, m_iTailPos, len);
                    m_iTailPos += len;
                }
            }
            catch (Exception e)
            {
                DebugMod.LogException(e, "CircleBuffer.PutData", false);
            }
            return(len);
        }
Esempio n. 5
0
        /// <summary>
        /// 格式化堆栈信息
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static void LogFormatStack(System.Diagnostics.StackTrace stackTrace, string extMsg)
        {
            try
            {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.AppendFormat("应用程序出现了对象锁定超时错误:\r\n");

                stringBuilder.AppendFormat("\r\n 额外信息: {0}", extMsg);
                stringBuilder.AppendFormat("\r\n {0}", stackTrace.ToString());

                //记录异常日志文件
                //LogManager.WriteException(stringBuilder.ToString());
            }
            catch (Exception e)
            {
                DebugMod.LogException(e);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// 判断如果不是 "*", 则转为指定的值, 否则默认值
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static Int32 ConvertToInt32(string str, Int32 defVal)
        {
            try
            {
                if ("*" != str)
                {
                    return(Convert.ToInt32(str));
                }

                return(defVal);
            }
            catch (Exception e)
            {
                DebugMod.LogException(e);
            }

            return(defVal);
        }
Esempio n. 7
0
        /// <summary>
        /// 将日期时间字符串转为整数表示
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static long ConvertToTicks(string str)
        {
            try
            {
                DateTime dt;
                if (!DateTime.TryParse(str, out dt))
                {
                    return(0L);
                }

                return(dt.Ticks / 10000);
            }
            catch (Exception e)
            {
                DebugMod.LogException(e);
            }

            return(0L);
        }
Esempio n. 8
0
        /// <summary>
        /// 输入密文和密钥,输出明文
        /// </summary>
        /// <param name="plainText"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Decrypt(string encryptText, string passwd, string saltValue)
        {
            if (string.IsNullOrEmpty(encryptText))
            {
                return(null);
            }

            byte[] bytesData = ByteHelper.HexString2Bytes(encryptText);
            if (null == bytesData)
            {
                return(null);
            }

            byte[] bytesResult = null;
            try
            {
                bytesResult = AesHelper.AesDecryptBytes(bytesData, passwd, saltValue);
            }
            catch (Exception e)
            {
                DebugMod.LogException(e);
                return(null);
            }

            string strResult = null;

            try
            {
                strResult = new UTF8Encoding().GetString(bytesResult, 0, bytesResult.Length);
            }
            //解析错误
            catch (Exception e)
            {
                DebugMod.LogException(e);
                return(null);
            }

            return(strResult);
        }
Esempio n. 9
0
        /// <summary>
        /// AES解密字节流
        /// </summary>
        /// <param name="encryptData">密文字节流</param>
        /// <param name="passwd">密码</param>
        /// <param name="saltValue">盐值</param>
        /// <returns>解密后的明文字节流</returns>
        public static byte[] AesDecryptBytes(byte[] encryptData, string passwd, string saltValue)
        {
            // 盐值(与加密时设置的值必须一致)
            // 密码值(与加密时设置的值必须一致)
            byte[] saltBytes = Encoding.UTF8.GetBytes(saltValue);

            AesManaged         aes = new AesManaged();
            Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(passwd, saltBytes);

            aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
            aes.KeySize   = aes.LegalKeySizes[0].MaxSize;
            aes.Key       = rfc.GetBytes(aes.KeySize / 8);
            aes.IV        = rfc.GetBytes(aes.BlockSize / 8);

            // 用当前的 Key 属性和初始化向量 IV 创建对称解密器对象
            ICryptoTransform decryptTransform = aes.CreateDecryptor();

            // 解密后的输出流
            MemoryStream decryptStream = new MemoryStream();

            // 将解密后的目标流(decryptStream)与解密转换(decryptTransform)相连接
            CryptoStream decryptor = new CryptoStream(decryptStream, decryptTransform, CryptoStreamMode.Write);

            try
            {
                // 将一个字节序列写入当前 CryptoStream (完成解密的过程)
                decryptor.Write(encryptData, 0, encryptData.Length);
                decryptor.Close();
            }
            catch (Exception e)
            {
                DebugMod.LogException(e);
                return(null); //解密失败
            }

            // 将解密后所得到的流转换为字符串
            return(decryptStream.ToArray());
        }