将RSA密钥转换为可读或二进制格式

时间:2014-06-15 08:30:31

标签: rsa

我一直在努力将rsa密钥转换为使用ASP.NET的二进制或可读格式 我经历了很多网站来解决问题 我想要的关键格式如下:

            -----BEGIN PRIVATE KEY-----
            BASE64 ENCODED DATA
            -----END PRIVATE KEY-----

我使用下面的代码使用RSA算法创建密钥。使用以下代码生成的密钥是xml格式,但我希望以可读格式或二进制格式。 请帮帮我。

使用的代码是。

default.aspx.cs

                using System;
                using System.Collections.Generic;
                using System.Web;
                using System.Web.UI;
                using System.Web.UI.WebControls;
                public partial class _Default:System.Web.UI.Page 
                {
                    protected void Page_Load(object sender, EventArgs e)
                    {

                    }
                    protected void Button1_Click(object sender, EventArgs e)
                    {
                        Cryptography.AssignNewKey();
                    }
                    protected void Button2_Click(object sender, EventArgs e)
                    {
                        txt2.Text = Cryptography.EncryptData(txt1.Text);
                    }
                    protected void Button3_Click(object sender, EventArgs e)
                    {
                        txt3.Text = Cryptography.DecryptData(txt2.Text);
                    }
                }


                Class: Cryptography.cs

                using System;
                using System.Collections.Generic;
                using System.Linq;
                using System.Web;
                using System.IO;
                using System.Security.Cryptography;
                /// <summary>
                /// Summary description for Cryptography
                /// </summary>

                public class Cryptography
                {
                    public Cryptography()
                    {
                        //
                        // TODO: Add constructor logic here
                        //
                    }

                    public static RSACryptoServiceProvider rsa;

                    public static void AssignParameter()
                    {
                        const int PROVIDER_RSA_FULL = 1;
                        const string CONTAINER_NAME = "SpiderContainer";
                        CspParameters cspParams;
                        cspParams = new CspParameters(PROVIDER_RSA_FULL);
                        cspParams.KeyContainerName = CONTAINER_NAME;
                        cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
                        cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";
                        rsa = new RSACryptoServiceProvider(cspParams);
                    }


                    public static string EncryptData(string data2Encrypt)
                    {
                        AssignParameter();
                        //StreamReader reader = new StreamReader(@"C:\Inetpub\wwwroot\dotnetspiderencryption\publickey.xml");
                        StreamReader reader = new StreamReader("C:/keyscsharp/publickey.xml");
                        string publicOnlyKeyXML = reader.ReadToEnd();
                        rsa.FromXmlString(publicOnlyKeyXML);
                        reader.Close();

                        //read plaintext, encrypt it to ciphertext

                        byte[] plainbytes = System.Text.Encoding.UTF8.GetBytes(data2Encrypt);
                        byte[] cipherbytes = rsa.Encrypt(plainbytes, false);
                        return Convert.ToBase64String(cipherbytes);
                    }
                    public static void AssignNewKey()
                    {
                        AssignParameter();

                        //provide public and private RSA params
                        //StreamWriter writer = new StreamWriter(@"C:\Inetpub\wwwroot\dotnetspiderencryption\privatekey.xml");
                        StreamWriter writer = new StreamWriter("C:/keyscsharp/privatekey.xml");





                        string publicPrivateKeyXML = rsa.ToXmlString(true);
                        writer.Write(publicPrivateKeyXML);
                        writer.Close();


                        //provide public only RSA params
                        //writer = new StreamWriter(@"C:\Inetpub\wwwroot\dotnetspiderencryption\publickey.xml");
                        writer = new StreamWriter("C:/keyscsharp/publickey.xml");
                        string publicOnlyKeyXML = rsa.ToXmlString(false);
                        writer.Write(publicOnlyKeyXML);
                        writer.Close();

                    }




                    public static string DecryptData(string data2Decrypt)
                    {
                        AssignParameter();

                        byte[] getpassword = Convert.FromBase64String(data2Decrypt);
                        //StreamReader reader = new StreamReader(@"C:\Inetpub\wwwroot\dotnetspiderencryption\privatekey.xml");
                        StreamReader reader = new StreamReader("C:/keyscsharp/privatekey.xml");
                        string publicPrivateKeyXML = reader.ReadToEnd();
                        rsa.FromXmlString(publicPrivateKeyXML);
                        reader.Close();

                        //read ciphertext, decrypt it to plaintext
                        byte[] plain = rsa.Decrypt(getpassword, false);
                        return System.Text.Encoding.UTF8.GetString(plain);

                    }
                }

请帮帮我。

0 个答案:

没有答案