-----------------------------------------------------------
New Message on BDOTNET
-----------------------------------------------------------
From: dnyanesh
Message 3 in Discussion
Hi,
Encrypting connection string In order to encrypt above connection string we
will be using System.Convert class. We will build a small console application
that allows us to pass plain connection string as command line argument and
then displays encrypted version on the console.
The code looks like this:
Public Shared Sub Main(args() As String)
Dim data() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes(args(0))
Dim str As String = Convert.ToBase64String(data)
Console.WriteLine(str)
End Sub
Here, we are using System.Text.ASCIIEncoding.ASCII class to convert the
connection string to an array of bytes. This is necessary because the Convert
class function ToBase64String() expects array of bytes and then returns Base64
encoded version of it.
You can invoke above application (I created it as Base64Encrypter.exe) at
command prompt like this:
Base64Encrypter.exe "data source=.\vsdotnet;initial catalog=Northwind;user
id=sa;password=mypassword"
The output will be:
ZGF0YSBzb3VyY2U9Llx2c2RvdG5ldDtpbml0aWFsIGNhdGFsb2c9Tm9ydGh3aW5kO3VzZXIgaWQ9c2E7
cGFzc3dvcmQ9bXlwYXNzd29yZA==
You can now copy-paste this encoded version of the connection string in the
web.config. The new appSettings section will look like this:
<APPSETTINGS>
<ADD DIV < key="connectionstring">
value="ZGF0YSBzb3VyY2U9Llx2c2RvdG5ldDtpbml0aWFsIGNhdGFsb2c9Tm9ydGh3aW5kO3VzZXIgaWQ9c2E7
cGFzc3dvcmQ9bXlwYXNzd29yZA=="/>
</APPSETTINGS>
Reading the encrypted connection string back
Now, let us see how we can read the encrypted connection string and decrypt it
so that we can use it further.
Dim data() As Byte = Convert.FromBase64String
(ConfigurationSettings.AppSettings("connectionstring"))
str = System.Text.ASCIIEncoding.ASCII.GetString(data)
Here, we again used the Convert class and called its FromBase64String function.
This function accepts Base64 encoded string and returns a byte array. In order
to retrieve the appSetting value we used ConfigurationSettings class as shown
above. Finally, we used ASCII class again to convert the byte array to a
string.
Regards,
Dnyanesh (Dnyaneshwar Parkhe)
MCAD.NET, MCP,
http://dnyaneshwar.blog-city.com
-----------------------------------------------------------
To stop getting this e-mail, or change how often it arrives, go to your E-mail
Settings.
http://groups.msn.com/BDOTNET/_emailsettings.msnw
Need help? If you've forgotten your password, please go to Passport Member
Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help
For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact
If you do not want to receive future e-mail from this MSN group, or if you
received this message by mistake, please click the "Remove" link below. On the
pre-addressed e-mail message that opens, simply click "Send". Your e-mail
address will be deleted from this group's mailing list.
mailto:[EMAIL PROTECTED]