using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace ClassLibrary1 { class Code { //1、base64 to string public static string Base64Encrypt(string strPath) { byte[] bpath = Convert.FromBase64String(strPath); string returnData = Encoding.Default.GetString(bpath); return returnData; } //2、string to base64 public static string Base64DesEncrypt(string strPath) { Encoding encode = Encoding.ASCII; byte[] bytedata = encode.GetBytes(strPath); string returnData = Convert.ToBase64String(bytedata, 0, bytedata.Length); return returnData; } /// <summary> /// 对字符串进MD5加密 /// </summary> /// <param name="sourceStr">源类型</param> /// <returns>加密后字符串</returns> public static string Md5Encrypt(string sourceStr) { MD5 md5 = new MD5CryptoServiceProvider(); //将要加密的字符串转换成字节数组 byte[] palindata = Encoding.Default.GetBytes(sourceStr); //通过字节数组进行加密 byte[] encryptdata = md5.ComputeHash(palindata); //将加密后的字节数组转换成字符串 string returnData = Convert.ToBase64String(encryptdata); return returnData; } /// <summary> /// Md5加密 /// </summary> /// <param name="pToEncrypt">要加密的string</param> /// <param name="sKey">要加密的key</param> /// <returns></returns> public static string Md5Encrypt(string pToEncrypt, string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt); des.Key = Encoding.ASCII.GetBytes(sKey); des.IV = Encoding.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); } var s = ret.ToString(); return s; } /// <summary> /// Md5解密 /// </summary> /// <param name="pToDecrypt">解密string</param> /// <param name="sKey">解密key(要8位数)</param> /// <returns></returns> public static 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 = Encoding.ASCII.GetBytes(sKey); des.IV = Encoding.ASCII.GetBytes(sKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Encoding.Default.GetString(ms.ToArray()); } public static CspParameters Param; /// <summary> /// 进行 RSA 加密 /// </summary> /// <param name="sourceStr">源字符串</param> /// <returns>加密后字符串</returns> public static string RsaEncrypt(string sourceStr) { Param = new CspParameters(); //密匙容器的名称,保持加密解密一致才能解密成功 Param.KeyContainerName = "Navis"; using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(Param)) { //将要加密的字符串转换成字节数组 byte[] plaindata = Encoding.Default.GetBytes(sourceStr); //通过字节数组进行加密 byte[] encryptdata = rsa.Encrypt(plaindata, false); //将加密后的字节数组转换成字符串 return Convert.ToBase64String(encryptdata); } } /// <summary> /// 通过RSA 加密方式进行解密 /// </summary> /// <param name="codingStr">加密字符串</param> /// <returns>解密后字符串</returns> public static string RsaDesEncrypt(string codingStr) { Param = new CspParameters(); Param.KeyContainerName = "Navis"; using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(Param)) { byte[] encryptdata = Convert.FromBase64String(codingStr); byte[] decryptdata = rsa.Decrypt(encryptdata, false); return Encoding.Default.GetString(decryptdata); } } } }