database connection string encryption and decryption

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Srinivasa Reddy K Ganji

    #1

    database connection string encryption and decryption

    Hi

    I want to encrypt the database connection string and add it to web.config
    file. Before connecting to the database I want to decrypt it.

    Can anyone suggest me how to implement this (c#)

    Regards,

    Reddy


  • Gary Varga

    #2
    database connection string encryption and decryption

    Give this a whirl:

    'Retrieve a connection to the database
    Public Shared Function Get_Connection( ) As
    System.Data.Sql Client.SqlConne ction

    Get_Connection = New
    System.Data.Sql Client.SqlConne ction()
    Get_Connection. ConnectionStrin g = Decrypt
    (System.Configu ration.Configur ationSettings.A ppSettings
    (CONNECTION_STR ING))
    Get_Connection. Open()

    End Function

    Public Shared Function Encrypt(ByVal Clear_Text As
    String) As String

    'Encrypt the text
    Encrypt = Convert.ToBase6 4String(Transfo rm
    (System.Text.En coding.Default. GetBytes(Clear_ Text),
    Get_Encryption_ Engine().Create Encryptor))

    End Function

    Public Shared Function Decrypt(ByVal Cipher_Text As
    String) As String

    'Decrypt the text
    Decrypt = System.Text.Enc oding.Default.G etString
    (Transform(Conv ert.FromBase64S tring(Cipher_Te xt),
    Get_Encryption_ Engine().Create Decryptor))

    End Function

    Private Shared Function Get_Encryption_ Engine() As
    System.Security .Cryptography.S ymmetricAlgorit hm

    'Setup the cryptographic service to use
    Get_Encryption_ Engine = New
    System.Security .Cryptography.R ijndaelManaged( )
    Get_Encryption_ Engine.Mode =
    System.Security .Cryptography.C ipherMode.CBC
    Get_Encryption_ Engine.Key =
    Convert.FromBas e64String
    ("U1fknVDCPQWER TYGZfRqvAYCK7gF pUukYKOqsCuN8XU =")
    Get_Encryption_ Engine.IV =
    Convert.FromBas e64String("vEQW ERTYRMrovjV+NXo s5g==")

    End Function

    Private Shared Function Transform(ByVal Source() As
    Byte, ByVal Transformer As
    System.Security .Cryptography.I CryptoTransform ) As Byte()

    'Transform the source
    Dim stream As New System.IO.Memor yStream()
    Dim cryptographic_s tream As
    System.Security .Cryptography.C ryptoStream = New
    System.Security .Cryptography.C ryptoStream(str eam,
    Transformer,
    System.Security .Cryptography.C ryptoStreamMode .Write)
    cryptographic_s tream.Write(Sou rce, 0,
    Source.Length)

    'Flush the buffer and release stream resources
    cryptographic_s tream.FlushFina lBlock()
    cryptographic_s tream.Close()

    'Return the data
    Transform = stream.ToArray( )

    End Function

    HTH,
    Gaz
    [color=blue]
    >-----Original Message-----
    >Hi
    >
    >I want to encrypt the database connection string and add[/color]
    it to web.config[color=blue]
    >file. Before connecting to the database I want to[/color]
    decrypt it.[color=blue]
    >
    >Can anyone suggest me how to implement this (c#)
    >
    >Regards,
    >
    >Reddy
    >
    >
    >.
    >[/color]

    Comment

    Working...