Results 1 to 5 of 5

Thread: Encryption

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2008
    Posts
    90

    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

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    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.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2008
    Posts
    90

    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.

  4. #4
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    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."

  5. #5
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    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.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width