Decrypt / Encrypt : Cryptography « Development « ASP.NET Tutorial






<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="Default_aspx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False"
            DataKeyNames="au_id">
            <Columns>
                <asp:BoundField DataField="au_id" HeaderText="au_id" ReadOnly="True" SortExpression="au_id" />
                <asp:BoundField DataField="au_lname" HeaderText="au_lname" SortExpression="au_lname" />
                <asp:BoundField DataField="au_fname" HeaderText="au_fname" SortExpression="au_fname" />
                <asp:BoundField DataField="phone" HeaderText="phone" SortExpression="phone" />
                <asp:BoundField DataField="address" HeaderText="address" SortExpression="address" />
                <asp:BoundField DataField="city" HeaderText="city" SortExpression="city" />
                <asp:BoundField DataField="state" HeaderText="state" SortExpression="state" />
                <asp:BoundField DataField="zip" HeaderText="zip" SortExpression="zip" />
                <asp:CheckBoxField DataField="contract" HeaderText="contract" SortExpression="contract" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
            SelectCommand="SELECT * FROM [authors]"></asp:SqlDataSource>
        &nbsp;
        <br />
    
    </div>
    </form>
</body>
</html>

File: Default.aspx.vb

Imports System.Configuration
'Imports System.Web
'Imports System.Web.Security
Imports System.Web.Security
Partial Class Default_aspx
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Load

        'Encrypt("DataProtectionConfigurationProvider")
        '--or--
        Decrypt()

        'Encrypt("RSAProtectedConfigurationProvider")
        ' Decrypt()
        'AddConnString()
        '---retrieve the newly added string
        Dim connect As String = _
           ConfigurationManager.ConnectionStrings _
           ("PubsConnectionString").ConnectionString
        Response.Write(connect)

    End Sub


    Public Sub Encrypt(ByVal protectionProvider As String)
        Dim config As Configuration = _
           System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration( _
           Request.ApplicationPath)

        Dim section As ConfigurationSection = config.Sections("connectionStrings")

        If Not section.SectionInformation.IsProtected Then
            section.SectionInformation.ProtectSection(protectionProvider)
            config.Save()
        End If
    End Sub

    Public Sub Decrypt()
        Dim config As Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration( _
           Request.ApplicationPath)
        Dim section As ConfigurationSection = config.Sections("connectionStrings")
        section.SectionInformation.UnProtectSection()
        config.Save()
    End Sub


    Public Sub AddConnString()
        Dim config As Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration( _
               Request.ApplicationPath)
        config.ConnectionStrings.ConnectionStrings.Add _
           (New ConnectionStringSettings("NorthwindConnectionString", _
           "server=localhost;database=northwind;integrated security=true"))
        config.Save()

       
    End Sub
End Class








9.9.Cryptography
9.9.1.Generate Keys
9.9.2.Cryptography.SHA1Managed
9.9.3.Decrypt / Encrypt