Hello everyone.

I've read several articles on the internet in trying to figure this out; however, those article seem a bit over my head apparently and I can't seem to find a code example that is simple and to the point to implement.

I'm coding a password manager program in vb 2005. All that is left to do is obtain a password from the user the first time they start the program, and use that password as the encryption key in creating the file that will hold the data from the listview control.

For now, the login form is simply taking the password the user enters and encrypting it inside a separate file for now as you will see below - the file itself is not encrypted. I have all the forms complete and the listview data is writing to a currently unencrypted file for now. I just want to make it all one file with the user password being the key to encrypt it all.

Here is the login code I'm using if we can somehow modify it to do what I'm looking for:

If txtUserPsw.Text = txtPswVerify.Text And Me.txtPswVerify.Text.Length >= 8 Then
Try
' Obtain a FileStream object.
Dim aFileStream As New FileStream(AppPath(False) & "\passsafe.bin", FileMode.Create)
' Obtain a BinaryWriter object.
Dim aBinaryWriter As New BinaryWriter(aFileStream)
' Encrypt the new password and binary write to a binary file.
aBinaryWriter.Write(EncryptPassword.EncryptString(Me.txtPswVerify.Text))
' Close the FileStream and the BinaryWriter objects.
aFileStream.Close()
aBinaryWriter.Close()
' Message user.
MessageBox.Show("Password was encrypted and saved. Don't forget it!")
'Display Main form (Frmlogin)
Dim safe As New frmSafe
'Hides login form
Me.Visible = False
safe.ShowDialog()
'disposes login form on exit of main form
'Application.Exit()
Me.Close()
Catch ex As Exception
' Message user.
MessageBox.Show("Password was not saved. ERROR: " & ex.Message)
End Try
End If

Thanks everyone!

Sincerely,
Harold

Member Avatar for iamthwee

What encryption method are you using

I'm using the following encryption class; however, I can use another if required to achieve what I'm trying to do. Thanks for taking time to review this:

Public Class EncryptPassword
Public Shared Function EncryptString(ByVal sourceString As String) As String
' Declare a variable of type Byte array named sourceStringToBytes.
' Call a UnicodeEncoding object's GetBytes method, passing it the sourceString.
' Assign the resulting byte array to the sourceStringToBytes variable.
Dim sourceStringToBytes As Byte() = (New UnicodeEncoding()).GetBytes(sourceString)
' Declare a variable of type Byte array named hashedBytes.
' Call a MD5CryptoServiceProvider objects's ComputeHash method
' passing it the sourceStringToBytes array.
' Assign the resulting byte array to the hashedBytes variable.
Dim hashedBytes As Byte() = New MD5CryptoServiceProvider().ComputeHash(sourceStringToBytes)
' Use the BitConverter class's shared ToString method to return
' the bytes encrypted as a string.
Return BitConverter.ToString(hashedBytes)
End Function
End Class

ive used XOR for encryption(email me and i will send you a code sample -vb.net 2003 format)

(this is the whole form)

PublicClass Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
Friend WithEvents txtNote As System.Windows.Forms.TextBox
Friend WithEvents mnuSaveAsItem As System.Windows.Forms.MenuItem
Friend WithEvents mnuInsertDateItem As System.Windows.Forms.MenuItem
Friend WithEvents mnuExitItem As System.Windows.Forms.MenuItem
Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
Friend WithEvents lblNote As System.Windows.Forms.Label
Friend WithEvents mnuOpenItem As System.Windows.Forms.MenuItem
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Friend WithEvents mnuCloseItem As System.Windows.Forms.MenuItem
'Required by the Windows Form Designer
Private components As System.ComponentModel.Container
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer. 
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.mnuCloseItem = New System.Windows.Forms.MenuItem
Me.mnuSaveAsItem = New System.Windows.Forms.MenuItem
Me.lblNote = New System.Windows.Forms.Label
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.MenuItem1 = New System.Windows.Forms.MenuItem
Me.mnuOpenItem = New System.Windows.Forms.MenuItem
Me.mnuInsertDateItem = New System.Windows.Forms.MenuItem
Me.mnuExitItem = New System.Windows.Forms.MenuItem
Me.txtNote = New System.Windows.Forms.TextBox
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog
Me.SuspendLayout()
'
'mnuCloseItem
'
Me.mnuCloseItem.Enabled = False
Me.mnuCloseItem.Index = 1
Me.mnuCloseItem.Text = "Close"
'
'mnuSaveAsItem
'
Me.mnuSaveAsItem.Index = 2
Me.mnuSaveAsItem.Text = "Save Encrypted File &As..."
'
'lblNote
'
Me.lblNote.Location = New System.Drawing.Point(16, 8)
Me.lblNote.Name = "lblNote"
Me.lblNote.Size = New System.Drawing.Size(344, 24)
Me.lblNote.TabIndex = 0
Me.lblNote.Text = "Type some text and then use Encryption commands."
'
'MainMenu1
'
Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})
'
'MenuItem1
'
Me.MenuItem1.Index = 0
Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuOpenItem, Me.mnuCloseItem, Me.mnuSaveAsItem, Me.mnuInsertDateItem, Me.mnuExitItem})
Me.MenuItem1.Text = "File"
'
'mnuOpenItem
'
Me.mnuOpenItem.Index = 0
Me.mnuOpenItem.Text = "Open Encrypted File..."
'
'mnuInsertDateItem
'
Me.mnuInsertDateItem.Index = 3
Me.mnuInsertDateItem.Text = "&Insert Date"
'
'mnuExitItem
'
Me.mnuExitItem.Index = 4
Me.mnuExitItem.Text = "E&xit"
'
'txtNote
'
Me.txtNote.Location = New System.Drawing.Point(16, 48)
Me.txtNote.Multiline = True
Me.txtNote.Name = "txtNote"
Me.txtNote.ScrollBars = System.Windows.Forms.ScrollBars.Both
Me.txtNote.Size = New System.Drawing.Size(360, 184)
Me.txtNote.TabIndex = 1
Me.txtNote.Text = ""
'
'SaveFileDialog1
'
Me.SaveFileDialog1.FileName = "doc1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(392, 277)
Me.Controls.Add(Me.txtNote)
Me.Controls.Add(Me.lblNote)
Me.Menu = Me.MainMenu1
Me.Name = "Form1"
Me.Text = "Xor Encryption"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub mnuSaveAsItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSaveAsItem.Click
Dim letter As Char
Dim strCode As String
Dim i, charsInFile, Code As Short
SaveFileDialog1.Filter = "Text files (*.txt)|*.txt"
SaveFileDialog1.ShowDialog()
If SaveFileDialog1.FileName <> "" Then
strCode = InputBox("Enter Encryption Code")
If strCode = "" Then Exit Sub 'if cancel clicked
'save text with encryption scheme
Code = CShort(strCode)
charsInFile = txtNote.Text.Length
FileOpen(1, SaveFileDialog1.FileName, OpenMode.Output)
For i = 0 To charsInFile - 1
letter = txtNote.Text.Substring(i, 1)
'convert to number w/ Asc, then use Xor to encrypt
Print(1, Asc(letter) Xor Code) 'and save in file
Next
FileClose(1)
mnuCloseItem.Enabled = True
End If
End Sub
Private Sub mnuInsertDateItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuInsertDateItem.Click
txtNote.Text = DateString & vbCrLf & txtNote.Text
txtNote.Select(1, 0) 'remove selection
End Sub
Private Sub mnuExitItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExitItem.Click
End
End Sub
Private Sub mnuOpenItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOpenItem.Click
Dim ch As Char
Dim strCode As String
Dim Code, Number As Short
Dim Decrypt As String = ""
OpenFileDialog1.Filter = "Text files (*.TXT)|*.TXT"
OpenFileDialog1.ShowDialog() 'display Open dialog box
If OpenFileDialog1.FileName <> "" Then
Try 'open file and trap any errors using handler
strCode = InputBox("Enter Encryption Code")
If strCode = "" Then Exit Sub 'if cancel clicked
Code = CShort(strCode)
FileOpen(1, OpenFileDialog1.FileName, OpenMode.Input)
Do Until EOF(1) 'read lines from file
Input(1, Number) 'read encrypted numbers
ch = Chr(Number Xor Code) 'convert with Xor
Decrypt = Decrypt & ch 'and build string
Loop
txtNote.Text = Decrypt 'then display converted string
lblNote.Text = OpenFileDialog1.FileName
txtNote.Select(1, 0) 'remove text selection
txtNote.Enabled = True 'allow text cursor
mnuCloseItem.Enabled = True 'enable Close command
mnuOpenItem.Enabled = False 'disable Open command
Catch
MsgBox("Error opening file.")
Finally
FileClose(1) 'close file
End Try
End If
End Sub
Private Sub mnuCloseItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCloseItem.Click
txtNote.Text = "" 'clear text box
lblNote.Text = "Load a text file with the Open command."
mnuCloseItem.Enabled = False 'disable Close command
mnuOpenItem.Enabled = True 'enable Open command
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
EndClass

courtousey of VB.NET 2003 Step By Step

Member Avatar for iamthwee

Out of interest does that md5 stuff come with vb.net or are you using a third party library?

dunno but all the Xor stuff just uses the .net framework - no addons needed

That whole app is from the sample cd of Visual Basic 2003.NET STep By STep by Microsoft Press

Ive emailed it so well see if maybe he can come to a compromise

The MD5 encryption is part of VB.net...no additional DLL's or anything has to be added.

That' why I chose that route first, but as simple as it is to implement, it's somewhat of a pain to extend like I wanted to with it.

Thanks for all you help.

I think this simple method will do more work..
source http://developerskb.blogspot.com

Public Class Form1
    Dim strtempchar As String
    Public Function cryption(ByVal text As String)
        Dim i As Integer
        For i = 1 To Len(text)
            If Asc(Mid$(text, i, 1)) < 128 Then
                strtempchar = CType(Asc(Mid$(text, i, 1)) + 128, String)
            ElseIf Asc(Mid$(text, i, 1)) > 128 Then
                strtempchar = CType(Asc(Mid$(text, i, 1)) - 128, String)
            End If
            Mid$(text, i, 1) = Chr(CType(strtempchar, Integer))
        Next i
        Return text
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        strtempchar = Me.TextBox1.Text
        Me.TextBox2.Text = cryption(strtempchar)
    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.