对称加密、解密_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 对称加密、解密

对称加密、解密

 2017/11/21 15:04:27  _York  程序员俱乐部  我要评论(0)
  • 摘要:///<summary>///加密///</summary>publicstaticclassEncrypting{#region使用对称加密、解密privatestaticstringGetEncryptKey(){returnConfigurationManager.AppSettings["EncryptKey"];}///<summary>///使用对称算法加密///</summary>///<paramname="str">
  • 标签:
 /// <summary>
    /// 加密
    /// </summary>
    public static class Encrypting
    {
        #region 使用对称加密、解密

        private static string GetEncryptKey()
        {
            return ConfigurationManager.AppSettings["EncryptKey"];
        }

        /// <summary>
        /// 使用对称算法加密
        /// </summary>
        /// <param name="str"></param>
        /// <param name="encryptKey"></param>
        /// <returns></returns>
        public static string SymmetricEncrypts(string str)
        {
            string result = string.Empty;
            byte[] inputData = System.Text.Encoding.UTF8.GetBytes(str);
            SymmetricAlgorithm Algorithm = null;
            MemoryStream msTarget = null;
            CryptoStream encStream = null;
            try
            {
                byte[] kv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
                //如需指定加密算法,可在Create()参数中指定字符串
                //Create()方法中的参数可以是:DES、RC2 System、Rijndael、TripleDES 
                //采用不同的实现类对IV向量的要求不一样(可以用GenerateIV()方法生成),无参数表示用Rijndael
                Algorithm = SymmetricAlgorithm.Create();//产生一种加密算法
                msTarget = new MemoryStream();
                //定义将数据流链接到加密转换的流。
                encStream = new CryptoStream(msTarget, Algorithm.CreateEncryptor(Convert.FromBase64String(GetEncryptKey()), kv), CryptoStreamMode.Write);
                encStream.Write(inputData, 0, inputData.Length);
                encStream.FlushFinalBlock();
                result = Convert.ToBase64String(msTarget.ToArray());
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                if (Algorithm != null) Algorithm.Clear();
                if (msTarget != null) msTarget.Close();
                if (encStream != null) encStream.Close();
            }
            return result;
        }


        /// <summary>
        /// 使用对称算法解密
        /// </summary>
        /// <param name="encryptStr"></param>
        /// <param name="encryptKey"></param>
        /// <returns></returns>
        public static string SymmectricDecrypts(string encryptStr)
        {
            string result = string.Empty;
            SymmetricAlgorithm Algorithm = null;
            MemoryStream msTarget = null;
            CryptoStream decStream = null;
            //加密时使用的是Convert.ToBase64String(),解密时必须使用Convert.FromBase64String()
            try
            {
                byte[] kv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
                byte[] encryptData = Convert.FromBase64String(encryptStr);
                Algorithm = SymmetricAlgorithm.Create();
                msTarget = new MemoryStream();
                decStream = new CryptoStream(msTarget, Algorithm.CreateDecryptor(Convert.FromBase64String(GetEncryptKey()), kv), CryptoStreamMode.Write);
                decStream.Write(encryptData, 0, encryptData.Length);
                decStream.FlushFinalBlock();
                result = System.Text.Encoding.Default.GetString(msTarget.ToArray());
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                if (Algorithm != null) Algorithm.Clear();
                if (msTarget != null) msTarget.Close();
                if (decStream != null) decStream.Close();
            }
            return result;
        }

        #endregion
    }

 

  • 相关文章
发表评论
用户名: 匿名