ASP.NET Tutorial/Development/Cryptography
Cryptography.SHA1Managed
<source lang="csharp">
<%@ Page %> <script Language="vb" runat="server"> Private Sub Page_Load(sender As Object, e As System.EventArgs)
Dim clearText As String = "Try not. Do. Or do not. There is no try." Dim encoder As New System.Text.UTF8Encoding() Response.Write(("Clear Text: " + clearText))Response.Write("
")
Dim sha As New System.Security.Cryptography.SHA1Managed() Dim input As Byte() = encoder.GetBytes(clearText) Dim output As Byte() = sha.ruputeHash(input) Response.Write(("Encrypted Text: " + encoder.GetString(output)))
End Sub </script></source>
Decrypt / Encrypt
<source lang="csharp">
<%@ 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">
<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>
</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</source>
Generate Keys
<source lang="csharp">
<%@ Page Language="C#" %> <%@ Import Namespace="System.Security.Cryptography" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">
void Page_Load() { lblAES.Text = GetSequence(64); lblSHA1.Text = GetSequence(128); } private string GetSequence(int length) { byte[] buffer = new byte[length/2]; RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider(); provider.GetBytes(buffer); StringBuilder builder = new StringBuilder(length); for (int i = 0; i < buffer.Length; i++) builder.Append(string.Format("{0:X2}", buffer[i])); return builder.ToString(); }
</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>Generate Keys</title>
</head> <body>
<form id="form1" runat="server">
AES: <asp:Label id="lblAES" Runat="server" />
SHA1: <asp:Label id="lblSHA1" Runat="server" />
</form>
</body> </html></source>