RSA 不可逆加解密简类

本文介绍了如何使用RSA算法进行不可逆加密与解密操作。通过调用RsaHelper类,生成公钥和私钥,然后对字符串进行加密和解密。同时,还展示了使用私钥进行数据签名以及公钥验证签名的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 调用

string tmpPriKey, tmpPubKey;
            RsaHelper.GetPriPubKey(out tmpPriKey, out tmpPubKey);
            string content = "这是im 223~! #苛sdkfj23";
            string inString = RsaHelper.RsaEncrypt(tmpPubKey, content);
            string outString = RsaHelper.RsaDecrypt(tmpPriKey, inString);


            var tmpBb = RsaHelper.RsaSignData(tmpPriKey, content);
            var tmpResult = RsaHelper.RsaVerifyData(tmpPubKey, content, tmpBb);


/// <summary>

    /// Rsa加解密
    /// </summary>
    public class RsaHelper
    {
        /// <summary>
        /// 获取私钥、公钥
        /// </summary>
        /// <param name="priKey"></param>
        /// <param name="pubKey"></param>
        public static void GetPriPubKey(out string priKey, out string pubKey)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            priKey = rsa.ToXmlString(true);
            pubKey = rsa.ToXmlString(false);
        }
        /// <summary>
        /// RSA加密
        /// </summary>
        /// <param name="publickey"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string RsaEncrypt(string publickey, string content)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(publickey);
            var cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);
            return Convert.ToBase64String(cipherbytes);


        }
        /// <summary>
        /// RSA解密
        /// </summary>
        /// <param name="privateKey"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string RsaDecrypt(string privateKey, string content)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(privateKey);
            var cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false);
            return Encoding.UTF8.GetString(cipherbytes);
        }


        /// <summary>
        /// RSA私钥签名
        /// </summary>
        /// <param name="privatekey"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static byte[] RsaSignData(string privatekey, string content)
        {
            RSACryptoServiceProvider oRSA3 = new RSACryptoServiceProvider();
            oRSA3.FromXmlString(privatekey);
            byte[] messagebytes = Encoding.UTF8.GetBytes(content);
            byte[] aOutput = oRSA3.SignData(messagebytes, "SHA1");
            return aOutput;
        }


        /// <summary>
        /// RSA公钥验证
        /// </summary>
        /// <param name="publickey"></param>
        /// <param name="content"></param>
        /// <param name="messagebytes"></param>
        /// <returns></returns>
        public static bool RsaVerifyData(string publickey, string content, byte[] messagebytes)
        {
            RSACryptoServiceProvider oRSA4 = new RSACryptoServiceProvider();
            oRSA4.FromXmlString(publickey);
            bool bVerify = oRSA4.VerifyData(Encoding.UTF8.GetBytes(content), "SHA1", messagebytes);
            return bVerify;


        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值