1

I am serializing an object to an XML string using the .net XML serializer. That object contains a property of type string, whose content is an encrypyted string. The encryption is done using the Rijndael algorithm also provided by the .net, and the call looks like this:

var encryptedArr = EncryptStringToBytes(plainText, RijndaelKey, RijndaelIv);
return Encoding.Default.GetString(encryptedArr);

Although serialization goes fine, the problem is when trying to deserialize. the serializer throws an exception saying

"There is an error in XML document (1,1130). ' ', hexadecimal value 0x02, is an invalid character. Line..."

The thing is that these characters are to my understanding results of the encryption process so I guess messing with the encrypted string to make it XML-compatible is not an option. I also tried encoding the output string in the above piece of code differently: UTF-8, Base64(which throws an exception saying the string is base64-incompatible) etc.

I've been looking into it for quite some time now. What do you recommend?

user181218
  • 1,655
  • 5
  • 28
  • 42
  • Take a look at this SO post about serializing an object containing invalid chars http://stackoverflow.com/questions/1165966/xml-serialization-of-an-object-containing-invalid-chars Maybe some code regarding your serialization and deserialization functions would help... so we can see the classes and techniques you are using. There is no point giving you methods or properties to set if we don't know the classes you are utilizing. – Mikanikal Jul 10 '14 at 02:28

1 Answers1

0

Have you taken a look at the example at the bottom of the RijndaelManaged class on MSDN?

Just wondering as they have a method, with the same name as the code you posted. If you are or arent encrypting via similar means, you could try returning a string instead of a byte array, from your method, by calling MemoryStream.GetString() and returning that value:

    static string EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
    {
        //...
        string cipherText = null;
        // Create an RijndaelManaged object 
        // with the specified key and IV. 
        using (RijndaelManaged rijAlg = new RijndaelManaged())
        {
            rijAlg.Key = Key;
            rijAlg.IV = IV;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

            // Create the streams used for encryption. 
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {

                        //Write all data to the stream.
                        swEncrypt.Write(plainText);
                    }
                    cipherText = msEncrypt.ToString();
                }
            }
        }


        // Return the encrypted bytes from the memory stream. 
        return cipherText;

    }

What happens if your plainText goes though that? Maybe more information is needed about the plaintext. Might be the case of: Old Post

Community
  • 1
  • 1
  • I might be missing something, but I can't see the connection to my question. Can you explain? I'm using the RijndaelManaged btw. – user181218 Jul 10 '14 at 05:20
  • @user181218 This has only one important part.. `cipherText = msEncrypt.ToString();` if we convert the encrypted text to string that has document encoding. Else the XML parser will find many characters that are not usual(match with the original document encoding) after encoding applied. – MarmiK Jul 10 '14 at 06:00