Imports System Imports System.IO Imports System.Text Imports System.Security.Cryptography
Public Class PersistedInfo Private utf As Encoding = New UTF8Encoding Private des3 As TripleDES = New TripleDESCryptoServiceProvider Private encrypt As ICryptoTransform Private decrypt As ICryptoTransform
Public Sub New(ByVal key As String) If Not (key.Length = 24) Then Throw New Exception ("Key musy be 24 characters") End If Dim bkey As Byte() = utf.GetBytes(key) Dim iv As Byte() = {1, 2, 3, 4, 5, 6, 7, 8} encrypt = des3.CreateEncryptor(bkey, iv) decrypt = des3.CreateDecryptor(bkey, iv) End Sub
Public Sub Save(ByVal data As String, ByVal filename As String) Dim cipher As Byte() = encrypt.TransformFinalBlock(utf.GetBytes(data), 0, utf.GetByteCount(data)) Dim stm As Stream = New FileStream (filename, FileMode.Create) stm.Write(cipher, 0, cipher.Length) stm.Close End Sub
Public Function Load(ByVal filename As String) As String Dim filelen As Integer = CType((New FileInfo (filename)).Length, Integer) Dim cipher(filelen - 1) As Byte Dim stm As Stream = New FileStream (filename, FileMode.Open) stm.Read(cipher, 0, filelen) stm.Close Dim plain As String = utf.GetString(decrypt.TransformFinalBlock(cipher, 0, cipher.Length)) Return plain End Function End Class
Public Class PersistedUserInfo Private pi As PersistedInfo Private _username As String Private _password As String Private _group As String
Public Sub New(ByVal key As String) pi = New PersistedInfo (key) End Sub
Public Sub Save(ByVal filename As String) pi.Save(_username + "," + _password + "," + _group, filename) End Sub
Public Sub Load(ByVal filename As String) Dim temp As String = pi.Load(filename) Dim tempsplit As String() = temp.Split(",".ToCharArray) _username = tempsplit(0) _password = tempsplit(1) _group = tempsplit(2) End Sub
Public Property Username() As String Get Return _username End Get Set _username = value End Set End Property
Public Property Password() As String Get Return _password End Get Set _password = value End Set End Property
Public Property Group() As String Get Return _group End Get Set _group = value End Set End Property End Class
Class MainClass
Public Shared Sub Main(ByVal args As String()) Dim pui As PersistedUserInfo = New PersistedUserInfo ("123456789012345678901234") pui.Username = "arne" pui.Password = "hemmeligt" pui.Group = "tech" pui.Save("C:\userinfo.dat") Dim pui2 As PersistedUserInfo = New PersistedUserInfo ("123456789012345678901234") pui2.Load("C:\userinfo.dat") Console.WriteLine(pui2.Username + " " + pui2.Password + " " + pui2.Group) End Sub End Class
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.