-
Apr 26th, 2006, 09:02 PM
#1
Thread Starter
New Member
[2005] File I/O Question
their is a problem...
LicenseInfo.vb
VB Code:
Imports System
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System.Runtime.InteropServices
Imports System.Text
Public Class LicenseInfo
Private _licenseFileName As String 'local var to hold the license file name
Private _licenseFileType As License 'local var to hold the license file type (private or shared)
Private mem As New MemoryStream
'license file options
Public Enum License
MainFile 'The primary File, all data read from it
FileOne 'Secondary File, check to verify integrety of MainFile, only changed
FileTwo 'Secondary File, check to verify integrety of MainFile, only changed
FileThree 'Secondary File, check to verify integrety of MainFile, only changed
FileFour 'Secondary File, check to verify integrety of MainFile, only changed
End Enum
'constructor
Public Sub New(ByVal LicenseFileType As License)
_licenseFileType = LicenseFileType 'remember this setting
InitializeLicenseFile() 'setup the filename and location
End Sub
'initialize the apps license file, create it if it doesn't exist
Private Sub InitializelicenseFile()
Dim sb As New Text.StringBuilder
'build the path\filename depending on the location of the license file
Select Case _licenseFileType
Case License.MainFile 'primary license file
'where the file is located
sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
sb.Append("\MedInfo.lic")
Case License.FileOne 'secondary license file
'where the file is located
sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
sb.Append("\backup\MedInfo.lic")
Case License.FileTwo 'secondary license file
'where the file is located
sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
sb.Append("\Licenses\MedInfo.lic")
Case License.FileThree 'secondary license file
'where the file is located
sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
sb.Append("\RBS\MedInfo.lic")
Case License.FileFour 'secondary license file
'where the file is located
sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
sb.Append("\MedInfo\License.lic")
End Select
'create the directory if it isn't there
If Not IO.Directory.Exists(sb.ToString) Then
IO.Directory.CreateDirectory(sb.ToString)
End If
_licenseFileName = sb.ToString 'completed license filename
'if the file doesn't exist, ignore it, revert to trial mode, and alert the user
If Not IO.File.Exists(_licenseFileName) Then
Vtype = "trial"
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")
End If
Dim sr As StreamReader
sr = File.OpenText(_licenseFileName)
Dim crypto As New CryptEngine
mem = crypto.Decrypt(sr)
End Sub
'get an application setting by key value
Public Function GetSetting(ByVal key As String) As String
'xml document object
Dim xd As New Xml.XmlDocument
'load the xml file
xd.Load(mem)
'query for a value
Dim Node As Xml.XmlNode = xd.DocumentElement.SelectSingleNode( _
"/MedInfo/License/add[@key=""" & key & """]")
'return the value or nothing if it doesn't exist
If Not Node Is Nothing Then
Return Node.Attributes.GetNamedItem("value").Value
Else
Return Nothing
End If
End Function
'save an application setting, takes a key and a value
Public Sub SaveSetting(ByVal key As String, ByVal value As String)
'xml document object
Dim xd As New Xml.XmlDocument
'load the xml file
xd.Load(mem)
'get the value
Dim Node As Xml.XmlElement = CType(xd.DocumentElement.SelectSingleNode( _
"/MedInfo/License/add[@key=""" & _
key & """]"), Xml.XmlElement)
If Not Node Is Nothing Then
'key found, set the value
Node.Attributes.GetNamedItem("value").Value = value
Else
'key not found, create it
Node = xd.CreateElement("add")
Node.SetAttribute("key", key)
Node.SetAttribute("value", value)
'look for the appsettings node
Dim Root As Xml.XmlNode = xd.DocumentElement.SelectSingleNode("/MedInfo/License")
'add the new child node (this key)
If Not Root Is Nothing Then
Root.AppendChild(Node)
Else
Try
'appsettings node didn't exist, add it before adding the new child
Root = xd.DocumentElement.SelectSingleNode("/MedInfo")
Root.AppendChild(xd.CreateElement("License"))
Root = xd.DocumentElement.SelectSingleNode("/MedInfo/License")
Root.AppendChild(Node)
Catch ex As Exception
'failed adding node, throw an error
Throw New Exception("Could not set value", ex)
End Try
End If
End If
'finally, save the new version of the license file
xd.Save(mem)
Dim fs As FileStream
fs = File.OpenWrite(_licenseFileName)
Dim crypto As New CryptEngine
fs = crypto.Encrypt(mem)
End Sub
End Class
LicenseInfo.vb 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))
CryptEngine.vb
VB Code:
Imports System
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
Public Class CryptEngine
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}
Private iv() As Byte = {1, 2, 3, 4, 5, 6, 7, 8}
Public Function Encrypt(ByVal plainText As String) As Byte()
Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
Dim inputInBytes() As Byte = utf8encoder.GetBytes(plainText)
Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateEncryptor(Me.key, Me.iv)
Dim encryptedStream As MemoryStream = New MemoryStream()
Dim cryptStream As CryptoStream = New CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Write)
cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
cryptStream.FlushFinalBlock()
encryptedStream.Position = 0
Dim result(encryptedStream.Length - 1) As Byte
encryptedStream.Read(result, 0, encryptedStream.Length)
cryptStream.Close()
Return result
End Function
Public Function Decrypt(ByVal inputInBytes() As Byte) As String
Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateDecryptor(Me.key, Me.iv)
Dim decryptedStream As MemoryStream = New MemoryStream()
Dim cryptStream As CryptoStream = New CryptoStream(decryptedStream, cryptoTransform, CryptoStreamMode.Write)
cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
cryptStream.FlushFinalBlock()
decryptedStream.Position = 0
Dim result(decryptedStream.Length - 1) As Byte
decryptedStream.Read(result, 0, decryptedStream.Length)
cryptStream.Close()
Dim myutf As UTF8Encoding = New UTF8Encoding()
Return myutf.GetString(result)
End Function
End Class
Please help me resolve this problem, thanks in advance.
Last edited by RyanB88; Apr 26th, 2006 at 09:50 PM.
-
Apr 26th, 2006, 09:05 PM
#2
Re: [2005] File I/O Question
What is the problem and what line is it occurring at? Also, the correct tags would be [ vbcode ] [ /vbcode ] to do the highlighting...
**EDIT - I see it now, misssed it through all of that same color text and code...
-
Apr 26th, 2006, 09:14 PM
#3
Thread Starter
New Member
Re: [2005] File I/O Question
Originally Posted by gigemboy
What is the problem and what line is it occurring at? Also, the correct tags would be [ vbcode ] [ /vbcode ] to do the highlighting...
**EDIT - I see it now, misssed it through all of that same color text and code...
but that would put my post over the 10000 charecter limit or whatever the limit to post size is...lol, the eroors are mixed in their, just confusing to find because the code isn't seperated well
but they are this:
LicenseInfo.vb 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))
hope someone can help me resolve the problems
-
Apr 26th, 2006, 09:26 PM
#4
Re: [2005] File I/O Question
If putting tags in your post would put it over the size limit then your post is way too long anyway. I for one will rarely read that much code even if it is in the correct tags. I'm quite sure that there is a lot of irrelevant stuff there. How about removing the imports from the top and that block of lines that are commented out for a start? If you want to maximise your chances of getting help then you need to make your thread as attractive as possible to those likely to help. Long code listings, particularly without formatting tags, are VERY unattractive. Think succinct and concise. Be as brief as you possibly can while still being clear and conveying all of the relevant information.
-
Apr 26th, 2006, 09:43 PM
#5
Re: [2005] File I/O Question
A StreamReader is an object used to read plain text from a file. You cannpot pass it to a method that expects a Byte array. You can call methods of the StreamReader to get a String object, like ReadToEnd, and then use an Encoding object to convert that to a Byte array.
It's basically the reverse for the second case. You get the Bytes from the MemoryStream and use an Encoding object to convert to a String. Also, you are assigning two different objects to the 'fs' variable. I don't think you want to do that.
-
Apr 26th, 2006, 09:49 PM
#6
Thread Starter
New Member
Re: [2005] File I/O Question
what would i use then?
could you provide me with some example code?
Last edited by RyanB88; Apr 26th, 2006 at 10:02 PM.
-
Jul 4th, 2008, 06:00 AM
#7
New Member
Re: [2005] File I/O Question
Is this not where your problem lies?
Code:
Public Class LicenseInfo
Private _licenseFileName As String 'local var to hold the license file name
Private _Jmcilhinney As String "Is a C#NT"
Private mem As New MemoryStream 'license file options
Public Enum License MainFile 'The primary File, all data read from it
FileOne 'Secondary File, check to verify integrety of MainFile, only changed
FileTwo 'Secondary File, check to verify integrety of MainFile, only changed
FileThree 'Secondary File, check to verify integrety of MainFile, only changed
FileFour 'Secondary File, check to verify integrety of MainFile, only changed
End Enum 'constructor
Public Sub New(ByVal LicenseFileType As License)_licenseFileType = LicenseFileType 'remember this setting
InitializeLicenseFile() 'setup the filename and location
End Sub
end class
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
|