zoukankan      html  css  js  c++  java
  • 加密与解密md5 3des

    /// <summary>
            /// MD5加密
            /// </summary>
            /// <param name="s"></param>
            /// <returns></returns>
            public static string MD5Encrypt(string s)
            {
                string strResult = "";
                System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
                byte[] bytResult = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(s));
                for (int i = 0; i < bytResult.Length; i++)
                {
                    strResult = strResult + bytResult[i].ToString("x").PadLeft(2,'0');
                }
                return strResult;
            }
    
            /// <summary>
            /// 与PHP兼容的MD5加密
            /// </summary>
            /// <param name="password"></param>
            /// <returns></returns>
            public static string MD5(string password)
            {
    
                byte[] textBytes = System.Text.Encoding.UTF8.GetBytes(password);
                try
                {
                    System.Security.Cryptography.MD5CryptoServiceProvider cryptHandler;
                    cryptHandler = new System.Security.Cryptography.MD5CryptoServiceProvider();
                    byte[] hash = cryptHandler.ComputeHash(textBytes);
                    string ret = "";
                    foreach (byte a in hash)
                    {
                        if (a < 16)
                            ret += "0" + a.ToString("x");
                        else
                            ret += a.ToString("x");
                    }
                    return ret;
                }
                catch
                {
                    throw;
                }
    
            }
    
            /// <summary>
            /// 16位md5加密,小写
            /// </summary>
            /// <param name="password"></param>
            /// <returns></returns>
            public static string MD5_16(string password) {
                return MD5(password).ToLower().Substring(8, 16);
            }
    private static string KEY = "222222222222222222";
            private static string IV = "11111222211222111";
    
            /// <summary>
            /// 3DES解密
            /// </summary>
            /// <param name="strText"></param>
            /// <param name="key"></param>
            /// <param name="iv"></param>
            /// <returns></returns>
            public static String DESDecrypt(String strText, string key, string iv)
            {
                System.Security.Cryptography.TripleDESCryptoServiceProvider des3 = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
                des3.Key = Convert.FromBase64String(key);
                des3.IV = Convert.FromBase64String(iv);
    
                string result = string.Empty;
                try
                {
                    byte[] buffer = Convert.FromBase64String(strText);
                    buffer = des3.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length);
                    result = System.Text.Encoding.UTF8.GetString(buffer);
                }
                catch
                {
                   
                }
                return result;
            }
    
            /// <summary>
            /// 3DES加密
            /// </summary>
            /// <param name="strText">待加密文字</param>
            /// <param name="key">KEY</param>
            /// <param name="iv">向量</param>
            /// <returns></returns>
            public static string DESEncrypt(string strText, string key, string iv)
            {
                System.Security.Cryptography.TripleDESCryptoServiceProvider des3 = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
                des3.Key = Convert.FromBase64String(key);
                des3.IV = Convert.FromBase64String(iv);
    
                string result = string.Empty;
                try
                {
                    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(strText);
                    buffer = des3.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length);
                    result = Convert.ToBase64String(buffer);
                }
                catch
                {
                    
                }
                return result;
            }
  • 相关阅读:
    JAVASCRIPT高程笔记-------JSON与AJAX
    JAVASCRIPT高程笔记-------第十章 DOM对象
    JAVASCRIPT高程笔记-------第八章 浏览器BOM对象
    JAVASCRIPT高程笔记-------第 七章 函数表达式
    JAVASCRIPT高程笔记-------第六章 面向对象的程序设计
    JAVASCRIPT高程笔记-------第五章 引用类型
    javascript高程笔记-------第四章 变量、作用域和内存问题
    redis 从0 到 1 键值相关命令 服务器相关命令
    SnpHub搭建(三) | 手动处理数据后的配置文件填写
    SnpHub搭建 | 数据处理中可能出现的问题
  • 原文地址:https://www.cnblogs.com/jasonlny/p/3747156.html
Copyright © 2011-2022 走看看