zoukankan      html  css  js  c++  java
  • RSA

    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);
            }
        }
    }
  • 相关阅读:
    关于GET和POST请求的区别,最通俗全面的回答
    Mac常用命令行
    Jquery中的done() fail() then() $when()到底是什么
    聊聊HTML5中的Web Notification桌面通知
    css实现左右两个div等高
    css样式优先级计算规则
    vue获取后端数据放在created还是mounted方法里面?
    vue的provide和inject特性
    前端路由的实现原理
    Windows鼠标右键新建中增加新建md文件
  • 原文地址:https://www.cnblogs.com/hnzheng/p/12932263.html
Copyright © 2011-2022 走看看