0

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>"

Does that look valid?

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);

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? EDIT: The actual error is:

 rsa.FromXmlString(Properties.Settings.Default.PublicKeyXml);
Community
  • 1
  • 1
user2471435
  • 1,644
  • 7
  • 35
  • 62

1 Answers1

1

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.

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.

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);
        }
    }

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.

TDSapp
  • 9
  • 2
  • @TDSapp....not sure why OP never did give you credit for your answer but it worked great for me. I hit upon your answer trying to rsa encrypt a very small xml doc yet was getting the bad length error. – GPGVM Nov 13 '14 at 05:33