Solved

encrypt connection string in Enterprise Library 2006

Posted on 2007-03-29
4
466 Views
Last Modified: 2013-11-07
i want to encrypt my connection string in web.config, i have gone through various articles like http://msdn.microsoft.com/msdnmag/issues/05/07/DataPoints/  but i couldn't find the Configuration Application Block in there. and there are no nodes like explained in this article.
0
Comment
Question by:aosexpert
4 Comments
 
LVL 41

Accepted Solution

by:
graye earned 125 total points
ID: 18822785
0
 
LVL 10

Expert Comment

by:adriankohws
ID: 18930411
Imports System.IO
Imports System.Security.Cryptography

Encrypting your ConnectionString ---
Dim NewStr() As Byte = Nothing
        'Streaming of Text, Encode as ASCII, then encrypt to array (Byte)
        If Not (ConnectionString = "") Then
            Dim eEncoding As New System.Text.ASCIIEncoding
            Dim byteArray() As Byte = eEncoding.GetBytes(EncryptText(ConnectionString))
            NewStr = byteArray
        End If



Decrypting ---
Dim ConnStr as string = ""
Dim Constr() As Byte = ConnectionString, Byte())
                For i = 0 To (Constr.Length - 1)
                    ConnStr = ConnStr & Chr(Constr(i))
                Next
            End If
 ConnStr = DecryptText(ConnStr )


----------------------------------------------------------------------------------------------------------
  ' Encrypt the text
    Public Shared Function EncryptText(ByVal strText As String) As String
        Return Encrypt(strText, "ΏΫ€Ǚ♫□₪Ω")
    End Function

    'Decrypt the text
    Public Shared Function DecryptText(ByVal strText As String) As String
        Return Decrypt(strText, "ΏΫ€Ǚ♫□₪Ω")
    End Function

    'The function used to encrypt the text
    Private Shared Function Encrypt(ByVal strText As String, ByVal strEncrKey _
             As String) As String
        Dim byKey() As Byte = {}
        Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}

        Try
            byKey = System.Text.Encoding.ASCII.GetBytes(Microsoft.VisualBasic.Left(strEncrKey, 8))
            Dim des As New DESCryptoServiceProvider()
            Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(strText)
            Dim ms As New MemoryStream()
            Dim cs As New CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write)
            cs.Write(inputByteArray, 0, inputByteArray.Length)
            cs.FlushFinalBlock()
            Return Convert.ToBase64String(ms.ToArray())
        Catch ex As Exception
            Return ex.Message
        End Try
    End Function

    'The function used to decrypt the text
    Private Shared Function Decrypt(ByVal strText As String, ByVal sDecrKey _
               As String) As String
        Dim byKey() As Byte = {}
        Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
        Dim inputByteArray(strText.Length) As Byte

        Try
            byKey = System.Text.Encoding.ASCII.GetBytes(Microsoft.VisualBasic.Left(sDecrKey, 8))
            Dim des As New DESCryptoServiceProvider()
            inputByteArray = Convert.FromBase64String(strText)
            Dim ms As New MemoryStream()
            Dim cs As New CryptoStream(ms, des.CreateDecryptor(byKey, 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())
        Catch ex As Exception
            Return ex.Message
        End Try
    End Function

This is the way to encrypted and decrypt strings to byte.  Hope it helps.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Cybersecurity has become the buzzword of recent years and years to come. The inventions of cloud infrastructure and the Internet of Things has made us question our online safety. Let us explore how cloud- enabled cybersecurity can help us with our b…
Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

707 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

19 Experts available now in Live!

Get 1:1 Help Now