zoukankan      html  css  js  c++  java
  • 对称加密、解密

     /// <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
        }
  • 相关阅读:
    细说MS事务管理
    大话设计模式(含源码)下载
    CSS基础到提高(PPT、视频、源代码),网页布局不用愁
    Android开发中Layout中明明改了id,但是还出现"cannot be resolved or is not a field"的原因
    读《OO真经》有感,以及我自己的关于哲学的体会
    Web服务小试牛刀
    在Android的RaletiveLayout中,如果空间的相对位置矛盾将不显示此控件
    .net 面试题2
    敏捷软件转
    中英文标点
  • 原文地址:https://www.cnblogs.com/tangchun/p/7872727.html
Copyright © 2011-2022 走看看