Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Password Encryption In VB.NET

Posted on 2010-09-11
2
Medium Priority
?
719 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
[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 Comments
 
LVL 18

Accepted Solution

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

Assisted Solution

by:klakkas
klakkas earned 1000 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

610 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