using System.Security.Cryptography; using System.Text; public class CryptoHelperService { #region TripleDES ///// ///// TripleDES加密 ///// ///// 明文字符串 ///// 16位key ///// 8位iv ///// //public static string TripleDESEncrypt(string clearText, string key, string iv) //{ // SymmetricAlgorithm provider = SymmetricAlgorithm.Create("TripleDES"); // provider.Key = Encoding.UTF8.GetBytes(key);//16位 // provider.IV = Encoding.UTF8.GetBytes(iv);//8位 // ICryptoTransform encryptor = provider.CreateEncryptor(); // return Encrypt(clearText, encryptor); //} ///// ///// TripleDES解密 ///// ///// 加密的文本 ///// 16位key ///// 8位iv ///// //public static string TripleDESDecrypt(string encryptedText, string key, string iv) //{ // try // { // SymmetricAlgorithm provider = SymmetricAlgorithm.Create("TripleDES"); // provider.Key = Encoding.UTF8.GetBytes(key);//16位 // provider.IV = Encoding.UTF8.GetBytes(iv);//8位 // ICryptoTransform decryptor = provider.CreateDecryptor(); // return Decrypt(encryptedText, decryptor); // } // catch // { // return string.Empty; // } //} #endregion #region DES /// /// DES加密 /// /// 明文字符串 /// 8位key /// 8位iv /// //public static string DESEncrypt(string clearText, string key, string iv) //{ // SymmetricAlgorithm provider = SymmetricAlgorithm.Create("DES"); // provider.Key = Encoding.UTF8.GetBytes(key);//8位 // provider.IV = Encoding.UTF8.GetBytes(iv);//8位 // ICryptoTransform encryptor = provider.CreateEncryptor(); // return Encrypt(clearText, encryptor); //} /// /// DES解密 /// /// 加密的文本 /// 8位key /// 8位iv /// //public static string DESDecrypt(string encryptedText, string key, string iv) //{ // SymmetricAlgorithm provider = SymmetricAlgorithm.Create("DES"); // provider.Key = Encoding.UTF8.GetBytes(key);//8位 // provider.IV = Encoding.UTF8.GetBytes(iv);//8位 // ICryptoTransform decryptor = provider.CreateDecryptor(); // return Decrypt(encryptedText, decryptor); //} #endregion //// DES,TripleDES通用加密算法 //private static string Encrypt(string clearText, ICryptoTransform encryptor) //{ // // 创建明文流 // byte[] clearBuffer = Encoding.UTF8.GetBytes(clearText); // MemoryStream clearStream = new MemoryStream(clearBuffer); // // 创建空的密文流 // MemoryStream encryptedStream = new MemoryStream(); // CryptoStream cryptoStream = // new CryptoStream(encryptedStream, encryptor, CryptoStreamMode.Write); // // 将明文流写入到buffer中 // // 将buffer中的数据写入到cryptoStream中 // int BufferSize = 1024; // int bytesRead = 0; // byte[] buffer = new byte[BufferSize]; // do // { // bytesRead = clearStream.Read(buffer, 0, BufferSize); // cryptoStream.Write(buffer, 0, bytesRead); // } while (bytesRead > 0); // cryptoStream.FlushFinalBlock(); // // 获取加密后的文本 // buffer = encryptedStream.ToArray(); // string encryptedText = Convert.ToBase64String(buffer); // return encryptedText; //} //// DES,TripleDES通用解密算法 //private static string Decrypt(string encryptedText, ICryptoTransform decryptor) //{ // try // { // byte[] encryptedBuffer = Convert.FromBase64String(encryptedText); // Stream encryptedStream = new MemoryStream(encryptedBuffer); // MemoryStream clearStream = new MemoryStream(); // CryptoStream cryptoStream = // new CryptoStream(encryptedStream, decryptor, CryptoStreamMode.Read); // int BufferSize = 1024; // int bytesRead = 0; // byte[] buffer = new byte[BufferSize]; // do // { // bytesRead = cryptoStream.Read(buffer, 0, BufferSize); // clearStream.Write(buffer, 0, bytesRead); // } while (bytesRead > 0); // buffer = clearStream.GetBuffer(); // string clearText = // Encoding.UTF8.GetString(buffer, 0, (int)clearStream.Length); // return clearText; // } // catch // { // return string.Empty; // } //} #region Rijndael /// /// 获得密钥 /// /// 密钥 private static byte[] GetLegalKey(RijndaelManaged Rijndael, string key) { string KeyTemp = key; Rijndael.GenerateKey(); byte[] bytTemp = Rijndael.Key; int KeyLength = bytTemp.Length; if (KeyTemp.Length > KeyLength) KeyTemp = KeyTemp.Substring(0, KeyLength); else if (KeyTemp.Length < KeyLength) KeyTemp = KeyTemp.PadRight(KeyLength, ' '); return ASCIIEncoding.ASCII.GetBytes(KeyTemp); } /// /// 获得初始向量IV /// /// 初试向量IV private static byte[] GetLegalIV(RijndaelManaged Rijndael, string iv) { string IVTemp = iv; Rijndael.GenerateIV(); byte[] bytTemp = Rijndael.IV; int IVLength = bytTemp.Length; if (IVTemp.Length > IVLength) IVTemp = IVTemp.Substring(0, IVLength); else if (IVTemp.Length < IVLength) IVTemp = IVTemp.PadRight(IVLength, ' '); return ASCIIEncoding.ASCII.GetBytes(IVTemp); } /// /// Rijndael加密 /// /// 明文 /// key:8位 /// iv:8位 /// public static string RijndaelEncrypt(string clearText, string key, string iv) { RijndaelManaged Rijndael = new RijndaelManaged(); byte[] bytIn = UTF8Encoding.UTF8.GetBytes(clearText); MemoryStream ms = new MemoryStream(); Rijndael.Key = GetLegalKey(Rijndael, key); Rijndael.IV = GetLegalIV(Rijndael, iv); ICryptoTransform encrypto = Rijndael.CreateEncryptor(); CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write); cs.Write(bytIn, 0, bytIn.Length); cs.FlushFinalBlock(); ms.Close(); byte[] bytOut = ms.ToArray(); return Convert.ToBase64String(bytOut); } /// /// Rijndael解密 /// /// 加密的文本 /// key:8位 /// iv:8位 /// public static string RijndaelDecrypt(string encryptedText, string key, string iv) { try { RijndaelManaged Rijndael = new RijndaelManaged(); byte[] bytIn = Convert.FromBase64String(encryptedText); MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length); Rijndael.Key = GetLegalKey(Rijndael, key); Rijndael.IV = GetLegalIV(Rijndael, iv); ICryptoTransform encrypto = Rijndael.CreateDecryptor(); CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read); StreamReader sr = new StreamReader(cs); return sr.ReadToEnd(); } catch { return string.Empty; } } #endregion #region RSA /// /// 获取秘钥 /// /// 私钥 /// 公钥 public static void RSAGetKey(out string privateKey, out string publicKey) { //加密解密用到的公钥与私钥 RSACryptoServiceProvider oRSA = new RSACryptoServiceProvider(); privateKey = oRSA.ToXmlString(true);//私钥 publicKey = oRSA.ToXmlString(false);//公钥 } /// /// RSA加密 /// /// 明文 /// 公钥 /// public static string RSAEncrypt(string clearText, string publicKey) { byte[] bytes = Encoding.UTF8.GetBytes(clearText); //需要加密的数据 //公钥加密 RSACryptoServiceProvider oRSA1 = new RSACryptoServiceProvider(); oRSA1.FromXmlString(publicKey); //加密要用到公钥所以导入公钥 byte[] AOutput = oRSA1.Encrypt(bytes, true); //AOutput 加密以后的数据, true:OAEP填充方式 return Convert.ToBase64String(AOutput); } /// /// RSA解密 /// /// RSA加密文本 /// 私钥 /// public static string RSADecrypt(string encryptedText, string privateKey) { try { RSACryptoServiceProvider oRSA2 = new RSACryptoServiceProvider(); oRSA2.FromXmlString(privateKey); byte[] AOutput = Convert.FromBase64String(encryptedText); byte[] AInput = oRSA2.Decrypt(AOutput, true);//true:OAEP填充方式 string reslut = Encoding.UTF8.GetString(AInput); return reslut; } catch { return string.Empty; } } /// /// RSA数字签名 /// /// 明文 /// 私钥 /// public static string RSASign(string clearText, string privateKey) { byte[] bs = Encoding.UTF8.GetBytes(clearText); RSACryptoServiceProvider oRSA = new RSACryptoServiceProvider(); //私钥签名 oRSA.FromXmlString(privateKey); byte[] AOutput = oRSA.SignData(bs, "SHA1"); return Convert.ToBase64String(AOutput); } /// /// RSA验证数字签名 /// /// 明文 /// 数字签名 /// 公钥 /// public static bool RSAVerify(string clearText, string signEncryptedText, string publicKey) { byte[] bs1 = Encoding.UTF8.GetBytes(clearText); byte[] bs2 = Convert.FromBase64String(signEncryptedText); RSACryptoServiceProvider oRSA = new RSACryptoServiceProvider(); //公钥验证 oRSA.FromXmlString(publicKey); bool bVerify = oRSA.VerifyData(bs1, "SHA1", bs2); return bVerify; } #endregion #region DSA /// /// 获取秘钥 /// /// 私钥 /// 公钥 public static void DSAGetKey(out string privateKey, out string publicKey) { //加密解密用到的公钥与私钥 DSACryptoServiceProvider DSA = new DSACryptoServiceProvider(); privateKey = DSA.ToXmlString(true);//私钥 publicKey = DSA.ToXmlString(false);//公钥 } /// /// 获取数字签名 /// /// 明文 /// 私钥 /// public static string DSASign(string clearText, string privateKey) { byte[] bs = Encoding.UTF8.GetBytes(clearText); DSACryptoServiceProvider DSA = new DSACryptoServiceProvider(); //私钥签名 DSA.FromXmlString(privateKey); byte[] AOutput = DSA.SignData(bs); return Convert.ToBase64String(AOutput); } /// /// 验证签名 /// /// 明文 /// 明文的数字签名 /// 公钥 /// public static bool DSAVerify(string clearText, string signEncryptedText, string publicKey) { byte[] bs1 = Encoding.UTF8.GetBytes(clearText); byte[] bs2 = Convert.FromBase64String(signEncryptedText); DSACryptoServiceProvider DSA = new DSACryptoServiceProvider(); //公钥验证 DSA.FromXmlString(publicKey); bool bVerify = DSA.VerifyData(bs1, bs2); return bVerify; } #endregion }