如何解密ASP.Net的SelectCommand中的值?

时间:2018-01-29 16:35:19

标签: asp.net sql-server password-encryption

目前我在SQL Server中测试加密,加密工作正常。我能够将加密值插入到我的表中,但是我在选择解密值时遇到了困难。

如果我使用

SelectCommand="SELECT [password], [username], [id] FROM [encryptionTest]" 

作为我的SelectCommand,我从表中获取加密值。使用ASP.Net中的SelectCommand选择解密值的正确语法是什么?

当我在选择SelectQuery时在属性中的SelectCommand上使用查询构建器时,下面的SelectCommand会返回正确的值但是当我尝试加载aspx页面时浏览器,它会导致运行时错误。

QueryBuilder Showing Decryption Values

QueryBuilder Showing Encrypted Values

如果有帮助,我会使用Visual Studio 2015.

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" 
         AutoEventWireup="true" CodeFile="encryptionTest.aspx.cs"  
         Inherits="IT_encryptionTest" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" runat="server" contentplaceholderid="MainContent">
        <dx:ASPxGridView ID="ASPxGridView2" runat="server" OnCustomErrorText="ASPxGridView2_CustomErrorText" AutoGenerateColumns="False" DataSourceID="SqlDataSourceEncryptionTest" KeyFieldName="id">
            <Columns>
                <dx:GridViewCommandColumn ShowDeleteButton="True" ShowEditButton="True" ShowNewButtonInHeader="True" VisibleIndex="0">
                </dx:GridViewCommandColumn>
                <dx:GridViewDataTextColumn FieldName="password" VisibleIndex="1">
                </dx:GridViewDataTextColumn>
                <dx:GridViewDataTextColumn FieldName="username" VisibleIndex="2">
                </dx:GridViewDataTextColumn>
                <dx:GridViewDataTextColumn FieldName="id" VisibleIndex="3" ReadOnly="True">
                    <EditFormSettings Visible="False" />
                </dx:GridViewDataTextColumn>
            </Columns>
        </dx:ASPxGridView>
    <asp:SqlDataSource ID="SqlDataSourceEncryptionTest" runat="server" ConnectionString="<%$ ConnectionStrings:GHF_SQLConnectionString %>" 
        SelectCommand="OPEN SYMMETRIC KEY Pass_Key_01 DECRYPTION BY CERTIFICATE TestCert01; SELECT CONVERT(nvarchar, DecryptByKey(password)) AS Password, [username], [id] FROM [encryptionTest]" 
        DeleteCommand="DELETE FROM [encryptionTest] WHERE [id] = @id" 
        InsertCommand="OPEN SYMMETRIC KEY Pass_Key_01 DECRYPTION BY CERTIFICATE TestCert01; INSERT INTO [encryptionTest] ([password], [username]) VALUES (EncryptByKey(Key_GUID('Pass_Key_01'),@password), @username)" 
        UpdateCommand="UPDATE [encryptionTest] SET [password] = @password, [username] = @username WHERE [id] = @id">
        <DeleteParameters>
            <asp:Parameter Name="id" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="password" Type="String" />
            <asp:Parameter Name="username" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="password" Type="String" />
            <asp:Parameter Name="username" Type="String" />
            <asp:Parameter Name="id" Type="Int32" />
        </UpdateParameters>
        </asp:SqlDataSource>
</asp:Content>

0 个答案:

没有答案