User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the VB.NET section within the Software Development category of DaniWeb, a massive community of 427,123 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,029 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our VB.NET advertiser: Programming Forums
Views: 4576 | Replies: 6
Reply
Join Date: Jan 2007
Posts: 4
Reputation: hkinser is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
hkinser hkinser is offline Offline
Newbie Poster

Help Encrypt File using User Supplied Password

  #1  
Jan 8th, 2007
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Posts: 4,782
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 319
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Encrypt File using User Supplied Password

  #2  
Jan 10th, 2007
What encryption method are you using
I'm not a programmer. My attitude starts with ignorance, holds steady at conversation, and ends with a trip to the hospital. Get used to it.
Reply With Quote  
Join Date: Jan 2007
Posts: 4
Reputation: hkinser is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
hkinser hkinser is offline Offline
Newbie Poster

Re: Encrypt File using User Supplied Password

  #3  
Jan 10th, 2007
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
Reply With Quote  
Join Date: Apr 2005
Location: Old Hampshire, Old England (LOL)
Posts: 11,937
Reputation: jbennet is a jewel in the rough jbennet is a jewel in the rough jbennet is a jewel in the rough jbennet is a jewel in the rough 
Rep Power: 30
Solved Threads: 268
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Microsoft Fanboy

Re: Encrypt File using User Supplied Password

  #4  
Jan 11th, 2007
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
Last edited by jbennet : Jan 11th, 2007 at 6:07 pm.
TRY MY SUGGESTIONS AT YOUR OWN RISK!
james.bennet1@ntlworld.com
Reply With Quote  
Join Date: Aug 2005
Posts: 4,782
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 319
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Encrypt File using User Supplied Password

  #5  
Jan 12th, 2007
Out of interest does that md5 stuff come with vb.net or are you using a third party library?
I'm not a programmer. My attitude starts with ignorance, holds steady at conversation, and ends with a trip to the hospital. Get used to it.
Reply With Quote  
Join Date: Apr 2005
Location: Old Hampshire, Old England (LOL)
Posts: 11,937
Reputation: jbennet is a jewel in the rough jbennet is a jewel in the rough jbennet is a jewel in the rough jbennet is a jewel in the rough 
Rep Power: 30
Solved Threads: 268
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Microsoft Fanboy

Re: Encrypt File using User Supplied Password

  #6  
Jan 12th, 2007
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
Last edited by jbennet : Jan 12th, 2007 at 3:15 pm.
TRY MY SUGGESTIONS AT YOUR OWN RISK!
james.bennet1@ntlworld.com
Reply With Quote  
Join Date: Jan 2007
Posts: 4
Reputation: hkinser is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
hkinser hkinser is offline Offline
Newbie Poster

Re: Encrypt File using User Supplied Password

  #7  
Jan 13th, 2007
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb VB.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the VB.NET Forum

All times are GMT -4. The time now is 6:28 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC