|
-
Mar 17th, 2009, 07:39 AM
#1
Thread Starter
Lively Member
Encryption
Hi, hope you can help. I would like to know if it is possible to be able to encrypt a file and have my application be able to decrypt it and ammed the file as necessary. and then when the program exits it will encrypt the file back up.
Hope you can help
Stuart
-
Mar 17th, 2009, 07:52 AM
#2
Re: Encryption
That's easy. Here's a simple routine that'll strong encrypt and decrypt files.
Be sure to Import these at the top of your class as well:
Code:
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text
Code:
Private bytIV() As Byte = {190, 45, 111, 11, 32, 89, 21, 9, 255, 97, 8, 55, 44, 211, 23, 63}
Private Const chrKeyFill As Char = "X"c
Private Const intKeySize As Integer = 32
Private Sub CryptFile(ByVal strInName As String, ByVal strOutName As String, ByVal strKey As String, ByVal booDecrypt As Boolean)
Dim bytStorage(4096) As Byte
Dim intTotalBytesWritten As Long = 8
Dim intPackageSize As Integer
Dim fin As New FileStream(strInName, FileMode.Open, FileAccess.Read)
Dim fout As New FileStream(strOutName, FileMode.OpenOrCreate, FileAccess.Write)
fout.SetLength(0)
Dim intTotalFileLength As Long = fin.Length
Dim rij As New System.Security.Cryptography.RijndaelManaged
Dim crsStream As CryptoStream
If booDecrypt Then
crsStream = New CryptoStream(fout, _
rij.CreateDecryptor(ConvertKeyToBytes(strKey), bytIV), CryptoStreamMode.Write)
Else
crsStream = New CryptoStream(fout, _
rij.CreateEncryptor(ConvertKeyToBytes(strKey), bytIV), CryptoStreamMode.Write)
End If
While intTotalBytesWritten < intTotalFileLength
intPackageSize = fin.Read(bytStorage, 0, 4096)
crsStream.Write(bytStorage, 0, intPackageSize)
intTotalBytesWritten = Convert.ToInt32(intTotalBytesWritten + intPackageSize / rij.BlockSize * rij.BlockSize)
End While
crsStream.Close()
fout.Close()
fin.Close()
End Sub
Private Function ConvertKeyToBytes(ByVal strKey As String) As Byte()
Dim intLength As Integer = strKey.Length
If intLength < intKeySize Then
strKey &= Strings.StrDup(intKeySize - intLength, chrKeyFill)
Else
strKey = strKey.Substring(0, intKeySize)
End If
Return Encoding.UTF8.GetBytes(strKey)
End Function
If you have any questions as to how it works, feel free to post them.
-
Mar 17th, 2009, 08:46 AM
#3
Thread Starter
Lively Member
Re: Encryption
Hi, thanks for the update. your response looks promising but i have never worked with encryption so i dont know where to start. Its basically so information can be stored in a file and not readable by another program like notepad.
-
Mar 17th, 2009, 08:47 AM
#4
Re: Encryption
Just remember, that any decryption you perform in your application can be viewed and used to decrypt and encrypt that same file, using your encryption algorithm, unless you take additional steps (such as obfuscation, or storing the key in a "less exposed" location).
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
Mar 17th, 2009, 09:51 AM
#5
Re: Encryption
Right, it sounds like all you need is something simple so give the routine I posted above a try.
To encrypt a file:
CryptFile("C:\unencrypted.txt", "C\encrypted.txt", "BlahBlahIAmAKey", False)
And to decrypt it back:
CryptFile("C:\encrypted.txt", "C\unencrypted.txt", "BlahBlahIAmAKey", True)
Now, you probably also want to, after you encrypt your file, delete the unencrypted version of it.
Also, when you decrypt a file, you probably don't want to write it back to the disk, you want to hold it in memory, manipulate it, and then encrypt that memory back to disk. Take a look at my Strong Encryption Class in my signature and you'll see examples of encrypting and decrypting strings.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|