最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

RSA類

2022-01-12 20:32 作者:佳語順  | 我要投稿

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


RSA類的評論 (共 條)

分享到微博請遵守國家法律
阿城市| 惠来县| 墨竹工卡县| 慈溪市| 泸西县| 巴里| 华坪县| 临洮县| 尖扎县| 西吉县| 施甸县| 沐川县| 星子县| 雷州市| 沅陵县| 册亨县| 汤阴县| 尖扎县| 洮南市| 青岛市| 义乌市| 罗源县| 南平市| 封丘县| 莱阳市| 武夷山市| 枣阳市| 泗水县| 奉贤区| 抚松县| 阳高县| 敦煌市| 仙游县| 汝城县| 五河县| 卢龙县| 龙陵县| 绿春县| 凤翔县| 盱眙县| 台湾省|