Solved

Password Encryption In VB.NET

Posted on 2010-09-11
2
678 Views
Last Modified: 2012-08-25
Hi,

I have to insert encrypted password in MS access data base using VB.net application.Later Once insert the value - I should able to decrypt it also.

Please provide the sample code for that

Thanks

0
Comment
Question by:futurebuilders
2 Comments
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 250 total points
ID: 33654585
0
 
LVL 7

Assisted Solution

by:klakkas
klakkas earned 250 total points
ID: 33660627
This is the Class I use.

The procedure is simple:

Using the encKey you want, encrypt the password in your application.
Save the result in the Database (string value).
Retreive the string from the DB.
Decrypt it in the applciation using the same key.
Imports System.Security.Cryptography

Imports System.IO



Public ClassEncryptionHelper

    Private Shared myEncKey As String = "Use An EnCryPTION KeY"

    Private Shared key() As Byte = {}

    Private Shared IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}



    Public Shared Function Decrypt(ByVal stringToDecrypt As String, Optional ByVal sEncryptionKey As String = "") As String

        If sEncryptionKey = "" Then sEncryptionKey = myEncKey

        Dim inputByteArray(stringToDecrypt.Length) As Byte

        Try

            key = System.Text.Encoding.UTF8.GetBytes(Left(sEncryptionKey, 8))

            Dim des As New DESCryptoServiceProvider()

            inputByteArray = Convert.FromBase64String(stringToDecrypt)

            Dim ms As New MemoryStream()

            Dim cs As New CryptoStream(ms, des.CreateDecryptor(key, IV), _

                CryptoStreamMode.Write)

            cs.Write(inputByteArray, 0, inputByteArray.Length)

            cs.FlushFinalBlock()

            Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8

            Return encoding.GetString(ms.ToArray(), 0, Convert.ToInt32(ms.Length))

        Catch e As Exception

            Return e.Message

        End Try

    End Function



    Public Shared Function Encrypt(ByVal stringToEncrypt As String, Optional ByVal sEncryptionKey As String = "") As String

        If SEncryptionKey = "" Then SEncryptionKey = myEncKey

        Try

            key = System.Text.Encoding.UTF8.GetBytes(Left(SEncryptionKey, 8))

            Dim des As New DESCryptoServiceProvider()

            Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes( _

                stringToEncrypt)

            Dim ms As New MemoryStream()

            Dim cs As New CryptoStream(ms, des.CreateEncryptor(key, IV), _

                CryptoStreamMode.Write)

            cs.Write(inputByteArray, 0, inputByteArray.Length)

            cs.FlushFinalBlock()

            Return Convert.ToBase64String(ms.ToArray())

        Catch e As Exception

            Return e.Message

        End Try

    End Function



End Class

Open in new window

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

11 Experts available now in Live!

Get 1:1 Help Now