美文网首页
2019-12-10 C#加密算法

2019-12-10 C#加密算法

作者: 1f658716b568 | 来源:发表于2019-12-10 15:20 被阅读0次
    /// <summary>
            /// 16位MD5加密
            /// </summary>
            /// <param name="password"></param>
            /// <returns></returns>
            public static string MD5Encrypt16(string password)
            {
                var md5 = new MD5CryptoServiceProvider();
                string t2 = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(password)), 4, 8);
                t2 = t2.Replace("-", "");
                return t2;
            }
    
            /// <summary>
            /// 32位MD5加密
            /// </summary>
            /// <param name="password"></param>
            /// <returns></returns>
            public static string MD5Encrypt32(string password)
            {
                string cl = password;
                string pwd = "";
                MD5 md5 = MD5.Create(); //实例化一个md5对像
                // 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择 
                byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
                // 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
                for (int i = 0; i < s.Length; i++)
                {
                    // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符 
                    pwd = pwd + s[i].ToString("x2");
                }
                return pwd;
            }
    
    
            /// <summary>
            /// 64位MD5加密
            /// </summary>
            public static string MD5Encrypt64(string password)
            {
                string cl = password;
                //string pwd = "";
                MD5 md5 = MD5.Create(); //实例化一个md5对像
                // 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择 
                byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
                return Convert.ToBase64String(s);
            }
            /// <summary>
            /// SHA1 加密 
            /// </summary>
            public static string SHA1Encrypt(string str)
            {
                var buffer = Encoding.UTF8.GetBytes(str);
                var data = SHA1.Create().ComputeHash(buffer);
    
                StringBuilder sub = new StringBuilder();
                foreach (var t in data)
                {
                    sub.Append(t.ToString("X2"));
                }
    
                return sub.ToString();
            }
            /// <summary>
            /// SHA256 加密 
            /// </summary>
            public string SHA256ToBase64(string data)
            {
                byte[] bytes = Encoding.UTF8.GetBytes(data);
                byte[] hash = SHA256Managed.Create().ComputeHash(bytes);
    
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < hash.Length; i++)
                {
                    builder.Append(hash[i].ToString("X2"));
                }
    
                return builder.ToString();
            }
            /// <summary>
            /// SHA512 加密 
            /// </summary>
            /// <param name="source"></param>
            /// <returns></returns>
            public static string sha512Encode(string source)
            {
                string result = "";
                byte[] buffer = Encoding.UTF8.GetBytes(source); //UTF-8 编码
    
                //64字节,512位          
                SHA512CryptoServiceProvider SHA512 = new SHA512CryptoServiceProvider();
                byte[] h5 = SHA512.ComputeHash(buffer);
                result = BitConverter.ToString(h5).Replace("-", string.Empty);
                return result.ToLower();
            }
    

    相关文章

      网友评论

          本文标题:2019-12-10 C#加密算法

          本文链接:https://www.haomeiwen.com/subject/rndggctx.html