117 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			C#
		
	
	
	
		
		
			
		
	
	
			117 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			C#
		
	
	
	
|  | using System; | |||
|  | using System.Collections; | |||
|  | using System.Collections.Generic; | |||
|  | using System.IO; | |||
|  | using System.Security.Cryptography; | |||
|  | using System.Text; | |||
|  | using System.Globalization; | |||
|  | 
 | |||
|  | namespace MMO | |||
|  | { | |||
|  |     public static class MMOEncrypt | |||
|  |     { | |||
|  |         #region AES | |||
|  |         public const string Key = "toukagameqwertyu"; | |||
|  |         public const string IV = "1012132405963708"; | |||
|  | 
 | |||
|  |         public static string AESEncrypt(string plainText) | |||
|  |         { | |||
|  |             if (plainText == null || plainText.Length <= 0) | |||
|  |                 throw new ArgumentNullException("plainText"); | |||
|  |             if (Key == null || Key.Length <= 0) | |||
|  |                 throw new ArgumentNullException("Key"); | |||
|  |             if (IV == null || IV.Length <= 0) | |||
|  |                 throw new ArgumentNullException("IV"); | |||
|  | 
 | |||
|  |             byte[] encrypted; | |||
|  |             using (Aes aesAlg = Aes.Create()) | |||
|  |             { | |||
|  |                 aesAlg.Key = Encoding.ASCII.GetBytes(Key); | |||
|  |                 aesAlg.IV = Encoding.ASCII.GetBytes(IV); | |||
|  |                 ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); | |||
|  |                 using (MemoryStream msEncrypt = new MemoryStream()) | |||
|  |                 { | |||
|  |                     using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) | |||
|  |                     { | |||
|  |                         using (StreamWriter swEncrypt = new StreamWriter(csEncrypt, Encoding.UTF8)) | |||
|  |                         { | |||
|  |                             swEncrypt.Write(plainText); | |||
|  |                         } | |||
|  |                         encrypted = msEncrypt.ToArray(); | |||
|  |                     } | |||
|  |                 } | |||
|  |             } | |||
|  |             return Convert.ToBase64String(encrypted); | |||
|  |         } | |||
|  | 
 | |||
|  |         public static string AESDecrypt(string cipherStr) | |||
|  |         { | |||
|  |             var cipherText = Convert.FromBase64String(cipherStr); | |||
|  |             if (cipherText == null || cipherText.Length <= 0) | |||
|  |                 throw new ArgumentNullException("cipherText"); | |||
|  |             if (Key == null || Key.Length <= 0) | |||
|  |                 throw new ArgumentNullException("Key"); | |||
|  |             if (IV == null || IV.Length <= 0) | |||
|  |                 throw new ArgumentNullException("IV"); | |||
|  | 
 | |||
|  |             string plaintext = null; | |||
|  |             using (Aes aesAlg = Aes.Create()) | |||
|  |             { | |||
|  |                 aesAlg.Key = Encoding.ASCII.GetBytes(Key); | |||
|  |                 aesAlg.IV = Encoding.ASCII.GetBytes(IV); | |||
|  |                 ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); | |||
|  |                 using (MemoryStream msDecrypt = new MemoryStream(cipherText)) | |||
|  |                 { | |||
|  |                     using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) | |||
|  |                     { | |||
|  |                         using (StreamReader srDecrypt = new StreamReader(csDecrypt, Encoding.UTF8)) | |||
|  |                         { | |||
|  |                             plaintext = srDecrypt.ReadToEnd(); | |||
|  |                         } | |||
|  |                     } | |||
|  |                 } | |||
|  |             } | |||
|  |             return plaintext; | |||
|  |         } | |||
|  |         #endregion | |||
|  | 
 | |||
|  |         #region MD5 | |||
|  |         public const string MD5KEY = "mooncake"; | |||
|  |         // 创建Key | |||
|  |         public static string GenerateKey() | |||
|  |         { | |||
|  |             DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create(); | |||
|  |             return ASCIIEncoding.ASCII.GetString(desCrypto.Key); | |||
|  |         } | |||
|  | 
 | |||
|  |         // 加密字符串 | |||
|  |         public static string MD5Encrypt(string pOriginal) | |||
|  |         { | |||
|  |             byte[] data = Encoding.UTF8.GetBytes(pOriginal); | |||
|  |             DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); | |||
|  |             DES.Key = ASCIIEncoding.ASCII.GetBytes(MD5KEY); | |||
|  |             DES.IV = ASCIIEncoding.ASCII.GetBytes(MD5KEY); | |||
|  |             ICryptoTransform desencrypt = DES.CreateEncryptor(); | |||
|  |             byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length); | |||
|  |             return BitConverter.ToString(result); | |||
|  |         } | |||
|  | 
 | |||
|  |         // 解密字符串 | |||
|  |         public static string MD5Decrypt(string pOriginal) | |||
|  |         { | |||
|  |             string[] sInput = pOriginal.Split("-".ToCharArray()); | |||
|  |             byte[] data = new byte[sInput.Length]; | |||
|  |             for (int i = 0; i < sInput.Length; i++) | |||
|  |             { | |||
|  |                 data[i] = byte.Parse(sInput[i], NumberStyles.HexNumber); | |||
|  |             } | |||
|  |             DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); | |||
|  |             DES.Key = ASCIIEncoding.ASCII.GetBytes(MD5KEY); | |||
|  |             DES.IV = ASCIIEncoding.ASCII.GetBytes(MD5KEY); | |||
|  |             ICryptoTransform desencrypt = DES.CreateDecryptor(); | |||
|  |             byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length); | |||
|  |             return Encoding.UTF8.GetString(result); | |||
|  |         } | |||
|  |         #endregion | |||
|  |     } | |||
|  | } |