Reputation: 377
I recently coded an app which creates a random password out of characters taken from the alphabet, then encrypt them using the Rijndael algorithm and writes them to a .txt file.
Call me paranoid, but I like password security.
Now today, I ran into a problem, it was all working just fine, until just now.
I wanted to create a new password and have it added to my initial file, which already contained another password and my access key.
For some reason, the code below either appends the new text to the previous line, which messes up my decryption, or (using the commented code) it inserts a blank line in between the previous line and the one it had just written.
Dim empty As Boolean = True
Using reader As StreamReader = New StreamReader(fileLoc)
'If Not reader.ReadLine() Is Nothing Then
' empty = False
'End If
End Using
Using writer As StreamWriter = New StreamWriter(fileLoc, True)
If Not empty Then
writer.WriteLine()
End If
If passName = "" Then
writer.WriteLine(cryptPass)
Else
writer.WriteLine(passName & " " & cryptPass)
End If
End Using
Upvotes: 0
Views: 61
Reputation: 39132
Try something like this out instead:
Dim lines As New List(Of String)
lines.AddRange(System.IO.File.ReadAllLines(fileLoc))
For i As Integer = lines.Count - 1 To 0 Step -1
If lines(i) = "" Then
lines.RemoveAt(i)
End If
Next
If passName = "" Then
lines.Add(cryptPass)
Else
lines.Add(passName & " " & cryptPass)
End If
System.IO.File.WriteAllLines(fileLoc, lines.ToArray)
Upvotes: 1