1. <legend id='sr2wi'><style id='sr2wi'><dir id='sr2wi'><q id='sr2wi'></q></dir></style></legend>
    <tfoot id='sr2wi'></tfoot>
      <bdo id='sr2wi'></bdo><ul id='sr2wi'></ul>
      <i id='sr2wi'><tr id='sr2wi'><dt id='sr2wi'><q id='sr2wi'><span id='sr2wi'><b id='sr2wi'><form id='sr2wi'><ins id='sr2wi'></ins><ul id='sr2wi'></ul><sub id='sr2wi'></sub></form><legend id='sr2wi'></legend><bdo id='sr2wi'><pre id='sr2wi'><center id='sr2wi'></center></pre></bdo></b><th id='sr2wi'></th></span></q></dt></tr></i><div id='sr2wi'><tfoot id='sr2wi'></tfoot><dl id='sr2wi'><fieldset id='sr2wi'></fieldset></dl></div>

      <small id='sr2wi'></small><noframes id='sr2wi'>

    1. Rfc2898/PBKDF2 与 SHA256 作为 C# 中的摘要

      时间:2023-06-02
    2. <i id='iJ7tb'><tr id='iJ7tb'><dt id='iJ7tb'><q id='iJ7tb'><span id='iJ7tb'><b id='iJ7tb'><form id='iJ7tb'><ins id='iJ7tb'></ins><ul id='iJ7tb'></ul><sub id='iJ7tb'></sub></form><legend id='iJ7tb'></legend><bdo id='iJ7tb'><pre id='iJ7tb'><center id='iJ7tb'></center></pre></bdo></b><th id='iJ7tb'></th></span></q></dt></tr></i><div id='iJ7tb'><tfoot id='iJ7tb'></tfoot><dl id='iJ7tb'><fieldset id='iJ7tb'></fieldset></dl></div>

    3. <tfoot id='iJ7tb'></tfoot>

      <small id='iJ7tb'></small><noframes id='iJ7tb'>

      <legend id='iJ7tb'><style id='iJ7tb'><dir id='iJ7tb'><q id='iJ7tb'></q></dir></style></legend>

            <tbody id='iJ7tb'></tbody>
              <bdo id='iJ7tb'></bdo><ul id='iJ7tb'></ul>
              1. 本文介绍了Rfc2898/PBKDF2 与 SHA256 作为 C# 中的摘要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我想在 c# 中使用 Rfc2898 来派生密钥.我还需要使用 SHA256 作为 Rfc2898 的摘要.我找到了类 Rfc2898DeriveBytes,但它使用 SHA-1,我看不出有办法让它使用不同的摘要.

                I want to use Rfc2898 in c# to derive a key. I also need to use SHA256 as Digest for Rfc2898. I found the class Rfc2898DeriveBytes, but it uses SHA-1 and I don't see a way to make it use a different digest.

                有没有办法在 c# 中使用 Rfc2898 和 SHA256 作为摘要(没有从头开始实现它)?

                Is there a way to use Rfc2898 in c# with SHA256 as digest (short of implementing it from scratch)?

                推荐答案

                查看 Bruno Garcia 的回答.

                See Bruno Garcia's answer.

                Carsten:请接受那个答案而不是这个答案.

                Carsten: Please accept that answer rather than this one.

                在我开始回答这个问题时,Rfc2898DeriveBytes 无法配置为使用不同的哈希函数.但与此同时,它也得到了改进.见布鲁诺加西亚的回答.以下函数可用于生成用户提供的密码的哈希版本,以存储在数据库中用于身份验证.

                At the time I started this answer, Rfc2898DeriveBytes was not configurable to use a different hash function. In the meantime, though, it has been improved; see Bruno Garcia's answer. The following function can be used to generate a hashed version of a user-provided password to store in a database for authentication purposes.

                对于旧 .NET 框架的用户,这仍然很有用:

                For users of older .NET frameworks, this is still useful:

                // NOTE: The iteration count should
                // be as high as possible without causing
                // unreasonable delay.  Note also that the password
                // and salt are byte arrays, not strings.  After use,
                // the password and salt should be cleared (with Array.Clear)
                
                public static byte[] PBKDF2Sha256GetBytes(int dklen, byte[] password, byte[] salt, int iterationCount){
                    using(var hmac=new System.Security.Cryptography.HMACSHA256(password)){
                        int hashLength=hmac.HashSize/8;
                        if((hmac.HashSize&7)!=0)
                            hashLength++;
                        int keyLength=dklen/hashLength;
                        if((long)dklen>(0xFFFFFFFFL*hashLength) || dklen<0)
                            throw new ArgumentOutOfRangeException("dklen");
                        if(dklen%hashLength!=0)
                            keyLength++;
                        byte[] extendedkey=new byte[salt.Length+4];
                        Buffer.BlockCopy(salt,0,extendedkey,0,salt.Length);
                        using(var ms=new System.IO.MemoryStream()){
                            for(int i=0;i<keyLength;i++){
                                extendedkey[salt.Length]=(byte)(((i+1)>>24)&0xFF);
                                extendedkey[salt.Length+1]=(byte)(((i+1)>>16)&0xFF);
                                extendedkey[salt.Length+2]=(byte)(((i+1)>>8)&0xFF);
                                extendedkey[salt.Length+3]=(byte)(((i+1))&0xFF);
                                byte[] u=hmac.ComputeHash(extendedkey);
                                Array.Clear(extendedkey,salt.Length,4);
                                byte[] f=u;
                                for(int j=1;j<iterationCount;j++){
                                    u=hmac.ComputeHash(u);
                                    for(int k=0;k<f.Length;k++){
                                        f[k]^=u[k];
                                    }
                                }
                                ms.Write(f,0,f.Length);
                                Array.Clear(u,0,u.Length);
                                Array.Clear(f,0,f.Length);
                            }
                            byte[] dk=new byte[dklen];
                            ms.Position=0;
                            ms.Read(dk,0,dklen);
                            ms.Position=0;
                            for(long i=0;i<ms.Length;i++){
                                ms.WriteByte(0);
                            }
                            Array.Clear(extendedkey,0,extendedkey.Length);
                            return dk;
                        }
                    }
                

                这篇关于Rfc2898/PBKDF2 与 SHA256 作为 C# 中的摘要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何在 C# 中使用公钥和私钥加密技术 下一篇:C# BouncyCastle - 使用公钥/私钥的 RSA 加密

                相关文章

                最新文章

                    <legend id='q3qId'><style id='q3qId'><dir id='q3qId'><q id='q3qId'></q></dir></style></legend>
                  1. <i id='q3qId'><tr id='q3qId'><dt id='q3qId'><q id='q3qId'><span id='q3qId'><b id='q3qId'><form id='q3qId'><ins id='q3qId'></ins><ul id='q3qId'></ul><sub id='q3qId'></sub></form><legend id='q3qId'></legend><bdo id='q3qId'><pre id='q3qId'><center id='q3qId'></center></pre></bdo></b><th id='q3qId'></th></span></q></dt></tr></i><div id='q3qId'><tfoot id='q3qId'></tfoot><dl id='q3qId'><fieldset id='q3qId'></fieldset></dl></div>
                  2. <small id='q3qId'></small><noframes id='q3qId'>

                    • <bdo id='q3qId'></bdo><ul id='q3qId'></ul>
                    <tfoot id='q3qId'></tfoot>