0

我似乎無法找到我應該如何使用sha1通過成員資格提供商解密加密密碼。asp.net加密會員密碼和用戶名檢索

我不能在這裏使用.GetPassword()方法,因爲我從sqldatasource中檢索值並將它們放到gridview中。

這裏的GridView控件:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
     DataKeyNames="UserId" DataSourceID="SqlDataSource1" 
     EmptyDataText="There are no data records to display." 
     OnSelectedIndexChanged="GridView1_SelectedIndexChanged" > 
     <Columns> 
      <asp:CommandField ShowDeleteButton="True" ButtonType="Button" /> 
      <asp:TemplateField HeaderText="Block users"> 
       <ItemTemplate> 
        <asp:Button runat="server" ID="btnBlock" CommandName="Block" CommandArgument='<%# Eval("UserId") %>' 
         Text="Block" OnClick="btnBlock_Click" Visible='<%# !Convert.ToBoolean(Eval("IsLockedOut")) %>' /> 
        <asp:Button runat="server" ID="btnDeblock" CommandName="Deblock" CommandArgument='<%# Eval("UserId") %>' 
         Text="Deblock" OnClick="btnBlock_Click" Visible='<%# Convert.ToBoolean(Eval("IsLockedOut")) %>' /> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="Username"> 
       <ItemTemplate> 
        <asp:Label ID="UserId" runat="server" Text='<%# Bind("UserId") %>' OnDataBinding="Decrypt" /> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:BoundField DataField="UserId" HeaderText="User id" ReadOnly="True" 
       SortExpression="UserId" /> 
      <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" /> 
      <asp:BoundField DataField="LastLoginDate" HeaderText="Last login" 
       SortExpression="LastLoginDate" /> 
      <asp:CheckBoxField DataField="IsLockedOut" HeaderText="Locked" 
       SortExpression="IsLockedOut" /> 
      <asp:BoundField DataField="FailedPasswordAttemptCount" 
       HeaderText="Failed logins" 
       SortExpression="FailedPasswordAttemptCount" /> 
      <asp:BoundField DataField="Comment" HeaderText="Comments" 
       SortExpression="Comment" /> 
     </Columns> 
    </asp:GridView> 

我已經更換了綁定列在一個TemplateField一個ItemTemplate。 itemtemplate中的標籤綁定到用戶名,標籤也有一個OnDataBind =「Decrypt」,它應該解密標籤的Text屬性中的值。 我一直在嘗試我在網上找到的幾個例子(甚至從這個論壇),但我對.net的理解並不是那麼棒。 下面是我在解密()偵聽器嘗試:

public void Decrypt(object sender, EventArgs e) 
{ 
    Label lbl = (Label)sender; 
    string decrypted = string.Empty; 
    UTF8Encoding encode = new UTF8Encoding(); 
    Decoder Decode = encode.GetDecoder(); 
    byte[] todecode_byte = Convert.FromBase64String(lbl.Text); 
    int charCount = Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length); 
    char[] decoded_char = new char[charCount]; 
    Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0); 
    decrypted = new String(decoded_char); 
    lbl.Text = decrypted; 
} 

我一直想把你的用戶名第一解密,我想這是密碼的方法相同。 爲了eleminate進一步的問題,這是我在web.config中

<membership defaultProvider="MembershipProvider"> 
    <providers> 
    <clear/> 
    <add name="MembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="GustaafConnectionString" 
     applicationName="Gustaaf" enablePasswordRetrieval="true" enablePasswordReset="false" requiresQuestionAndAnswer="true" 
     requiresUniqueEmail="false" passwordFormat="Encrypted"/> 
    </providers> 
</membership> 
<machineKey validationKey="..." decryptionKey="..." validation="SHA1" decryption="AES"/> 

回答

2

SHA1是一個哈希算法,而不是一個加密算法,哈希值是通過定義一個方法的設置,所以它們不能被撤消。因此你永遠不會使用sha來「解密」任何東西,更不用說哈希了。

您的數據似乎被AES加密,而不是sha。你也在編碼和加密混淆。 下面是有關在.NET中使用AES一些資料:http://msdn.microsoft.com/en-us/library/system.security.cryptography.aes.aspx

編碼是所有有關解釋字節的數據作爲在一個方案中或另一個(ASCII,unicode的,UCT-8),以便一旦有數據解密,可能你的字符必須將其編碼爲一個字符串以供顯示,但這對解密是次要的。

ADDENDUM:您可能無法避免使用membership.GetPassword(),因爲您的成員資格DB實現中使用的加密密鑰可能無法檢索。你有沒有考慮過使用對象數據源呢?那麼你可以使用.GetPassword()預先填充代碼中的條目列表並將它們綁定到網格。