database connection string encryption and decryption

Discussion in 'ASP .Net' started by Srinivasa Reddy K Ganji, Jul 18, 2003.

  1. 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
    Srinivasa Reddy K Ganji, Jul 18, 2003
    #1
    1. Advertising

  2. Srinivasa Reddy K Ganji

    Gary Varga Guest

    Give this a whirl:

    'Retrieve a connection to the database
    Public Shared Function Get_Connection() As
    System.Data.SqlClient.SqlConnection

    Get_Connection = New
    System.Data.SqlClient.SqlConnection()
    Get_Connection.ConnectionString = Decrypt
    (System.Configuration.ConfigurationSettings.AppSettings
    (CONNECTION_STRING))
    Get_Connection.Open()

    End Function

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

    'Encrypt the text
    Encrypt = Convert.ToBase64String(Transform
    (System.Text.Encoding.Default.GetBytes(Clear_Text),
    Get_Encryption_Engine().CreateEncryptor))

    End Function

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

    'Decrypt the text
    Decrypt = System.Text.Encoding.Default.GetString
    (Transform(Convert.FromBase64String(Cipher_Text),
    Get_Encryption_Engine().CreateDecryptor))

    End Function

    Private Shared Function Get_Encryption_Engine() As
    System.Security.Cryptography.SymmetricAlgorithm

    'Setup the cryptographic service to use
    Get_Encryption_Engine = New
    System.Security.Cryptography.RijndaelManaged()
    Get_Encryption_Engine.Mode =
    System.Security.Cryptography.CipherMode.CBC
    Get_Encryption_Engine.Key =
    Convert.FromBase64String
    ("U1fknVDCPQWERTYGZfRqvAYCK7gFpUukYKOqsCuN8XU=")
    Get_Encryption_Engine.IV =
    Convert.FromBase64String("vEQWERTYRMrovjV+NXos5g==")

    End Function

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

    'Transform the source
    Dim stream As New System.IO.MemoryStream()
    Dim cryptographic_stream As
    System.Security.Cryptography.CryptoStream = New
    System.Security.Cryptography.CryptoStream(stream,
    Transformer,
    System.Security.Cryptography.CryptoStreamMode.Write)
    cryptographic_stream.Write(Source, 0,
    Source.Length)

    'Flush the buffer and release stream resources
    cryptographic_stream.FlushFinalBlock()
    cryptographic_stream.Close()

    'Return the data
    Transform = stream.ToArray()

    End Function

    HTH,
    Gaz

    >-----Original Message-----
    >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, Jul 18, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    1,607
    Sushant Bhatia
    May 3, 2005
  2. Aidan Diffey
    Replies:
    11
    Views:
    9,486
    Chris Uppal
    Oct 24, 2005
  3. dj_uncas
    Replies:
    12
    Views:
    19,643
    dj_uncas
    Nov 24, 2008
  4. Jean-Claude Neveu

    Python and PHP encryption/decryption

    Jean-Claude Neveu, Aug 19, 2009, in forum: Python
    Replies:
    3
    Views:
    760
    Piet van Oostrum
    Aug 21, 2009
  5. Henrik Bruhn

    encryption and decryption using SoapExtension

    Henrik Bruhn, Feb 18, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    142
    Henrik Bruhn
    Feb 18, 2004
Loading...

Share This Page