c#实现RSA公钥加密私钥解密代码

代码语言:c#

所属分类:加密解密

代码描述:c#实现RSA公钥加密私钥解密代码

代码标签: 公钥 加密 私钥 解密

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;

namespace HelloWorldApplication
{

    public class HelloWorld

    {

        private static string publicKey =

        "<RSAKeyValue><Modulus>6CdsXgYOyya/yQH"+

        "TO96dB3gEurM2UQDDVGrZoe6RcAVTxAqDDf5L"+

        "wPycZwtNOx3Cfy44/D5Mj86koPew5soFIz9sx"+

        "PAHRF5hcqJoG+q+UfUYTHYCsMH2cnqGVtnQiE"+

        "/PMRMmY0RwEfMIo+TDpq3QyO03MaEsDGf13sP"+

        "w9YRXiac=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";

        private static string privateKey =

        "<RSAKeyValue><Modulus>6CdsXgYOyya/yQH"+

        "TO96dB3gEurM2UQDDVGrZoe6RcAVTxAqDDf5L"+

        "wPycZwtNOx3Cfy44/D5Mj86koPew5soFIz9sx"+

        "PAHRF5hcqJoG+q+UfUYTHYCsMH2cnqGVtnQiE"+

        "/PMRMmY0RwEfMIo+TDpq3QyO03MaEsDGf13sP"+

        "w9YRXiac=</Modulus><Exponent>AQAB</Exponent>"+

        "<P>/aoce2r6tonjzt1IQI6FM4ysR40j/gKvt4d"+

        "L411pUop1Zg61KvCm990M4uN6K8R/DUvAQdrRd"+

        "VgzvvAxXD7ESw==</P><Q>6kqclrEunX/fmOle"+

        "VTxG4oEpXY4IJumXkLpylNR3vhlXf6ZF9obEpG"+

        "lq0N7sX2HBxa7T2a0WznOAb0si8FuelQ==</Q>"+

        "<DP>3XEvxB40GD5v/Rr4BENmzQW1MBFqpki6FU"+

        "GrYiUd2My+iAW26nGDkUYMBdYHxUWYlIbYo6Te"+

        "zc3d/oW40YqJ2Q==</DP><DQ>LK0XmQCmY/ArY"+

        "gw2Kci5t51rluRrl4f5l+aFzO2K+9v3PGcndjA"+

        "StUtIzBWGO1X3zktdKGgCLlIGDrLkMbM21Q==</DQ><InverseQ>"+

        "GqC4Wwsk2fdvJ9dmgYlej8mTDBWg0Wm6aqb5kjn"+

        "cWK6WUa6CfD+XxfewIIq26+4Etm2A8IAtRdwPl4"+

        "aPjSfWdA==</InverseQ><D>a1qfsDMY8DSxB2D"+

        "Cr7LX5rZHaZaqDXdO3GC01z8dHjI4dDVwOS5ZFZ"+

        "s7MCN3yViPsoRLccnVWcLzOkSQF4lgKfTq3IH40"+

        "H5N4gg41as9GbD0g9FC3n5IT4VlVxn9ZdW+WQry"+

        "oHdbiIAiNpFKxL/DIEERur4sE1Jt9VdZsH24CJE=</D></RSAKeyValue>";

        static void Main(string[] args) {
            Console.OutputEncoding = System.Text.Encoding.Unicode;
            Console.WriteLine("公钥加密");
            string encodeString = Encrypt("1234567");
            Console.WriteLine(encodeString);

            Console.WriteLine("私钥解密");
            string decode = Decrypt(encodeString);
            Console.WriteLine(decode);

        }

        public static string Decrypt(string base64code) {

            try

            {



                //Create a UnicodeEncoder to convert between byte array and string.

                UnicodeEncoding ByteConverter = new UnicodeEncoding();



                //Create a new instance of RSACryptoServiceProvider to generate

                //public and private key data.

                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

                RSA.FromXmlString(privateKey);



                byte[] encryptedData;

                byte[] decryptedData;

                encryptedData = Convert.FromBase64String(base64code);



                //Pass the data to DECRYPT, the private key information

                //(using RSACryptoServiceProvider.ExportParameters(true),

                //and a boolean flag specifying no OAEP padding.

                decryptedData = RSADecrypt(

                    encryptedData, RSA.ExportParameters(true), false);



                //Display the decrypted plaintext to the console.

                return ByteConverter.GetString(decryptedData);

            }

            catch (Exception exc) {

               .........完整代码请登录后点击上方下载按钮下载查看

网友评论0

相似代码