I need a little help changing an encryption code
I had done a search on the forums and for encryption. I found one reply that lead me to this sitehttp://www.vb-helper.com/howto_net_des_file.html I have tested the code and to my suprise it actually works. It works so well infact that I'd like to add this in to a program that I am currently working on. However, I would like to change only one thing. I would like to be able to read from the file and at write to the same file. Now, I know very little about IO Streams and binary. All I really know is fileopen(1, dir, mode) but that's for text. Can some one please help me with this? I really need this to encrypt data files from my program and creating one unencrypted file then runing the encryption function to encrypt and create a second file is a little too much. I mean I did get this to work but there are a few bugs.
[956 byte] By [
Wellnow] at [2007-12-24]
Try this:
Module Module1
Sub Main()
'Open file to read everything into context string
Dim f As New IO.FileStream("c:\file.txt", IO.FileMode.Open)
Dim r As New IO.StreamReader(f)
Dim content As String = r.ReadToEnd()
f.Close()
' write back to the same file
f = New IO.FileStream("c:\file.txt", IO.FileMode.Create)
Dim w As New IO.StreamWriter(f)
w.Write(content + "s")
w.Flush()
f.Close()
End Sub
End Module
Thanks, That works a little better but I keep getting an error saying that 'Read' is not a member of string.
This is where it is comming from:
Dim bytes_read As Integer
bytes_read = in_stream.Read(buffer, 0, BLOCK_SIZE)
I think, I'm not sure, but I think I need to read the binary.
Basically I have a program that use a listview in details. A user will then save the content then have it encrypted. Now this is sensitive information so I'm not about to conver the the Characters to hex or anything easy to decrypt. I like this encryption but... I think I'm just stuck on this little thing. But if any one has a good text encryption that requires a password, (I'm looking in the Crypto Services of .NET) please share it.
Thanks
This is one way:
Dim myByte As Byte = Nothing
Dim fsRead As New FileStream("C:\FilenameToRead", FileMode.Open, FileAccess.Read)
Dim fsWrite As New
FileStream("C:\FilenameToWrite", FileMode.Create, FileAccess.Write,
FileShare.None)
Dim fsBinRead = New BinaryReader(fsRead)
Dim fsBinWrite = New BinaryWriter(fsWrite)
Try
Do
myByte = fsBinRead.ReadByte
fsBinWrite.Write(myByte)
Loop
Catch eof As EndOfStreamException
fsWrite.Close()
fsRead.Close()
End Try
..............................
I'm not sure what you are looking for but this will read a file in
binary mode and write to another file in binary mode. It reads and
writes one byte at a time until the read file returns an end of file
exception. Hopefully you can adapt it to your needs.
-Olice