zoukankan      html  css  js  c++  java
  • C#加密解密算法

    记录备忘

    就SHA1与MD5在C#上的使用来看其实差不多,因为已有底层实现,使用的时候只是用不同的实例调用一下ComputeHash方法。另外,SHA1和MD5都是只能加密,无法解密出原来数据的,因为只摘取了部分数据出来处理。

    SHA1加密与验证:

     /// <summary>
            /// SHA1加密
            /// </summary>
            public static string SHA1Encrypt(string input, Encoding encoding = null)
            {
                if (encoding == null)
                {
                    encoding = Encoding.UTF8;
                }
                SHA1 sha1 = SHA1.Create();
                byte[] data = sha1.ComputeHash(encoding.GetBytes(input));
                StringBuilder sBuilder = new StringBuilder();
                for (int i = 0; i < data.Length; i++)
                {
                    sBuilder.Append(data[i].ToString("x2"));
                }
                return sBuilder.ToString();
            }
            public static bool SHA1Verify(string input,string hash,Encoding encoding = null)
            {
                string hashOfInput = SHA1Encrypt(input, encoding);
                StringComparer comparer = StringComparer.OrdinalIgnoreCase;
                return 0 == comparer.Compare(hashOfInput, hash);            
            }

    MD5加密与验证:

    /// <summary>
            /// MD5加密
            /// </summary>
            /// <param name="input">需要加密的内容</param>
            /// <param name="encoding">字符编码</param>
            /// <returns></returns>
            public static string MD5Encrypt(string input, Encoding encoding = null)
            {
                if (encoding == null)
                {
                    encoding = Encoding.UTF8;
                }
                MD5 md5Hasher = MD5.Create();
                byte[] data = md5Hasher.ComputeHash(encoding.GetBytes(input));
                StringBuilder sBuilder = new StringBuilder();
                for (int i = 0; i < data.Length; i++)
                {
                    sBuilder.Append(data[i].ToString("x2"));
                }
                return sBuilder.ToString();
            }
    
            /// <summary>
            /// MD5验证传入的密文是否经由传入的明文加密所得
            /// </summary>
            /// <param name="input">明文</param>
            /// <param name="hash">密文</param>
            /// <returns>密文是否由明文加密所得</returns>
            public static bool MD5Verify(string input, string hash, Encoding encoding = null)
            {            
                string hashOfInput = MD5Encrypt(input, encoding); // Create a StringComparer and compare the hashes.
                StringComparer comparer = StringComparer.OrdinalIgnoreCase;
                return 0 == comparer.Compare(hashOfInput, hash);            
            }

    DES加密解密,据我所知,加密和解密要一起使用的话,两个方法需要向量一致,就是IV参数:

    /// <summary>
            /// DES 加密 注意:密钥必须为8位
            /// </summary>
            /// <param name="inputString">待加密字符串</param>
            /// <param name="encryptKey">密钥</param>
            /// <returns>加密后的字符串</returns>
            public static string DesEncrypt(string inputString, string encryptKey)
            {
                byte[] byKey = null;
                byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
                byKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.UTF8.GetBytes(inputString);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write))
                    {
                        cs.Write(inputByteArray, 0, inputByteArray.Length);
                        cs.FlushFinalBlock();
                        return Convert.ToBase64String(ms.ToArray());
                    }
                }
            }
    
            /// <summary>
            /// DES 解密 注意:密钥必须为8位
            /// </summary>
            /// <param name="inputString">待解密字符串</param>
            /// <param name="decryptKey">密钥</param>
            /// <returns>解密后的字符串</returns>
            public static string DesDecrypt(string inputString, string decryptKey)
            {
                byte[] byKey = null;
                byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
                byte[] inputByteArray = new Byte[inputString.Length];
                byKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                inputByteArray = Convert.FromBase64String(inputString);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write))
                    {
                        cs.Write(inputByteArray, 0, inputByteArray.Length);
                        cs.FlushFinalBlock();
                        return Encoding.UTF8.GetString(ms.ToArray());
                    }
                }
                    
            }
  • 相关阅读:
    2019牛客暑期多校训练营(第六场)
    2019牛客暑期多校训练营(第五场)
    2019牛客暑期多校训练营(第四场)
    2019牛客暑期多校训练营(第三场)
    Codeforces Round #554 (Div. 2) C. Neko does Maths (数论 GCD(a,b) = GCD(a,b-a))
    Codeforces Round #486 (Div. 3) C "Equal Sums" (map+pair<>)
    Count New String
    【模板】后缀自动机 (SAM)
    Watchcow
    二次剩余
  • 原文地址:https://www.cnblogs.com/lihan829/p/9250622.html
Copyright © 2011-2022 走看看