Solved

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

Posted on 2007-03-17
4
1,352 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now