VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 307,154 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,599 people online right now. Registration is fast and FREE... Join Now!




encrypt and decrypt text file

 

encrypt and decrypt text file

kayatri

22 Jul, 2009 - 11:21 PM
Post #1

D.I.C Head
**

Joined: 5 May, 2009
Posts: 190


My Contributions
Hi all

I save data from datagridview to text file . I also load the same data from text file into datagridview for edit and update the same thing to text file.
Now i want to make my Drill layer header.txt in encrypted format to prevent anyone to open that file and modify out of program. They only can modify it through the program

How can we do like this?



My Drill Layer Header.txt file is like this

T240C3.175F012S27H2000
T239C0.95F034S55H1000
T236C1.2F029S48H2000
T234C1.6F029S48H2000
T232C1.95F022S37H2000
T230C2.4F017S3H2000


I find this code from internet:

CODE
Imports System
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System.Runtime.InteropServices
Imports System.Text

Module Module1
    ' Call this function to remove the key from memory after it is used for security.
    <DllImport("kernel32.dll")> _
    Public Sub ZeroMemory(ByVal addr As IntPtr, ByVal size As Integer)
    End Sub

    ' Function to generate a 64-bit key.
    Function GenerateKey() As String
        ' Create an instance of a symmetric algorithm. The key and the IV are generated automatically.
        Dim desCrypto As DESCryptoServiceProvider = DESCryptoServiceProvider.Create()

        ' Use the automatically generated key for encryption.
        Return ASCIIEncoding.ASCII.GetString(desCrypto.Key)

    End Function

    Public Sub EncryptFile(ByVal sInputFilename As String, _
                   ByVal sOutputFilename As String, _
                   ByVal sKey As String)

        Dim fsInput As New FileStream(sInputFilename, _
                                          FileMode.Open, FileAccess.Read)
        Dim fsEncrypted As New FileStream(sOutputFilename, _
                                    FileMode.Create, FileAccess.Write)

        Dim DES As New DESCryptoServiceProvider()

        'Set secret key for DES algorithm.
        'A 64-bit key and an IV are required for this provider.
        DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)

        'Set the initialization vector.
        DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)

        'Create the DES encryptor from this instance.
        Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
        'Create the crypto stream that transforms the file stream by using DES encryption.
        Dim cryptostream As New CryptoStream(fsEncrypted, _
                                            desencrypt, _
                                            CryptoStreamMode.Write)

        'Read the file text to the byte array.
        Dim bytearrayinput(fsInput.Length - 1) As Byte
        fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
        'Write out the DES encrypted file.
        cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
        cryptostream.Close()
    End Sub

    Public Sub DecryptFile(ByVal sInputFilename As String, _
        ByVal sOutputFilename As String, _
        ByVal sKey As String)

        Dim DES As New DESCryptoServiceProvider()
        'A 64-bit key and an IV are required for this provider.
        'Set the secret key for the DES algorithm.
        DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
        'Set the initialization vector.
        DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)

        'Create the file stream to read the encrypted file back.
        Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
        'Create the DES decryptor from the DES instance.
        Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
        'Create the crypto stream set to read and to do a DES decryption transform on incoming bytes.
        Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
        'Print out the contents of the decrypted file.
        Dim fsDecrypted As New StreamWriter(sOutputFilename)
        fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
        fsDecrypted.Flush()
        fsDecrypted.Close()
    End Sub
End module



This is how i save the datagridview content to Drill Layer Header.txt. And how i call module to encrypt

CODE
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Using theWriter As New System.IO.StreamWriter("c:\Drill Layer Header.txt", True)

            For Each row As DataGridViewRow In Me.DataGridView1.Rows

                If Not row.IsNewRow Then

                    theWriter.WriteLine(String.Format("T{0}C{1}F{2}S{3}H{4}", _
                                                   row.Cells(0).Value, _
                                                   row.Cells(1).Value, _
                                                   row.Cells(2).Value, _
                                                   row.Cells(3).Value, _
                                                   row.Cells(4).Value))
                End If
            Next

        End Using

        MessageBox.Show("Information Saved", "Saved!")

        'Must be 64 bits, 8 bytes.
        Dim sSecretKey As String

        ' Get the key for the file to encrypt.
        ' You can distribute this key to the user who will decrypt the file.
        sSecretKey = GenerateKey()

        ' For additional security, pin the key.
        Dim gch As GCHandle = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned)


        ' Encrypt the file.        
        EncryptFile("c:\Drill Layer Header.txt", _
                        "c:\Drill Layer Header original1.txt", _
                        sSecretKey)
      



        ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2)
        gch.Free()


This code can encryp the text file.
But accrding to this code
EncryptFile("c:\Drill Layer Header.txt", _ "c:\Drill Layer Header original1.txt", _ sSecretKey) It ask for input and output file. So it take data from Drill Layer Header then encrypt the save in drill layer header original.txt.
I want it to take data from drill layer header and encrypt and save in same file.
Can we do in that way?




User is offlineProfile CardPM
+Quote Post



Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 05:18PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month