using System; using System.Collections.Generic; using System.Security.Cryptography; using System.Text; namespace AlicizaX { public static partial class Utility { /// /// 加密解密相关的实用函数。 /// public static partial class Encryption { public class Rsa { private readonly RSACryptoServiceProvider _rsa = null; public Rsa(RSACryptoServiceProvider rsa) { _rsa = rsa; } public Rsa(string key) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(key); this._rsa = rsa; } public static Dictionary Make() { Dictionary dic = new Dictionary(); RSACryptoServiceProvider dsa = new RSACryptoServiceProvider(); dic["privateKey"] = dsa.ToXmlString(true); dic["publicKey"] = dsa.ToXmlString(false); return dic; } /// /// 加密 /// /// 公钥 /// 所加密的内容 /// 加密后的内容 public static string RSAEncrypt(string publicKey, string content) { byte[] res = RSAEncrypt(publicKey, Encoding.UTF8.GetBytes(content)); return Convert.ToBase64String(res); } public string Encrypt(string content) { byte[] res = Encrypt(Encoding.UTF8.GetBytes(content)); return Convert.ToBase64String(res); } public static byte[] RSAEncrypt(string publicKey, byte[] content) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(publicKey); byte[] cipherBytes = rsa.Encrypt(content, false); return cipherBytes; } public byte[] Encrypt(byte[] content) { byte[] cipherBytes = _rsa.Encrypt(content, false); return cipherBytes; } /// /// 解密 /// /// 私钥 /// 加密后的内容 /// 解密后的内容 public static string RSADecrypt(string privateKey, string content) { byte[] res = RSADecrypt(privateKey, Convert.FromBase64String(content)); return Encoding.UTF8.GetString(res); } public static byte[] RSADecrypt(string privateKey, byte[] content) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(privateKey); byte[] cipherBytes = rsa.Decrypt(content, false); return cipherBytes; } public string Decrypt(string content) { byte[] res = Decrypt(Convert.FromBase64String(content)); return Encoding.UTF8.GetString(res); } public byte[] Decrypt(byte[] content) { byte[] bytes = _rsa.Decrypt(content, false); return bytes; } /// /// 签名 /// public static byte[] RSASignData(byte[] dataToSign, string privateKey) { try { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(privateKey); return rsa.SignData(dataToSign, new SHA1CryptoServiceProvider()); } catch { return null; } } public static string RSASignData(string dataToSign, string privateKey) { byte[] res = RSASignData(Encoding.UTF8.GetBytes(dataToSign), privateKey); return Convert.ToBase64String(res); } public byte[] SignData(byte[] dataToSign) { try { return _rsa.SignData(dataToSign, new SHA1CryptoServiceProvider()); } catch { return null; } } public string SignData(string dataToSign) { byte[] res = SignData(Encoding.UTF8.GetBytes(dataToSign)); return Convert.ToBase64String(res); } /// /// 验证签名 /// public static bool RSAVerifyData(byte[] dataToVerify, byte[] signedData, string publicKey) { try { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(publicKey); return rsa.VerifyData(dataToVerify, new SHA1CryptoServiceProvider(), signedData); } catch { return false; } } public static bool RSAVerifyData(string dataToVerify, string signedData, string publicKey) { return RSAVerifyData(Encoding.UTF8.GetBytes(dataToVerify), Convert.FromBase64String(signedData), publicKey); } public bool VerifyData(byte[] dataToVerify, byte[] signedData) { try { return _rsa.VerifyData(dataToVerify, new SHA1CryptoServiceProvider(), signedData); } catch { return false; } } public bool VerifyData(string dataToVerify, string signedData) { try { return VerifyData(Encoding.UTF8.GetBytes(dataToVerify), Convert.FromBase64String(signedData)); } catch { return false; } } } } } }