最近的一些 杂记,MD5加密

来源:互联网 发布:公司电脑网络部署方案 编辑:程序博客网 时间:2024/05/17 09:37

1.string

转化大小写,strplus.ToLowerInvariant()  小写

ToUpperInvariant()大写;

2..NET自带MD5加密

 public String md5(String s)
        {
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);
            bytes = md5.ComputeHash(bytes);
            md5.Clear();


            string ret = "";
            for (int i = 0; i < bytes.Length; i++)
            {
                ret += Convert.ToString(bytes[i], 16).PadLeft(2, '0');
            }


            return ret.PadLeft(32, '0');
        }

3.

using System.Security.Cryptography;
using System.IO; 
using System.Text; 
///MD5加密
   public string MD5Encrypt(string pToEncrypt, string sKey)
   { 
    DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
    byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt); 
    des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); 
    des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); 
    MemoryStream ms = new MemoryStream(); 
    CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(),CryptoStreamMode.Write); 
    cs.Write(inputByteArray, 0, inputByteArray.Length); 
    cs.FlushFinalBlock(); 
    StringBuilder ret = new StringBuilder(); 
    foreach(byte b in ms.ToArray()) 
    { 
     ret.AppendFormat("{0:X2}", b); 
    } 
    ret.ToString(); 
    return ret.ToString();


   }

   ///MD5解密
   public string MD5Decrypt(string pToDecrypt, string sKey)
   { 
    DESCryptoServiceProvider des = new DESCryptoServiceProvider();

    byte[] inputByteArray = new byte[pToDecrypt.Length / 2]; 
    for(int x = 0; x < pToDecrypt.Length / 2; x++) 
    { 
     int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16)); 
     inputByteArray[x] = (byte)i; 
    }

    des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); 
    des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); 
    MemoryStream ms = new MemoryStream(); 
    CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(),CryptoStreamMode.Write); 
    cs.Write(inputByteArray, 0, inputByteArray.Length); 
    cs.FlushFinalBlock();

    StringBuilder ret = new StringBuilder(); 
             
    return System.Text.Encoding.Default.GetString(ms.ToArray()); 
   }

此代码来自网友,本人亲自试过,可以使用 算法实用