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

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

问题描述

这是后续的此SO文章,我学会了生成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文档,并尝试将其转换为Encrypt函数的字节[]:

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天全站免登陆