Solved

Password Encryption In VB.NET

Posted on 2010-09-11
2
683 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

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
DataGridview Currency Formating? 22 53
Search and display values in the textbox using VB.net and SQL 19 33
VB.net/WinForms: adjust Screen Resolution 6 42
Help with Syntax 9 28
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 When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

895 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

13 Experts available now in Live!

Get 1:1 Help Now