Solved

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

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

617 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