I’ve been away from coding for a little while, however I needed to make a start on a new tool.
It’s dog simple, but on the off chance it would be useful to others I’m posting it up. Basically its a simple implementation of the built in compression and encryption routines in .net.
A word of warning – if the information is very confidential and high risk, storing your key in the code like I’ve done is a pretty bad idea. 30 seconds with .net reflector or Telerik will reveal it. I’ll amend this post with a more secure version once I’ve worked this out
.
First I encrypt then I compress. However, the sharp eyed will notice that compression of an encrypted file doesn’t reduce file size – a fact I discovered a few moments ago.
Module StorageTest
Property Content As New Content 'an arbitrary class with members like a listOfItems As List(Of ItemClass)
Private nl = ControlChars.NewLine
Private Encoded As String = "C:\TestFile\encoded.txt"
Private Decoded As String = "C:\TestFile\decoded.txt"
Private CompressEncoded As String = "C:\TestFile\encoded.zip"
'Inexcusable hard coded key for higher risk data!
ReadOnly TripleDES As New TripleDES(New Byte() {13, 76, 12, 24, 1, 19, 183, 122, 19, 154, 92, 125, 76, 98, 44, 33, 84, 7, 220, 13, 122, 89, 198, 136})
Sub StoreList()
Dim tempString = ""
'Home brew very quick xml hack. Next version to use proper nested xml
Dim tagBuild = Function(i, v) i & nl & v & nl & i.replace("<", "</") & nl
'Discovered I can initialise and LINQuery a list at the point of doing a For Each :)
For Each m In From e In Content.ListOfItems From m1 In {tagBuild("<ID>", e.ID),
tagBuild("<catagory>", e.Catagory),
tagBuild("<Name>", e.Name),
tagBuild("<Order>", e.Order)}
Select m1
tempString += m
Next
TripleDES.Encrypt(tempString, Encoded)
Compress(Encoded, CompressEncoded)
Decompress(CompressEncoded, Encoded)
TripleDES.Decrypt(Encoded, Decoded)
streamWriter.Close()
End Sub
Sub Compress(filename As String, zippedFile As String)
If IO.File.Exists(zippedFile) Then IO.File.Delete(zippedFile)
If IO.File.Exists(filename) Then
Using archive As ZipArchive = Open(zippedFile, ZipArchiveMode.Create)
archive.CreateEntryFromFile(filename, Path.GetFileName(filename), CompressionLevel.Fastest)
End Using
End If
End Sub
Sub Decompress(ZippedFile As String, UnzippedFile As String)
If IO.File.Exists(UnzippedFile) Then IO.File.Delete(UnzippedFile)
If IO.File.Exists(ZippedFile) Then
Using archive As ZipArchive = Open(ZippedFile, ZipArchiveMode.Read)
archive.ExtractToDirectory(Path.GetDirectoryName(UnzippedFile))
End Using
End If
End Sub
End Module
Here is the encryption class:
Imports System.Security.Cryptography
Imports System.IO
Public Class TripleDES
Protected Iv As Byte()
Protected Key As Byte()
Public Sub New(ByVal Key As Byte())
MyBase.New()
Dim numArray As Byte() = New Byte() {13, 35, 4, 152, 14, 44, 2, 12}
Iv = numArray
Me.Key = Key
End Sub
Friend Sub Decrypt(Source As String, FileName As String)
Dim DecodedWriter As New StreamWriter(FileName)
Using tripleDESCryptoServiceProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
Using cryptoTransform As ICryptoTransform = tripleDESCryptoServiceProvider.CreateDecryptor(Me.Key, Me.Iv)
Using fileStream As FileStream = File.Open(Source, FileMode.OpenOrCreate)
Using cryptoStream As CryptoStream = New CryptoStream(fileStream, cryptoTransform, CryptoStreamMode.Read)
Using srDecrypt As New StreamReader(cryptoStream)
DecodedWriter.WriteLine(srDecrypt.ReadToEnd())
End Using
End Using
End Using
End Using
End Using
End Sub
Friend Sub Encrypt(ByVal Data As String, filename As String)
Using tripleDesCryptoServiceProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
Using cryptoTransform As ICryptoTransform = tripleDesCryptoServiceProvider.CreateEncryptor(Me.Key, Me.Iv)
Using fileStream As FileStream = File.Open(filename, FileMode.OpenOrCreate)
Using cryptoStream As CryptoStream = New CryptoStream(fileStream, cryptoTransform, CryptoStreamMode.Write)
Using srDecrypt As New StreamWriter(cryptoStream)
srDecrypt.WriteLine(Data)
End Using
End Using
End Using
End Using
End Using
End Sub
End Class