|
-
Feb 13th, 2014, 11:10 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Question on reading encrypted XML files
Okay, I've got an app that reads an XML file, using XmlTextReader, and it works fine. But when the software is released, the XML data needs to be readable ONLY through the app, which means the Xml file needs to be encrypted. This can be done with the .NET encryption engine... shouldn't be a problem.
Here's the question. I want to still use XmlTextReader to read the decrypted data, but no decrypted copy of the file should EVER exist on the disk. Here's what I'd like to do, and I want to know if it's possible... and how, if it is.
I read the encrypted file in, decrypt it into the Xml format, then use that decrypted text as a memory stream for XmlTextReader to pull from and process normally.
Is this possible? If not, is there another way to accomplish what I need to do?
-
Feb 13th, 2014, 12:25 PM
#2
Thread Starter
Hyperactive Member
Re: Question on reading encrypted XML files
Okay, I got this figured out. To produce the encrypted XML file, I used the following code:
Code:
Private Sub EncryptXmlFile(ByVal sourceFile As String, byVal destFile As String)
'This reads in the normal XML file
Dim info As FileInfo = New FileInfo(sourceFile)
Dim input As FileStream = New FileStream(sourceFile, FileMode.Open, FileAccess.Read)
Dim dta As byte() = new byte(info.Length)
input.Read(dta, 0, (int) info.Length)
input.Close()
'This sets up the output and gets the encryption ready
Dim output As FileStream = New FileStream(destFile, FileMode.OpenOrCreate, FileAccess.Write)
Dim cryptic As DESCryptoServiceProvider = New DESCryptoServiceProvider()
cryptic.Key = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH")
cryptic.IV = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH")
'Encrypt and write to new file
Dim crStream As CryptoStream = New CryptoStream(output, cryptic.CreateEncryptor(), CryptoStreamMode.Write)
crStream.Write(dta, 0, dta.Length)
crStream.Close()
output.Close()
End Sub
That code reads the original, plain-text XML file, encrypts it, and saves to a new file. Now, to read the encrypted XML file using XmlTextReader, I use the following snippet:
Code:
'Set the decryption key
Dim cryptic As DESCryptoServiceProvider = New DESCryptoServiceProvider()
cryptic.Key = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH")
cryptic.IV = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH")
'Decrypts file and directs it to an XmlTextReader
Dim input As FileStream = New FileStream(xmlFileName, FileMode.Open)
Dim crStream As CryptoStream = New CryptoStream(input, cryptic.CreateDecryptor(), CryptoStreamMode.Read)
Dim streamReader As StreamReader = New StreamReader(crStream, Encoding.ASCII)
Dim reader As XmlTextReader = New XmlTextReader(streamReader)
That code accesses the encrypted Xml file, decrypts it, and opens an XmlTextReader to be processed normally. The code seems to be working fine.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|