Solved

encrypt connection string in Enterprise Library 2006

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

If you thought ransomware was bad, think again! Doxware has the potential to be even more damaging.
Encryption for Business Encryption (https://en.wikipedia.org/wiki/Encryption) ensures the safety of our data when sending emails. In most cases, to read an encrypted email you must enter a secret key that will enable you to decrypt the email. T…
The viewer will learn how to use and create new code templates 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 …

738 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