Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with checking if excel exist in client's computer 9 43
Crystal Report for VS2013 17 71
WPF - Tooltips for ComboBox items 5 28
Getfiles in vb.net 28 42
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

919 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

16 Experts available now in Live!

Get 1:1 Help Now