I then open the file and try to decrypt it, and get the error "Padding is invalid and cannot be removed".
I think this is because during encryption, I execute
[code]key = New RijndaelManaged()[/code]
And, during decryption, I also execute
[code]key = New RijndaelManaged()[/code]
How can I save the key from the encryption ?Thank you.
This is the code to encrypt and save the XML file:
[code]Imports System.Xml
Imports System.Security.Cryptography
Imports System.Security.Cryptography.Xml
Dim key As RijndaelManaged = Nothing
' Create a new Rijndael key.
key = New RijndaelManaged()
' Load an XML document.
Dim xmlDoc As New XmlDocument()
xmlDoc.PreserveWhitespace = True
xmlDoc.Load("myXMLFile.XML")
Encrypt(xmlDoc, "myElement1", key)
Encrypt(xmlDoc, "myElement2", key)
xmlDoc.Save("myXMLFile.XML")[/code]
This is the code to decrypt the XML file:
[code]Imports System.Xml
Imports System.Security.Cryptography
Imports System.Security.Cryptography.Xml
Dim key As RijndaelManaged = Nothing
key = New RijndaelManaged()
Dim xmlDoc As New XmlDocument()
xmlDoc.PreserveWhitespace = True
xmlDoc.Load("myXMLFile.XML")
Decrypt(xmlDoc, key)[/code]