Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-03-17
4
Medium Priority
?
1,423 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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 86

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

722 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