using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace Siia.Veima.Host.Utils { public class CommonFunction { /// <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("X"); //} //return pwd; MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(password))); t2 = t2.Replace("-", ""); return t2; } /// <summary> /// 加密 /// </summary> /// <param name="characters">要加密的明文</param> /// <returns>加密后的密文</returns> public static string EncryptStr(string characters) { string key = "330ead0b-4951-46a7-8db8-0e2569472fd1"; //位移加密 byte[] bStr = (new UnicodeEncoding()).GetBytes(characters); for (int i = 0; i < bStr.Length; i++) { byte b = (byte)(bStr[i] + 255); bStr[i] = b; } byte[] bKey = (new UnicodeEncoding()).GetBytes(key);//加密鎖 //異或加密 for (int i = 0; i < bStr.Length; i += 2) { for (int j = 0; j < bKey.Length; j += 3) { bStr[i] = Convert.ToByte(bStr[i] ^ bKey[j]); } } return (new UnicodeEncoding()).GetString(bStr).TrimEnd('\0'); } /// <summary> /// 解密 /// </summary> /// <param name="ciphertext">要解密的密文</param> /// <returns>解密后的明文</returns> public static string DecryptStr(string ciphertext) { string key = "330ead0b-4951-46a7-8db8-0e2569472fd1"; byte[] bStr = (new UnicodeEncoding()).GetBytes(ciphertext); byte[] bKey = (new UnicodeEncoding()).GetBytes(key);//加密鎖 //異或解密 for (int i = 0; i < bStr.Length; i += 2) { for (int j = 0; j < bKey.Length; j += 3) { bStr[i] = Convert.ToByte(bStr[i] ^ bKey[j]); } } //位移解密 for (int i = 0; i < bStr.Length; i++) { byte b = (byte)(bStr[i] - 255); bStr[i] = b; } return (new UnicodeEncoding()).GetString(bStr).TrimEnd('\0'); } //加密 public static string Encryption(string express) { CspParameters param = new CspParameters(); param.KeyContainerName = "siia";//密匙容器的名稱,保持加密解密一致才能解密成功 using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param)) { byte[] plaindata = Encoding.Default.GetBytes(express);//將要加密的字符串轉換為字節數組 byte[] encryptdata = rsa.Encrypt(plaindata, false);//將加密后的字節數據轉換為新的加密字節數組 return Convert.ToBase64String(encryptdata);//將加密后的字節數組轉換為字符串 } } //解密 public static string Decrypt(string ciphertext) { CspParameters param = new CspParameters(); param.KeyContainerName = "330ead0b-4951-46a7-8db8-0e2569472fd1"; using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param)) { byte[] encryptdata = Convert.FromBase64String(ciphertext); byte[] decryptdata = rsa.Decrypt(encryptdata, false); return Encoding.Default.GetString(decryptdata); } } } }