1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using System.Web;
- namespace UAS_MES.PublicMethod
- {
- class Encryption
- {
- private static string encryptKey = "MUPSWORD";
- public static string EncryptStr(string pToEncrypt)
- {
- using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
- {
- byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
- des.Key = ASCIIEncoding.ASCII.GetBytes(encryptKey);
- des.IV = ASCIIEncoding.ASCII.GetBytes(encryptKey);
- System.IO.MemoryStream ms = new System.IO.MemoryStream();
- using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
- {
- cs.Write(inputByteArray, 0, inputByteArray.Length);
- cs.FlushFinalBlock();
- cs.Close();
- }
- string str = Convert.ToBase64String(ms.ToArray());
- ms.Close();
- return str;
- }
- }
- public static string GETMD5(string password)
- {
- //初始化MD5对象
- MD5 md5 = MD5.Create();
- //将源字符串转化为byte数组
- Byte[] soucebyte = Encoding.Default.GetBytes(password);
- //soucebyte转化为mf5的byte数组
- Byte[] md5bytes = md5.ComputeHash(soucebyte);
- //将md5的byte数组再转化为MD5数组
- StringBuilder sb = new StringBuilder();
- foreach (Byte b in md5bytes)
- {
- //x表示16进制,2表示2位
- sb.Append(b.ToString("x2"));
- }
- return sb.ToString();
- }
- public static string DecryptStr(string pToDecrypt)
- {
- byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
- using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
- {
- des.Key = ASCIIEncoding.ASCII.GetBytes(encryptKey);
- des.IV = ASCIIEncoding.ASCII.GetBytes(encryptKey);
- System.IO.MemoryStream ms = new System.IO.MemoryStream();
- using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
- {
- cs.Write(inputByteArray, 0, inputByteArray.Length);
- cs.FlushFinalBlock();
- cs.Close();
- }
- string str = Encoding.UTF8.GetString(ms.ToArray());
- ms.Close();
- return str;
- }
- }
- }
- }
|