RSA類
public static void LogAdd(){}??
#region 字符串加密解密
? ? ? ? /// <summary>
? ? ? ? /// RSA字符串(英文字符串115個)加密
? ? ? ? /// </summary>
? ? ? ? /// <param name="content">要操作的字符串</param>
? ? ? ? /// <param name="privatekey">公鑰</param>
? ? ? ? /// <param name="encoding">編碼</param>
? ? ? ? /// <returns>加密輸出,字符串類型,(Base64)</returns>
? ? ? ? public static string RSAEncrypt(string content, string publickey, string encoding = "UTF-8")
? ? ? ? {
? ? ? ? ? ? string ret = null;
? ? ? ? ? ? if (string.IsNullOrEmpty(content))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? LogAdd("RSA失敗:加密字符串為空!");
? ? ? ? ? ? ? ? return ret;
? ? ? ? ? ? }
? ? ? ? ? ? if (string.IsNullOrEmpty(publickey))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? LogAdd("RSA失?。好艽a為空!");
? ? ? ? ? ? ? ? return ret;
? ? ? ? ? ? }
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
? ? ? ? ? ? ? ? byte[] cipherbytes;
? ? ? ? ? ? ? ? rsa.FromXmlString(publickey);
? ? ? ? ? ? ? ? cipherbytes = rsa.Encrypt(Encoding.GetEncoding(encoding).GetBytes(content), false);
? ? ? ? ? ? ? ? ret = Convert.ToBase64String(cipherbytes);
? ? ? ? ? ? ? ? LogAdd("RSA成功!");
? ? ? ? ? ? ? ? return ret;
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? LogAdd("RSA失?。?#34; + ex.Message);
? ? ? ? ? ? ? ? return ret;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// RSA字符串(英文字符串115個)解密
? ? ? ? /// </summary>
? ? ? ? /// <param name="content">要操作的字符串</param>
? ? ? ? /// <param name="privatekey">私鑰</param>
? ? ? ? /// <param name="encoding">編碼</param>
? ? ? ? /// <returns>解密輸出,字符串類型,指定編碼輸出</returns>
? ? ? ? public static string RSADecrypt(string content, string privatekey, string encoding = "UTF-8")
? ? ? ? {
? ? ? ? ? ? string ret = null;
? ? ? ? ? ? if (string.IsNullOrEmpty(content))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? LogAdd("RSA失敗:加密字符串為空!");
? ? ? ? ? ? ? ? return ret;
? ? ? ? ? ? }
? ? ? ? ? ? if (string.IsNullOrEmpty(privatekey))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? LogAdd("RSA失敗:密碼為空!");
? ? ? ? ? ? ? ? return ret;
? ? ? ? ? ? }
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
? ? ? ? ? ? ? ? byte[] cipherbytes;
? ? ? ? ? ? ? ? rsa.FromXmlString(privatekey);
? ? ? ? ? ? ? ? cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false);
? ? ? ? ? ? ? ? ret = Encoding.GetEncoding(encoding).GetString(cipherbytes);
? ? ? ? ? ? ? ? LogAdd("RSA成功!");
? ? ? ? ? ? ? ? return ret;
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? LogAdd("RSA失?。?#34; + ex.Message);
? ? ? ? ? ? ? ? return ret;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 容器名給字符串加密
? ? ? ? /// </summary>
? ? ? ? /// <param name="strText">添加字符串</param>
? ? ? ? /// <param name="ContainerName">容器名</param>
? ? ? ? /// <param name="encoding">編碼</param>
? ? ? ? /// <returns></returns>
? ? ? ? public static string RSAContainerEncrypt(string strText, string ContainerName, string encoding = "UTF-8")
? ? ? ? {
? ? ? ? ? ? string ret = null;
? ? ? ? ? ? if (string.IsNullOrEmpty(strText))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? LogAdd("RSA失?。杭用茏址疄榭眨?#34;);
? ? ? ? ? ? ? ? return ret;
? ? ? ? ? ? }
? ? ? ? ? ? if (string.IsNullOrEmpty(ContainerName))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? LogAdd("RSA失?。好艽a為空!");
? ? ? ? ? ? ? ? return ret;
? ? ? ? ? ? }
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? CspParameters CSApars = new CspParameters();
? ? ? ? ? ? ? ? CSApars.KeyContainerName = ContainerName;
? ? ? ? ? ? ? ? RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(CSApars);
? ? ? ? ? ? ? ? byte[] byteText = Encoding.GetEncoding(encoding).GetBytes(strText);
? ? ? ? ? ? ? ? byte[] byteEntry = rsa.Encrypt(byteText, false);
? ? ? ? ? ? ? ? ret = Convert.ToBase64String(byteEntry);
? ? ? ? ? ? ? ? LogAdd("RSA成功!");
? ? ? ? ? ? ? ? return ret;
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? LogAdd("RSA失敗:" + ex.Message);
? ? ? ? ? ? ? ? return ret;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 容器名給字符串解密
? ? ? ? /// </summary>
? ? ? ? /// <param name="strText">添加字符串</param>
? ? ? ? /// <param name="ContainerName">容器名</param>
? ? ? ? /// <param name="encoding">編碼</param>
? ? ? ? /// <returns></returns>
? ? ? ? public static string RSAContainerDecrypt(string strText, string ContainerName, string encoding = "UTF-8")
? ? ? ? {
? ? ? ? ? ? string ret = null;
? ? ? ? ? ? if (string.IsNullOrEmpty(strText))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? LogAdd("RSA失?。杭用茏址疄榭眨?#34;);
? ? ? ? ? ? ? ? return ret;
? ? ? ? ? ? }
? ? ? ? ? ? if (string.IsNullOrEmpty(ContainerName))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? LogAdd("RSA失?。好艽a為空!");
? ? ? ? ? ? ? ? return ret;
? ? ? ? ? ? }
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? CspParameters CSApars = new CspParameters();
? ? ? ? ? ? ? ? CSApars.KeyContainerName = ContainerName;
? ? ? ? ? ? ? ? RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(CSApars);
? ? ? ? ? ? ? ? byte[] byteEntry = Convert.FromBase64String(strText);
? ? ? ? ? ? ? ? byte[] byteText = rsa.Decrypt(byteEntry, false);
? ? ? ? ? ? ? ? ret = Encoding.GetEncoding(encoding).GetString(byteText);
? ? ? ? ? ? ? ? LogAdd("RSA成功!");
? ? ? ? ? ? ? ? return ret;
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? LogAdd("RSA失?。?#34; + ex.Message);
? ? ? ? ? ? ? ? return ret;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? #endregion
? ? ? ? #region 數(shù)字簽名和驗證簽名
? ? ? ? /// <summary>
? ? ? ? /// 字符串數(shù)字簽名
? ? ? ? /// </summary>
? ? ? ? /// <param name="plaintext">原文</param>
? ? ? ? /// <param name="privateKey">私鑰</param>
? ? ? ? /// <param name="algorithm">哈希</param>
? ? ? ? /// <param name="encoding">字符串編碼</param>
? ? ? ? /// <returns>簽名輸出字符串類型(Base64)</returns>
? ? ? ? public static string RSAAutograph(string plaintext, string privateKey, HashAlgorithm algorithm, string encoding = "UTF-8")
? ? ? ? {
? ? ? ? ? ? if (string.IsNullOrEmpty(plaintext))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? LogAdd("操作失?。鹤址當?shù)據(jù)為空!");
? ? ? ? ? ? ? ? return null;
? ? ? ? ? ? }
? ? ? ? ? ? if (string.IsNullOrEmpty(privateKey))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? LogAdd("操作失?。核借€為空!");
? ? ? ? ? ? ? ? return null;
? ? ? ? ? ? }
? ? ? ? ? ? //UnicodeEncoding ByteConverter = new UnicodeEncoding();
? ? ? ? ? ? byte[] dataToEncrypt = Encoding.GetEncoding(encoding).GetBytes(plaintext);
? ? ? ? ? ? //byte[] dataToEncrypt = ByteConverter.GetBytes(plaintext);
? ? ? ? ? ? using (RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? RSAalg.FromXmlString(privateKey);
? ? ? ? ? ? ? ? //使用SHA1進行摘要算法,生成簽名
? ? ? ? ? ? ? ? byte[] encryptedData = RSAalg.SignData(dataToEncrypt, algorithm);
? ? ? ? ? ? ? ? LogAdd("RSA簽名成功!");
? ? ? ? ? ? ? ? return Convert.ToBase64String(encryptedData);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 字符串驗證簽名
? ? ? ? /// </summary>
? ? ? ? /// <param name="plaintext">原文</param>
? ? ? ? /// <param name="SignedData">簽名</param>
? ? ? ? /// <param name="publicKey">公鑰</param>
? ? ? ? /// <param name="algorithm">哈希</param>
? ? ? ? /// <param name="encoding">字符串編碼</param>
? ? ? ? /// <returns>驗證成功失敗,布爾類型</returns>
? ? ? ? public static bool VerifySignedString(string plaintext, string SignedData, string publicKey, HashAlgorithm algorithm, string encoding = "UTF-8")
? ? ? ? {
? ? ? ? ? ? bool bl = false;
? ? ? ? ? ? if (string.IsNullOrEmpty(plaintext))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? LogAdd("操作失敗:字符串數(shù)據(jù)為空!");
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? ? ? if (string.IsNullOrEmpty(SignedData))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? LogAdd("操作失敗:簽名字符串數(shù)據(jù)為空!");
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? ? ? if (string.IsNullOrEmpty(publicKey))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? LogAdd("操作失?。核借€為空!");
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? ? ? using (RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? RSAalg.FromXmlString(publicKey);
? ? ? ? ? ? ? ? byte[] dataToVerifyBytes = Encoding.GetEncoding(encoding).GetBytes(plaintext);
? ? ? ? ? ? ? ? byte[] signedDataBytes = Convert.FromBase64String(SignedData);
? ? ? ? ? ? ? ? if (RSAalg.VerifyData(dataToVerifyBytes, algorithm, signedDataBytes))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? LogAdd("RSA簽名驗證成功!");
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? LogAdd("RSA簽名驗證失敗!");
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 文件添加簽名
? ? ? ? /// </summary>
? ? ? ? /// <param name="DataStream">文件流</param>
? ? ? ? /// <param name="privateKey">私鑰</param>
? ? ? ? /// <param name="algorithm">哈希算法名</param>
? ? ? ? /// <returns>簽名輸出字符串類型(Base64)</returns>
? ? ? ? public static string RSAAutographFlie(Stream DataStream, string privateKey, HashAlgorithm algorithm)
? ? ? ? {
? ? ? ? ? ? if (DataStream.Length == 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? LogAdd("操作失?。汉灻募榭?!");
? ? ? ? ? ? ? ? return null;
? ? ? ? ? ? }
? ? ? ? ? ? if (string.IsNullOrEmpty(privateKey))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? LogAdd("操作失敗:私鑰為空!");
? ? ? ? ? ? ? ? return null;
? ? ? ? ? ? }
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? DataStream.Position = 0;//當前流0位置開始,不然會出錯。
? ? ? ? ? ? ? ? RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();
? ? ? ? ? ? ? ? RSAalg.FromXmlString(privateKey);
? ? ? ? ? ? ? ? byte[] encryptedData = RSAalg.SignData(DataStream, algorithm);
? ? ? ? ? ? ? ? LogAdd("RSA簽名成功!");
? ? ? ? ? ? ? ? return Convert.ToBase64String(encryptedData);
? ? ? ? ? ? }
? ? ? ? ? ? catch (CryptographicException e)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine(e.Message);
? ? ? ? ? ? ? ? return null;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 文件驗證簽名
? ? ? ? /// </summary>
? ? ? ? /// <param name="DataStream">文件流</param>
? ? ? ? /// <param name="SignedData">簽名</param>
? ? ? ? /// <param name="publicKey">公鑰</param>
? ? ? ? /// <param name="algorithm">哈希算法名</param>
? ? ? ? /// <returns>驗證成功失敗,布爾類型</returns>
? ? ? ? public static bool RSAVerifySignedFile(Stream DataStream, string SignedData, string publicKey, HashAlgorithmName algorithm)
? ? ? ? {
? ? ? ? ? ? if (DataStream.Length == 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? LogAdd("操作失敗:簽名文件為空!");
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? ? ? if (string.IsNullOrEmpty(SignedData))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? LogAdd("操作失?。汉灻址當?shù)據(jù)為空!");
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? ? ? if (string.IsNullOrEmpty(publicKey))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? LogAdd("操作失?。汗€為空!");
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? ? ? using (RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? DataStream.Position = 0;//當前流0位置開始,不然會出錯。
? ? ? ? ? ? ? ? RSAalg.FromXmlString(publicKey);
? ? ? ? ? ? ? ? byte[] signedDataBytes = Convert.FromBase64String(SignedData);
? ? ? ? ? ? ? ? if (RSAalg.VerifyData(DataStream, signedDataBytes, algorithm, System.Security.Cryptography.RSASignaturePadding.Pkcs1))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? LogAdd("RSA簽名驗證成功!");
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? LogAdd("RSA簽名驗證失?。?#34;);
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? #endregion