zoukankan      html  css  js  c++  java
  • C# RSA加密

    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("加密数据: "+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+" " + bs);
                //导入私钥解密
                RSA.ImportCspBlob(Convert.FromBase64String(keyValuePair.Value));
                byte[] d = RSA.Decrypt(b, false);
                Console.WriteLine("Decrypt解密后数据: " +Encoding.UTF8.GetString(d));
            }
            /// <summary>
            /// 获取一对 公钥 私钥方法
            /// </summary>
            /// <returns></returns>
            public static KeyValuePair<string, string> GetKeyPair1()
            {
                string public_Key = Convert.ToBase64String(RSA.ExportCspBlob(false));
                string private_Key = Convert.ToBase64String(RSA.ExportCspBlob(true));
                return new KeyValuePair<string, string>(public_Key, private_Key);
            }
        }
    }

  • 相关阅读:
    在win2003中发布部署vs2010b2写的mvc2网站
    安装blender2.5Alpha0
    Win7下虚拟机个人使用小结:Virtual PC,VMware和VirtualBox
    ASP.NET AJAX Control Toolkit Beta 0911 发布[再增两控件]
    Camtasia 6录屏时鼠标闪烁问题解决
    为XNA制做安装程序(四)WIX Toolset 3.0 for Visual Studio 2008
    Oracle EM 12c
    无题
    从徐汇到虹口
    近况
  • 原文地址:https://www.cnblogs.com/fireblog/p/12198955.html
Copyright © 2011-2022 走看看