错误RSA加密Xml文件转换为byte [] [英] Error with RSA Encrypting Xml file converted into byte[]

查看:239
本文介绍了错误RSA加密Xml文件转换为byte []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是这个SO post ,其中我学会了生成RSA密钥对,并在设置中存储公钥。我通过以下方式生成了我的密钥:

This is a follow-on to this SO post in which I learned to generate the RSA key pair and store the Public key in the Settings. I generated my key by:

 CspParameters cspParams = new CspParameters();
                cspParams.KeyContainerName = "XML_ENC_RSA_KEY";
                RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(cspParams);
                string keyXml = rsaKey.ToXmlString(true);

我将该字符串的公钥部分复制到我的程序设置中,它看起来像:

I copied the public key part of that string into my program settings and it looks like:

"<RSAKeyValue><Modulus>mfXS3Na0XfkjhpjS3sL5XcC9o+j6KXi1LB9yBc4SsTMo1Yk/pFsXr74gNj4aRxKB45+hZH/lSo933NCDEh25du1iMsaH4TGQNkCqi+HDLQjOrdXMMNmaQrLXGlY7UCCfFUnkEUxX51AlyVLzqLycaAt6zm5ljnDXojMC7JoCrTM=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"

这看起来有效吗?

我正在获取我的XML文档,并尝试将其转换为加密功能的byte []:

Then I am taking my XML document and trying to convert it to a byte[] for the Encrypt function:

string fileName = System.IO.Path.Combine(Application.StartupPath, "alphaService.xml");
                XDocument doc = new XDocument();
                XElement xml = new XElement("Info",
                    new XElement("DatabaseServerName", txtServerName.Text),
                    new XElement("DatabaseUserName", txtDatabaseUserName.Text),
                    new XElement("DatabasePassword", txtDatabasePassword.Text),
                    new XElement("ServiceAccount", txtAccount.Text),
                    new XElement("ServicePassword", txtServicePassword.Text),
                    new XElement("RegistrationCode", txtRegistrationCode.Text));

                doc.Add(xml);
                doc.Save(fileName);

                // Convert XML doc to byte stream
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(fileName);
                byte[] fileBytes = Encoding.Default.GetBytes(xmlDoc.OuterXml);

                Encrypt(fileBytes);

我从Encrypt函数中得到一个Syntax Error line1,它是:

I am getting a "Syntax Error line1" from the Encrypt function which is:

 private static byte[] Encrypt(byte[] bytes)
        {
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
            {
                rsa.FromXmlString(Properties.Settings.Default.PublicKeyXml);
                return rsa.Encrypt(bytes, true);
            }
        }

任何想法?编辑:实际的错误是:

Any ideas? The actual error is:

 rsa.FromXmlString(Properties.Settings.Default.PublicKeyXml);


推荐答案

我只是在这里查找您的信息上的RSACryptoServiceProvider。我试过你的代码,它为我工作,好吧,我从来没有得到你的错误,直到我再次阅读你的消息。

I was just looking at your post here looking for information on the RSACryptoServiceProvider. I tried your code and it worked for me, Well, sort of, I never got the error you did until I started reading your message again.

在Properties.Settings中删除您的公钥的引号。当我看到你发布了你的公共密钥,我进去,并添加了我的字符串的报价,我得到完全相同的错误。

Remove the quotes from your public key in the Properties.Settings. When I saw what you had posted for your public key I went in and added the quotes to my string and I get the exact same error you did.

我在哪里得到一个错误,但不同于你的加密上得到一个错误的长度错误。但是,我想出,如果我改变的行转换XmL到一个字节到.ToString()而不是.OuterXML它的工作原理。

Where I did get an error but different from yours was getting a bad length error on the encrypt. However, I figured out that if I change the line for converting the XmL to a byte to .ToString() and not .OuterXML it works.

    private void button4_Click(object sender, EventArgs e)
    {
        string fileName = System.IO.Path.Combine(Application.StartupPath, "alphaService.xml");

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(fileName);
        byte[] fileBytes = Encoding.ASCII.GetBytes(xmlDoc.ToString());

        byte[] EncryptedBytes = Encrypt(fileBytes);
        string EncryptedString = Encoding.ASCII.GetString(EncryptedBytes);
    }

    private static byte[] Encrypt(byte[] bytes)
    {
        using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
        {
            rsa.FromXmlString(Properties.Settings.Default.PublicKeyXml);
            return rsa.Encrypt(bytes, false);
        }
    }

我更改并编码为ASCII,字节数组转换为字符串,如果使用相同的方法转换为字节数组,最好这样做。

I changed and encoding to ASCII so I could convert the byte array to a string and it's better to do that if you convert to the byte array using the same method.

这篇关于错误RSA加密Xml文件转换为byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆