Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > database connection string encryption and decryption

Reply
Thread Tools

database connection string encryption and decryption

 
 
Srinivasa Reddy K Ganji
Guest
Posts: n/a
 
      07-18-2003
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


 
Reply With Quote
 
 
 
 
Gary Varga
Guest
Posts: n/a
 
      07-18-2003
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.AppSet tings
(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.Writ e)
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
>
>
>.
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python and PHP encryption/decryption Jean-Claude Neveu Python 3 08-21-2009 05:36 PM
Reg : Image Encryption and Decryption in PGP Deepa Software 0 07-13-2009 06:40 AM
Java string encryption/decryption dj_uncas Java 12 11-24-2008 01:55 AM
Encryption/Decryption on both Java and Delphi Aidan Diffey Java 11 10-24-2005 08:12 AM
encryption and decryption using SoapExtension Henrik Bruhn ASP .Net Web Services 0 02-18-2004 11:55 AM



Advertisments