Solved

encrypt connection string in Enterprise Library 2006

Posted on 2007-03-29
4
472 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get the web content with different encodings 2 45
Regex validation 2 24
Moving code from one TFS branch to another 3 25
Data encryption when using public Wi-Fi 4 26
There are many Password Managers (PM) out there to choose from. PM's can help with your password habits and routines, but they should not be a crutch you rely on too heavily. I also have an article for company/enterprise PM's.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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