VB .NET Tip: Encryption in Just Twelve Lines of Code!

At times, you may want to very simply encrypt a small piece of text to store in the registry, a database, or file, but you don't want the overhead or complexity of a government-standard encryption technique.

A much simpler encryption method is required, and the following function provides just that. It's called Crypt: Pass it your plain text and it'll encrypt it; pass it your encrypted text and it'll decrypt it. It's simple and all in fewer than fifteen lines of code:

Public Function SimpleCrypt( _
       ByVal Text As String) As String
  ' Encrypts/decrypts the passed string using 
  ' a simple ASCII value-swapping algorithm
  Dim strTempChar As String, i As Integer
  For i = 1 To Len(Text)
    If Asc(Mid$(Text, i, 1)) < 128 Then
      strTempChar = _
CType(Asc(Mid$(Text, i, 1)) + 128, String)
    ElseIf Asc(Mid$(Text, i, 1)) > 128 Then
      strTempChar = _
CType(Asc(Mid$(Text, i, 1)) - 128, String)
    End If
    Mid$(Text, i, 1) = _
        Chr(CType(strTempChar, Integer))
  Next i
  Return Text
End Function

It's not recommended for highly confidential information (as anyone with this script could also decrypt your data), but it's nonetheless highly useful. Here's how you might use this function:

Dim MyText As String
' Encrypt
MyText = "Karl Moore"
MyText = Crypt(MyText)
MessageBox.Show(MyText)
' Decrypt
MyText = Crypt(MyText)
MessageBox.Show(MyText)

# # #



About the Author

Karl Moore

Karl Moore (MCSD, MVP) is an experience author living in Yorkshire, England. He is the author of numerous technology books, including the new Ultimate VB .NET and ASP.NET Code Book (ISBN 1-59059-106-2), plus regularly features at industry conferences and on BBC radio. Moore also runs his own creative consultancy, White Cliff Computing Ltd. Visit his official Web site at www.karlmoore.com.

Comments

  • Balu

    Posted by arbalu on 12/03/2008 09:46am

    Really helped me. it enough for me. I found similar examples at http://developerskb.blogspot.com

    Reply
  • Nice

    Posted by CTELLO on 08/11/2005 09:13am

    good code!! Short and sweet!!! thanks for your post.

    Reply
  • .NET code

    Posted by darwen on 08/06/2005 06:11pm

    I can do highly secure encryption in .NET code in four lines of code. I think your article has a half-life. But for those out there who are using VB6, this is useful.

    • The Four lines...

      Posted by Zangai on 07/10/2006 11:07am

      Yeah, i would like to know that...

      Reply
    • So what are your four lines?

      Posted by Brad Jones on 08/08/2005 10:03am

      So what are the four lines you'd use in .NET?

      Reply
    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Between contractors and employees, there is a great deal of risk surrounding the privacy and security of sensitive data in the healthcare industry. When a breach is suspected, the law requires that healthcare organizations quickly launch an investigation to find the source and scope of the issue. These investigations can be quite costly, to say nothing of the penalties for the breach itself and the damage to reputation and brand. This white paper details new affordable software tools that can help healthcare …

  • As everyone scrambles to protect customers and consumers from the Heartbleed virus, there will be a variety of mitigating solutions offered up to address this pesky bug. There are a variety of points within the data path where solutions could be put into place to mitigate this (and similar) vulnerabilities and customers must choose the most strategic point in the network at which to deploy their selected mitigation. Read this white paper to learn the ins and outs of mitigating the risk of Heartbleed and the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds