C# RSA加密

2021-01-17 12:13

阅读:498

标签:gui   returns   oba   mba   解密   public   air   ide   service   

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static public RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
        static void Main(string[] args)
        {
            string cc = Guid.NewGuid().ToString();
            Console.WriteLine("加密数据:\n"+cc);
            var keyValuePair = GetKeyPair1();
            //Console.WriteLine(keyValuePair.Value);//打印私钥
            //导入公钥 加密
            RSA.ImportCspBlob(Convert.FromBase64String(keyValuePair.Key));
            byte[] b= RSA.Encrypt(Encoding.UTF8.GetBytes(cc), false);
            string bs = Convert.ToBase64String(b);
            Console.WriteLine("Encrypt加密后数据:"+bs.Length+"\n" + bs);
            //导入私钥解密
            RSA.ImportCspBlob(Convert.FromBase64String(keyValuePair.Value));
            byte[] d = RSA.Decrypt(b, false);
            Console.WriteLine("Decrypt解密后数据:\n" +Encoding.UTF8.GetString(d));
        }
        ///


        /// 获取一对 公钥 私钥方法
        ///

        ///
        public static KeyValuePair GetKeyPair1()
        {
            string public_Key = Convert.ToBase64String(RSA.ExportCspBlob(false));
            string private_Key = Convert.ToBase64String(RSA.ExportCspBlob(true));
            return new KeyValuePair(public_Key, private_Key);
        }
    }
}

C# RSA加密

标签:gui   returns   oba   mba   解密   public   air   ide   service   

原文地址:https://www.cnblogs.com/fireblog/p/12198955.html


评论


亲,登录后才可以留言!