I am attempting to save a file as encrypted and be able to open and decrypt is but their are some errors.

Errors:
Error 22 Value of type 'System.IO.StreamReader' cannot be converted to '1-dimensional array of Byte'. LincenseInfo.vb 90 30
at line: mem = crypto.Decrypt(sr), specifically the sr part is causing the error

the related code is:
Private mem As New MemoryStream
Dim sr As StreamReader
sr = File.OpenText(_licenseFileName)
Dim crypto As New CryptEngine
mem = crypto.Decrypt(sr)

Error 23 Value of type 'System.IO.MemoryStream' cannot be converted to 'String'. LincenseInfo.vb 161 29
at line: fs = crypto.Encrypt(mem), specifically the mem part is causing the error

the related code is:
Private mem As New MemoryStream
Dim fs As FileStream
fs = File.OpenWrite(_licenseFileName)
Dim crypto As New CryptEngine
fs = crypto.Encrypt(mem)

Please help me resolve this problem, thanks in advance.

If you need the full coding to the files involved, here it is:

LicenseInfo.vb
VB Code:
  1. Imports System
  2. Imports System.IO
  3. Imports System.Security
  4. Imports System.Security.Cryptography
  5. Imports System.Runtime.InteropServices
  6. Imports System.Text
  7.  
  8. Public Class LicenseInfo
  9.     Private _licenseFileName As String 'local var to hold the license file name
  10.     Private _licenseFileType As License 'local var to hold the license file type (private or shared)
  11.     Private mem As New MemoryStream
  12.  
  13.     'license file options
  14.     Public Enum License
  15.         MainFile  'The primary File, all data read from it
  16.         FileOne 'Secondary File, check to verify integrety of MainFile, only changed
  17.         FileTwo 'Secondary File, check to verify integrety of MainFile, only changed
  18.         FileThree 'Secondary File, check to verify integrety of MainFile, only changed
  19.         FileFour 'Secondary File, check to verify integrety of MainFile, only changed
  20.     End Enum
  21.  
  22.     'constructor
  23.     Public Sub New(ByVal LicenseFileType As License)
  24.         _licenseFileType = LicenseFileType 'remember this setting
  25.  
  26.         InitializeLicenseFile() 'setup the filename and location
  27.     End Sub
  28.  
  29.     'initialize the apps license file, create it if it doesn't exist
  30.     Private Sub InitializelicenseFile()
  31.         Dim sb As New Text.StringBuilder
  32.  
  33.         'build the path\filename depending on the location of the license file
  34.         Select Case _licenseFileType
  35.             Case License.MainFile 'primary license file
  36.                 'where the file is located
  37.                 sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
  38.                 sb.Append("\MedInfo.lic")
  39.             Case License.FileOne 'secondary license file
  40.                 'where the file is located
  41.                 sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
  42.                 sb.Append("\backup\MedInfo.lic")
  43.             Case License.FileTwo 'secondary license file
  44.                 'where the file is located
  45.                 sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
  46.                 sb.Append("\Licenses\MedInfo.lic")
  47.             Case License.FileThree 'secondary license file
  48.                 'where the file is located
  49.                 sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
  50.                 sb.Append("\RBS\MedInfo.lic")
  51.             Case License.FileFour 'secondary license file
  52.                 'where the file is located
  53.                 sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
  54.                 sb.Append("\MedInfo\License.lic")
  55.         End Select
  56.  
  57.         'create the directory if it isn't there
  58.         If Not IO.Directory.Exists(sb.ToString) Then
  59.             IO.Directory.CreateDirectory(sb.ToString)
  60.         End If
  61.  
  62.         _licenseFileName = sb.ToString 'completed license filename
  63.  
  64.         'if the file doesn't exist, ignore it, revert to trial mode, and alert the user
  65.         If Not IO.File.Exists(_licenseFileName) Then
  66.             Vtype = "trial"
  67.             MsgBox("A license file cannot be located, Medical Info will revert to trail mode, please re-register Medical Info to resolve this problem, thanyou.", MsgBoxStyle.Information, "Licensing Error")
  68.         End If
  69.  
  70.         Dim sr As StreamReader
  71.         sr = File.OpenText(_licenseFileName)
  72.         Dim crypto As New CryptEngine
  73.         mem = crypto.Decrypt(sr)
  74.     End Sub
  75.  
  76.     'get an application setting by key value
  77.     Public Function GetSetting(ByVal key As String) As String
  78.         'xml document object
  79.         Dim xd As New Xml.XmlDocument
  80.  
  81.         'load the xml file
  82.         xd.Load(mem)
  83.  
  84.         'query for a value
  85.         Dim Node As Xml.XmlNode = xd.DocumentElement.SelectSingleNode( _
  86.                                   "/MedInfo/License/add[@key=""" & key & """]")
  87.  
  88.         'return the value or nothing if it doesn't exist
  89.         If Not Node Is Nothing Then
  90.             Return Node.Attributes.GetNamedItem("value").Value
  91.         Else
  92.             Return Nothing
  93.         End If
  94.  
  95.     End Function
  96.  
  97.     'save an application setting, takes a key and a value
  98.     Public Sub SaveSetting(ByVal key As String, ByVal value As String)
  99.         'xml document object
  100.         Dim xd As New Xml.XmlDocument
  101.  
  102.         'load the xml file
  103.         xd.Load(mem)
  104.  
  105.         'get the value
  106.         Dim Node As Xml.XmlElement = CType(xd.DocumentElement.SelectSingleNode( _
  107.                                            "/MedInfo/License/add[@key=""" & _
  108.                                            key & """]"), Xml.XmlElement)
  109.         If Not Node Is Nothing Then
  110.             'key found, set the value
  111.             Node.Attributes.GetNamedItem("value").Value = value
  112.         Else
  113.             'key not found, create it
  114.             Node = xd.CreateElement("add")
  115.             Node.SetAttribute("key", key)
  116.             Node.SetAttribute("value", value)
  117.  
  118.             'look for the appsettings node
  119.             Dim Root As Xml.XmlNode = xd.DocumentElement.SelectSingleNode("/MedInfo/License")
  120.  
  121.             'add the new child node (this key)
  122.             If Not Root Is Nothing Then
  123.                 Root.AppendChild(Node)
  124.             Else
  125.                 Try
  126.                     'appsettings node didn't exist, add it before adding the new child
  127.                     Root = xd.DocumentElement.SelectSingleNode("/MedInfo")
  128.                     Root.AppendChild(xd.CreateElement("License"))
  129.                     Root = xd.DocumentElement.SelectSingleNode("/MedInfo/License")
  130.                     Root.AppendChild(Node)
  131.                 Catch ex As Exception
  132.                     'failed adding node, throw an error
  133.                     Throw New Exception("Could not set value", ex)
  134.                 End Try
  135.             End If
  136.         End If
  137.  
  138.         'finally, save the new version of the license file
  139.         xd.Save(mem)
  140.  
  141.         Dim fs As FileStream
  142.         fs = File.OpenWrite(_licenseFileName)
  143.         Dim crypto As New CryptEngine
  144.         fs = crypto.Encrypt(mem)
  145.     End Sub
  146. End Class

CryptEngine.vb
VB Code:
  1. Imports System
  2. Imports System.IO
  3. Imports System.Text
  4. Imports System.Security.Cryptography
  5. Public Class CryptEngine
  6.     Private key() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}
  7.     Private iv() As Byte = {1, 2, 3, 4, 5, 6, 7, 8}
  8.  
  9.     Public Function Encrypt(ByVal plainText As String) As Byte()
  10.         Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
  11.         Dim inputInBytes() As Byte = utf8encoder.GetBytes(plainText)
  12.  
  13.         Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
  14.  
  15.         Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateEncryptor(Me.key, Me.iv)
  16.  
  17.         Dim encryptedStream As MemoryStream = New MemoryStream()
  18.         Dim cryptStream As CryptoStream = New CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Write)
  19.  
  20.         cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
  21.         cryptStream.FlushFinalBlock()
  22.         encryptedStream.Position = 0
  23.  
  24.         Dim result(encryptedStream.Length - 1) As Byte
  25.         encryptedStream.Read(result, 0, encryptedStream.Length)
  26.         cryptStream.Close()
  27.         Return result
  28.     End Function
  29.  
  30.     Public Function Decrypt(ByVal inputInBytes() As Byte) As String
  31.         Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
  32.         Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
  33.  
  34.         Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateDecryptor(Me.key, Me.iv)
  35.  
  36.         Dim decryptedStream As MemoryStream = New MemoryStream()
  37.         Dim cryptStream As CryptoStream = New CryptoStream(decryptedStream, cryptoTransform, CryptoStreamMode.Write)
  38.         cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
  39.         cryptStream.FlushFinalBlock()
  40.         decryptedStream.Position = 0
  41.  
  42.         Dim result(decryptedStream.Length - 1) As Byte
  43.         decryptedStream.Read(result, 0, decryptedStream.Length)
  44.         cryptStream.Close()
  45.         Dim myutf As UTF8Encoding = New UTF8Encoding()
  46.         Return myutf.GetString(result)
  47.     End Function
  48. End Class