コード例 #1
0
        /// <summary>
        /// 加密函数
        /// </summary>
        /// <param name="origialText"></param>
        /// <param name="argumentParam"></param>
        /// <returns></returns>
        public string Encrypt(string origialText, params object[] argumentParam)
        {
            StringBuilder ret = new StringBuilder();

            #region 获取解密算法

            string assignKey;
            object obj_argKey = null != argumentParam && argumentParam.Length > 0 ? argumentParam.First() : null;
            if (null == obj_argKey || string.IsNullOrEmpty(obj_argKey.ToString()))
            {
                assignKey = this.AlgorithmKey;
            }
            else
            {
                assignKey = MD5Handler.Generate(obj_argKey.ToString(), true).Top(8);
            }

            ICryptoTransform transform = null;
            using (DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider())
            {
                desProvider.Key = ASCIIEncoding.ASCII.GetBytes(assignKey);
                desProvider.IV  = ASCIIEncoding.ASCII.GetBytes(assignKey);
                transform       = desProvider.CreateEncryptor();
            }

            #endregion

            #region 执行加密

            byte[] inputByteArray;
            byte[] cryptoBytes;
            inputByteArray = Encoding.Default.GetBytes(origialText);

            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cryptoBytes = ms.ToArray();
                }
            }
            foreach (byte b in cryptoBytes)
            {
                ret.AppendFormat("{0:X2}", b);
            }

            #endregion

            return(ret.ToString());
        }
コード例 #2
0
        /// <summary>
        /// 解密函数
        /// </summary>
        /// <param name="ciphertext"></param>
        /// <param name="argumentParam"></param>
        /// <returns></returns>
        public string Decrypt(string ciphertext, params object[] argumentParam)
        {
            string originalText = null;

            if (!this.IsCiphertext(ciphertext))
            {
                throw new Exception("不是标准的DES密文格式,无法进行解密");
            }

            #region 验证密文

            byte[] cipherBytes = null;
            int    len         = ciphertext.Length / 2;
            cipherBytes = new byte[len];
            int x, i;

            for (x = 0; x < len; x++)
            {
                i = Convert.ToInt32(ciphertext.Substring(x * 2, 2), 16);
                cipherBytes[x] = (byte)i;
            }

            #endregion

            #region 获取解密算法

            string assignKey;
            object obj_argKey = null != argumentParam && argumentParam.Length > 0 ? argumentParam.First() : null;
            if (null == obj_argKey || string.IsNullOrEmpty(obj_argKey.ToString()))
            {
                assignKey = this.AlgorithmKey;
            }
            else
            {
                assignKey = MD5Handler.Generate(obj_argKey.ToString(), true).Top(8);
            }

            ICryptoTransform transform = null;
            using (DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider())
            {
                desProvider.Key = ASCIIEncoding.ASCII.GetBytes(assignKey);
                desProvider.IV  = ASCIIEncoding.ASCII.GetBytes(assignKey);
                transform       = desProvider.CreateDecryptor();
            }

            #endregion

            #region 执行解密

            using (MemoryStream ms = new MemoryStream())
            {
                CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write);
                cs.Write(cipherBytes, 0, cipherBytes.Length);
                cs.FlushFinalBlock();
                originalText = Encoding.Default.GetString(ms.ToArray());
            }

            #endregion

            return(originalText);
        }