MD5自定义秘钥加解密

/// 
        /// 加密解密钥匙
        /// 
        private static string SecretCode = ConfigurationManager.AppSettings["SecretCode"].ToString();

        /// 
        /// 加密函数
        /// 
        /// 加密数据
        /// 返回加密后数据
        public static string DesEncrypt(string data)
        {
            string result = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(data) == false)
                {
                    //访问数据加密标准(DES)算法的加密服务提供程序 (CSP) 版本的包装对象
                    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                    des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(SecretCode);//建立加密对象的密钥和偏移量
                    des.IV = ASCIIEncoding.ASCII.GetBytes(SecretCode); //原文使用ASCIIEncoding.ASCII方法的GetBytes方法
                    byte[] inputbyteArray = Encoding.Default.GetBytes(data);//把字符串放到byte数组中
                    MemoryStream ms = new MemoryStream();//创建其支持存储区为内存的流 

                    CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                    cs.Write(inputbyteArray, 0, inputbyteArray.Length);
                    cs.FlushFinalBlock();

                    StringBuilder ret = new StringBuilder();
                    foreach (byte b in ms.ToArray())
                    {
                        ret.AppendFormat("{0:X2}", b);
                    }
                    result = ret.ToString();
                }
            }
            catch (Exception ex)
            {
                result = ex.ToString();
            }
            return result;
        }
        /// 
        /// 解密E函数
        /// 
        /// 被解密的字符串
        /// 返回被解密的字符串
        public static string DesDecrypt(string data)
        {
            string result = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(data) == false)
                {
                    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                    byte[] inputbyteArray = new byte[data.Length / 2];
                    for (int i = 0; i < data.Length / 2; i++)
                    {
                        int x = Convert.ToInt32(data.Substring(i * 2, 2), 16);
                        inputbyteArray[i] = (byte)x;
                    }
                    des.Key = ASCIIEncoding.ASCII.GetBytes(SecretCode);
                    des.IV = ASCIIEncoding.ASCII.GetBytes(SecretCode);
                    MemoryStream ms = new MemoryStream();
                    CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                    cs.Write(inputbyteArray, 0, inputbyteArray.Length);
                    cs.FlushFinalBlock();
                    result = System.Text.Encoding.Default.GetString(ms.ToArray());
                }
            }
            catch (Exception ex)
            {
                result = ex.ToString();
            }
            return result;
        }

你可能感兴趣的:(系统,RSA加密解密)