2

I am getting the runtime error {"Bad Length.\r\n"} on the line:

return rsa.Encrypt(bytes, true);

This is in the function:

 private static byte[] Encrypt(byte[] bytes)
        {
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
            {
                string test = Properties.Settings.Default.PublicKeyXml;

                rsa.FromXmlString("<RSAKeyValue><Modulus>mfXS3Na0XfkjhpjS3sL5XcC9o+j6KXi1LB9yBc4SsTMo1Yk/pFsXr74gNj4aRxKB45+hZH/lSo933NCDEh25du1iMsaH4TGQNkCqi+HDLQjOrdXMMNmaQrLXGlY7UCCfFUnkEUxX51AlyVLzqLycaAt6zm5ljnDXojMC7JoCrTM=</Modulus><Exponent>AQAB</Exponent></RSAKeyFile>");
                return rsa.Encrypt(bytes, true);
            }
        }

I am using a key size of 8192:

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

The XML file is small. According to the length at runtime, it is only 225 bytes:

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);
                int fileBytesLength = fileBytes.Length;



  Encrypt(fileBytes);

According to this SO post, a key size of 4096 bytes should have been sufficient:

((KeySize - 384) / 8) + 7

What key size do I have to use? Why doesn't 8096 bytes work? How can I get this to work?

Community
  • 1
  • 1
user2471435
  • 1,644
  • 7
  • 35
  • 62
  • 6
    I would strongly recommend not encrypting your XML directly with an RSA key. It doesn't scale, and is far slower than symmetric encryption. A better approach would be to encrypt your XML with a symmetric algorithm, like AES, then encrypt the AES symmetric key with your RSA key. – vcsjones Apr 07 '14 at 17:13
  • Could you please show the code to do this in an answer? – user2471435 Apr 07 '14 at 17:19
  • @vcsjones is right, don't rely on RSA to do your encryption for an arbitrary amount of data. Use it to safely exchange a symmetric key which can be a known length. – David Zech Apr 07 '14 at 17:20
  • 2
    Is the key contained in your first code snippet the one you're actually using? If so, it's only 1024 bit, not 8192. – Iridium Apr 07 '14 at 21:28
  • @Iridium I knew I missed something, well spotted. – Maarten Bodewes Apr 13 '14 at 20:52
  • 1
    You are doing it wrong. RSA is for key transport (encrypting other keys), not data encryption. – erickson Apr 15 '14 at 22:51

1 Answers1

-1

This is the same error I found when I tried the code of one or your other post.

Change this

byte[] fileBytes = Encoding.Default.GetBytes(xmlDoc.OuterXml);

to this

byte[] fileBytes = Encoding.Default.GetBytes(xmlDoc.ToString());

I would also suggest that you change your encoding from default to Encoding.ASCII or something more defined. It will be easier to convert it to a string to save and then back to a byte array to decrypt.

TDSapp
  • 9
  • 2