Note that there are some explanatory texts on larger screens.

plurals
  1. POXML Encryption/Decryption in Separate Files
    text
    copied!<p>I need to encrypt an XML file, send it to another location and decrypt it there. As you can see from the codes below, I am using the same key but it won't work (for now im simply using two local files).</p> <p>The error I get is as follows:</p> <blockquote> <p>Padding is invalid and cannot be removed.</p> </blockquote> <p>on this line in Decrypt.aspx: <em>Dim xReader As XmlTextReader</em></p> <p>I am thinking it might be at the actual encryption stage causing errors e.g., file not saving correctly.</p> <p><strong>Encrypt.aspx</strong></p> <pre><code> Dim rijnAlg As RijndaelManaged rijnAlg = RijndaelManaged.Create() rijnAlg.Key = {118, 123, 23, 17, 161, 152, 35, 68, 126, 213, 16, 115, 68, 217, 58, 108, 56, 218, 5, 78, 28, 128, 113, 208, 61, 56, 10, 87, 187, 162, 233, 38} rijnAlg.IV = {33, 241, 14, 16, 103, 18, 14, 248, 4, 54, 18, 5, 60, 76, 16, 191} Dim encryptor As ICryptoTransform encryptor = rijnAlg.CreateEncryptor(rijnAlg.Key, rijnAlg.IV) Dim wStream As FileStream wStream = File.Open("C:\test.xml", FileMode.Create) Dim cStream As CryptoStream cStream = New CryptoStream(wStream, encryptor, CryptoStreamMode.Write) Dim sWriter As StreamWriter sWriter = New StreamWriter(cStream) XMLDoc.Save(sWriter) 'Clear memory' wStream.Flush() wStream.Close() </code></pre> <p><strong>Decrypt.aspx</strong></p> <pre><code> Dim rijnAlg As RijndaelManaged rijnAlg = RijndaelManaged.Create() rijnAlg.Key = {118, 123, 23, 17, 161, 152, 35, 68, 126, 213, 16, 115, 68, 217, 58, 108, 56, 218, 5, 78, 28, 128, 113, 208, 61, 56, 10, 87, 187, 162, 233, 38} rijnAlg.IV = {33, 241, 14, 16, 103, 18, 14, 248, 4, 54, 18, 5, 60, 76, 16, 191} Dim decryptor As ICryptoTransform decryptor = rijnAlg.CreateDecryptor(rijnAlg.Key, rijnAlg.IV) Response.Write(rijnAlg.Key) Response.Write(rijnAlg.IV) Dim rStream As FileStream rStream = File.OpenRead("C:\test.xml") Dim cStream As CryptoStream cStream = New CryptoStream(rStream, decryptor, CryptoStreamMode.Read) Dim xReader As XmlTextReader xReader = New XmlTextReader(cStream) Dim xDoc As XDocument xDoc = XDocument.Load(xReader) xDoc.Save("C:\test.xml") </code></pre> <p>And for the sake of possible interest, here is the XML creation code I am using:</p> <pre><code>Dim XMLDoc As XDocument XMLDoc = New XDocument( New XDeclaration("1.0", "utf-8", "yes"), New XElement("user", New XElement("details", New XElement("firstname", Firstname.Text), New XElement("surname", Lastname.Text) ) ) ) XMLDoc.Save("C:\test.xml") </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload