?
Solved

encrypt connection string in Enterprise Library 2006

Posted on 2007-03-29
4
Medium Priority
?
486 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
[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
4 Comments
 
LVL 41

Accepted Solution

by:
graye earned 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Suggested Courses

770 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