一个使用公钥签名与加密,使用密钥解密的案例。。。

        static void Main(string[] args)
        {
            string s = "老王头,这周又开始忙了!";
            byte[] data;
            RSACryptoServiceProvider rsp = new RSACryptoServiceProvider();
            System.IO.File.WriteAllText(@"D:\wangran\公钥.xml", rsp.ToXmlString(false));
            System.IO.File.WriteAllText(@"D:\wangran\密钥.xml", rsp.ToXmlString(true));
            //byte[] 公钥 = rsp.ExportCspBlob(false);
            byte[] 密钥 = rsp.ExportCspBlob(true);//负责解密
            //System.IO.File.WriteAllText(@"D:\wangran\公钥.txt", Convert.ToBase64String(公钥));
            System.IO.File.WriteAllText(@"D:\wangran\密钥.txt", Convert.ToBase64String(密钥));
            data = System.Text.UTF8Encoding.UTF8.GetBytes(s);
            byte[] data_1 = rsp.SignData(data, "md5");

            byte[] data1 = rsp.Encrypt(data, false);

            Console.WriteLine(
            System.Text.UTF8Encoding.UTF8.GetString(data));

            Console.WriteLine(
            System.Text.UTF8Encoding.UTF8.GetString(data_1));


            rsp = new RSACryptoServiceProvider();
            //rsp.ImportCspBlob(公钥);
            rsp.FromXmlString(System.IO.File.ReadAllText(@"D:\wangran\公钥.xml"));
            //公钥只签名与加密,不解密
            byte[] data2 = rsp.Encrypt(data, false);
            Console.WriteLine(Convert.ToBase64String(data1));
            Console.WriteLine(Convert.ToBase64String(data2));

            //验签
            Console.WriteLine(rsp.VerifyData(data, "md5", data_1));

            rsp = new RSACryptoServiceProvider();
            //rsp.ImportCspBlob(公钥);
            rsp.FromXmlString(System.IO.File.ReadAllText(@"D:\wangran\公钥.xml"));
            data2 = rsp.Encrypt(data, false);
            Console.WriteLine(Convert.ToBase64String(data1));
            Console.WriteLine(Convert.ToBase64String(data2));

            //验签
            Console.WriteLine(rsp.VerifyData(data, "md5", data_1));
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值