Click here to Skip to main content
15,881,044 members
Articles / Programming Languages / Visual Basic

Encrypting a dataset using AES, including compression

Rate me:
Please Sign up or sign in to vote.
4.98/5 (49 votes)
21 Aug 2015CPOL7 min read 123.7K   4.8K   127  
Article describes how to encrypt a dataset using AES. Optionally the dataset is compressed before the encryption.
''' <summary>
''' This class is responsible for data related operations
''' </summary>
Friend Module DataHandler

   ''' <summary>
   ''' The dataset for the application
   ''' </summary>
   Friend Property TheDataSet As EncryptDataSetVB.MyDataSet

   ''' <summary>
   ''' Name of the file where the dataset is saved
   ''' </summary>
   Friend Property FileName As String

   ''' <summary>
   ''' Username for encryption
   ''' </summary>
   Friend Property UserName As String

   ''' <summary>
   ''' Password for encryption
   ''' </summary>
   Friend Property Password As String

   ''' <summary>
   ''' Will the data be compressed before encrypting
   ''' </summary>
   Friend Property Compress As Boolean

   '' <summary>
   '' Returns the application title from assembly information
   '' </summary>
   Friend ReadOnly Property AppTitle As String
      Get
         Dim titleAttribute As System.Reflection.AssemblyTitleAttribute

         titleAttribute = System.Reflection.Assembly.GetExecutingAssembly().GetCustomAttributes(
            GetType(System.Reflection.AssemblyTitleAttribute),
            False)(0)

         Return titleAttribute.Title
      End Get
   End Property

   ''' <summary>
   ''' Creates a new dataset. The dataset is created in the specified file and encrypted using specified credentials. 
   ''' The dataset created can be accessed vie TheDataSet property
   ''' </summary>
   ''' <param name="fileName">Filename for the dataset</param>
   ''' <param name="userName">User name for encription</param>
   ''' <param name="password">Password for encryption</param>
   ''' <param name="compress">Compress the dataset when saving it</param>
   ''' <returns>True if successfull</returns>
   Friend Function CreateDataSet(fileName As String, userName As String, password As String, compress As Boolean) As Boolean
      Dim myDataSet As EncryptDataSetVB.MyDataSet = New EncryptDataSetVB.MyDataSet()

      Try
         ' Try to encrypt and decrypt the dataset with information supplied
         myDataSet.WriteXml(fileName, userName, password, compress)
         myDataSet.ReadXml(fileName, userName, password, compress)
      Catch exception As System.Exception
         System.Windows.MessageBox.Show(
            exception.Message,
            DataHandler.AppTitle,
            System.Windows.MessageBoxButton.OK,
            System.Windows.MessageBoxImage.Stop)
         Return False
      End Try

      ' Everything worked so set current information
      DataHandler.FileName = fileName
      DataHandler.UserName = userName
      DataHandler.Password = password
      DataHandler.Compress = compress
      DataHandler.TheDataSet = myDataSet
      DataHandler.TheDataSet.AcceptChanges()

      Return True
   End Function

   ''' <summary>
   ''' Opens an existing dataset with the given credentials
   ''' </summary>
   ''' <param name="fileName">File name containing the dataset</param>
   ''' <param name="userName">User name for decryption</param>
   ''' <param name="password">Password for decryption</param>
   ''' <param name="compressed">Is the file compressed</param>
   ''' <returns>True if succesful</returns>
   Friend Function OpenDataSet(fileName As String, userName As String, password As String, compressed As Boolean) As Boolean
      Dim myDataSet As EncryptDataSetVB.MyDataSet = New EncryptDataSetVB.MyDataSet()

      Try
         ' Open the dataset with given information
         myDataSet.ReadXml(fileName, userName, password, compressed)
      Catch cryptographicException As System.Security.Cryptography.CryptographicException
         System.Windows.MessageBox.Show(
            "Invalid user name or password.",
            DataHandler.AppTitle,
            System.Windows.MessageBoxButton.OK,
            System.Windows.MessageBoxImage.Stop)
         Return False
      Catch exception As System.Exception
         System.Windows.MessageBox.Show(
            exception.Message,
            DataHandler.AppTitle,
            System.Windows.MessageBoxButton.OK,
            System.Windows.MessageBoxImage.Stop)
         Return False
      End Try

      ' Everything worked so set current information
      DataHandler.FileName = fileName
      DataHandler.UserName = userName
      DataHandler.Password = password
      DataHandler.Compress = compressed
      DataHandler.TheDataSet = myDataSet
      DataHandler.TheDataSet.AcceptChanges()

      Return True
   End Function

   ''' <summary>
   ''' Saves the current changes to the encrypted dataset
   ''' </summary>
   ''' <returns>True if successful</returns>
   Friend Function SaveChanges() As Boolean
      Try
         DataHandler.TheDataSet.AcceptChanges()
         DataHandler.TheDataSet.WriteXml(DataHandler.FileName, DataHandler.UserName, DataHandler.Password, DataHandler.Compress)
      Catch exception As System.Exception
         System.Windows.MessageBox.Show(
            exception.Message,
            DataHandler.AppTitle,
            System.Windows.MessageBoxButton.OK,
            System.Windows.MessageBoxImage.Stop)
         Return False
      End Try

      Return True
   End Function

   ''' <summary>
   ''' Cancels all the non saved changes in the dataset
   ''' </summary>
   ''' <returns>Quite often true</returns>
   Friend Function CancelChanges() As Boolean
      DataHandler.TheDataSet.RejectChanges()

      Return True
   End Function
End Module

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Europe Europe
Biography provided

Comments and Discussions