In .net I use the following code to encrypt text data files created, not sure if this is any help.
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
Imports System.IO.Directory
Private Function CreateKey(ByVal strPassword As String) As Byte()
Dim bytKey As Byte()
Dim bytSalt As Byte() = System.Text.Encoding.ASCII.GetBytes("yourstring")
Dim pdb As New Rfc2898DeriveBytes(strPassword, bytSalt)
bytKey = pdb.GetBytes(32)
Return bytKey 'Return the key.
End Function
Private Function CreateIV(ByVal strPassword As String) As Byte()
Dim bytIV As Byte()
Dim bytSalt As Byte() = System.Text.Encoding.ASCII.GetBytes("yourstring")
Dim pdb As New Rfc2898DeriveBytes(strPassword, bytSalt)
bytIV = pdb.GetBytes(16)
Return bytIV 'Return the IV.
End Function
Private Sub EncryptTextToFile(ByVal Data As String, ByVal FileName As String)
Dim key() As Byte = CreateKey("anotherstring")
Dim IV() As Byte = CreateIV("yetsanotherstring")
Try
' Create or open the specified file.
Dim fStream As FileStream = File.Open(FileName, FileMode.Create)
' Create a new Rijndael object.
Dim RijndaelAlg As Rijndael = Rijndael.Create
' Create a CryptoStream using the FileStream
' and the passed key and initialization vector (IV).
Dim cStream As New CryptoStream(fStream, RijndaelAlg.CreateEncryptor(key, IV), CryptoStreamMode.Write)
' Create a StreamWriter using the CryptoStream.
Dim sWriter As New StreamWriter(cStream)
Try
' Write the data to the stream
' to encrypt it.
sWriter.WriteLine(Data)
Catch e As Exception
MessageBox.Show(e.Message, "An error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
' Close the streams and
' close the file.
sWriter.Close()
cStream.Close()
fStream.Close()
End Try
Catch e As CryptographicException
MessageBox.Show(e.Message, "A Cryptographic error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error)
Catch e As UnauthorizedAccessException
MessageBox.Show(e.Message, "A file error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Function DecryptTextFromFile(ByVal FileName As String) As String
Dim key() As Byte = CreateKey("anotherstring")
Dim IV() As Byte = CreateIV("yetanotherstring")
Try
' Create or open the specified file.
Dim fStream As FileStream = File.Open(FileName, FileMode.OpenOrCreate)
' Create a new Rijndael object.
Dim RijndaelAlg As Rijndael = Rijndael.Create
' Create a CryptoStream using the FileStream
' and the passed key and initialization vector (IV).
Dim cStream As New CryptoStream(fStream, RijndaelAlg.CreateDecryptor(key, IV), CryptoStreamMode.Read)
' Create a StreamReader using the CryptoStream.
Dim sReader As New StreamReader(cStream)
' Read the data from the stream
' to decrypt it.
Dim val As String = Nothing
Try
val = sReader.ReadLine()
Catch e As Exception
MessageBox.Show(e.Message, "An Cerror occurred", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
' Close the streams and
' close the file.
sReader.Close()
cStream.Close()
fStream.Close()
End Try
' Return the string.
Return val
Catch e As CryptographicException
MessageBox.Show(e.Message, "A Cryptographic error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return Nothing
Catch e As UnauthorizedAccessException
MessageBox.Show(e.Message, "A file error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return Nothing
End Try
End Function
----
"What is this talk of 'release'? Klingons do not'release' software. It escapes leaving a bloody trail of developers and quality assurance people in its wake!"