首页 > 其他 > 详细

RSA

时间:2020-05-21 18:45:57      阅读:50      评论:0      收藏:0      [点我收藏+]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    public class Rsa
    {
        /// <summary>
        ///  生成key  方法1 
        /// </summary>
        /// <returns></returns>
        public static (string publickey, string privateKey) GetBase64keys()
        {
            RSACryptoServiceProvider rSA = new RSACryptoServiceProvider();
            string publicKey = Convert.ToBase64String(rSA.ExportCspBlob(false));
            string privateKey = Convert.ToBase64String(rSA.ExportCspBlob(false));
            return (publickey: publicKey, privateKey: privateKey);
        }
        /// <summary>
        ///   生成key 方法2
        /// </summary>
        /// <returns></returns>
        public static (string publickey, string privateKey) GetToXmlkeys()
        {
            RSACryptoServiceProvider rSA = new RSACryptoServiceProvider();
            string publicKey = rSA.ToXmlString(false);
            string privateKey = rSA.ToXmlString(true);
            return (publickey: publicKey, privateKey: privateKey);
        }
        public static string EncryptByBase64(string privateKey, string text)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.ImportCspBlob(Convert.FromBase64String(privateKey));
            var bytes = Encoding.UTF8.GetBytes(text);
            var result = rsa.Encrypt(bytes, false);
            return Encoding.UTF8.GetString(result);
        }
        public static string DecryptByBase64(string publicKey, string text)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.ImportCspBlob(Convert.FromBase64String(publicKey));
            var bytes = Encoding.UTF8.GetBytes(text);
            var result = rsa.Decrypt(bytes, false);
            return Encoding.UTF8.GetString(result);
        }
    }
}

 

RSA

原文:https://www.cnblogs.com/hnzheng/p/12932263.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!