Solved

Using Crytography or Hash to Encrypt/Decrypt a text file in vb.net

Posted on 2007-03-17
4
1,378 Views
Last Modified: 2011-09-20
Hi,
I have a application that has several text files that I want to minimize the tampering from the end user because my program relies on these files. I would like to encrypt/decrypt these files using a "Key" to turn the encrypted file into a hex file. Would it be better to use a hash or cryptography scheme. I'm not looking to make the file super secret but just to deter casual picker. Please let me know how to do this in vb.net.
0
Comment
Question by:mk8guy
  • 2
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 18740849
Here is a very simple Encryption/Decryption class that works with Strings.  

If your files are relatively small, then you can read the whole thing into a String using StreamReader.ReadToEnd(), and then encrypt/decrypt it in memory using the methods below.

Leave the file encypted on the hard drive...

Imports System.IO
Imports System.Text
Imports System.Security.Cryptography

Public Class Crypto

    Private Shared DES As New TripleDESCryptoServiceProvider
    Private Shared MD5 As New MD5CryptoServiceProvider

    Public Shared Function MD5Hash(ByVal value As String) As Byte()
        Return MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(value))
    End Function

    Public Shared Function Encrypt(ByVal stringToEncrypt As String, ByVal key As String) As String
        DES.Key = Crypto.MD5Hash(key)
        DES.Mode = CipherMode.ECB
        Dim Buffer As Byte() = ASCIIEncoding.ASCII.GetBytes(stringToEncrypt)
        Return Convert.ToBase64String(DES.CreateEncryptor().TransformFinalBlock(Buffer, 0, Buffer.Length))
    End Function

    Public Shared Function Decrypt(ByVal encryptedString As String, ByVal key As String) As String
        Try
            DES.Key = Crypto.MD5Hash(key)
            DES.Mode = CipherMode.ECB
            Dim Buffer As Byte() = Convert.FromBase64String(encryptedString)
            Return ASCIIEncoding.ASCII.GetString(DES.CreateDecryptor().TransformFinalBlock(Buffer, 0, Buffer.Length))
        Catch ex As Exception
            MessageBox.Show("Invalid Key", "Decryption Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Function

End Class
0
 

Author Comment

by:mk8guy
ID: 18760658
Hi,

Your suggestion looks great, but it might be a little to high-powered for my application. Back in '92 I used  a simple sub to encrypt/decrypt by just adding a two-digit number to the ascii the character which I found very effective but now I was thinking to add just a little more sophistication. What i'm asking is that the file on the hard drive are "hash" encrypted then when my application needs one it opens, decrypts, uses it then encrypts it. The file size never exceeds 4K bytes so all this happens in less that 2-seconds. As far as the sub is concerned is that it has a path or string to work with. The key would be "hardcoded" into the sub.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18760724
"What i'm asking is that the file on the hard drive are "hash" encrypted then when my application needs one it opens, decrypts, uses it then encrypts it."

You can't decrypt something that has been hashed as hashing is "one way"...

Please read this article:
http://www.unixwiz.net/techtips/iguide-crypto-hashes.html
0
 

Author Comment

by:mk8guy
ID: 18761787
Ok. I got it and thanks. I will do it the simple way.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question