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

    .net名称空间System.Security.Cryptography下DESCryptoServiceProvider类为我们提供了加密和解密方法,我们只需少许代码便可实现加密和解密。

    稍感不托的地方,如果不是自行加密的在解密时会报错。

    使用注意事项,密钥64位,8个字符。

    定义默认加密密钥

    const string KEY_64 = "ChinabCd";
    const string IV_64 = "ChinabCd";

    加密

    /// <summary>
    /// 按指定键值进行加密
    /// </summary>
    /// <param name="strContent">要加密字符</param>
    /// <param name="strKey">自定义键值</param>
    /// <returns></returns>
    
    public static string EnCrypt(string strContent, string strKey)
    
            {
                if (string.IsNullOrEmpty(strContent)) return string.Empty;
                if (strKey.Length > 8) strKey = strKey.Substring(0, 8); else strKey = KEY_64;
                byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(strKey);
                byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
    
                DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
                int i = cryptoProvider.KeySize;
                MemoryStream ms = new MemoryStream();
                CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
    
                StreamWriter sw = new StreamWriter(cst);
                sw.Write(strContent);
                sw.Flush();
                cst.FlushFinalBlock();
                sw.Flush();
                return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
            }
    View Code

     解密

     /// <summary>
            /// 按指定键值进行解密
            /// </summary>
            /// <param name="strContent">要解密字符</param>
            /// <param name="strKey">加密时使用的键值</param>
            /// <returns></returns>
            public static string DeCrypt(string strContent, string strKey)
            {
                if (string.IsNullOrEmpty(strContent)) return string.Empty;
                if (strKey.Length > 8) strKey = strKey.Substring(0, 8); else strKey = KEY_64;
                byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(strKey);
                byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
    
                byte[] byEnc;
                try
                {
                    byEnc = Convert.FromBase64String(strContent);
                }
                catch
                {
                    return null;
                }
    
                DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
                MemoryStream ms = new MemoryStream(byEnc);
                CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
                StreamReader sr = new StreamReader(cst);
                return sr.ReadToEnd();
            }
    View Code
  • 相关阅读:
    P2176 [USACO14FEB]路障Roadblock
    洛谷 P1187 3D模型
    洛谷 P2777 [AHOI2016初中组]自行车比赛
    洛谷P2896 [USACO08FEB]一起吃饭Eating Together
    洛谷P2983 [USACO10FEB]购买巧克力Chocolate Buying
    洛谷 P2858 [USACO06FEB]奶牛零食Treats for the Cows
    Restaurant
    OR in Matrix
    poj 3321Apple Tree
    Codeforces Round #204 (Div. 2) C. Jeff and Rounding
  • 原文地址:https://www.cnblogs.com/lucika/p/3480184.html
Copyright © 2011-2022 走看看