【问题标题】:Deriving a crypto key from a password从密码派生加密密钥
【发布时间】:2009-01-13 11:51:03
【问题描述】:

我正在尝试使用 .net 2.0 PasswordDeriveBytes 类生成密钥。

  PasswordDeriveBytes pdb = new PasswordDeriveBytes("blahblahblah",null);
  byte[] iv = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
  byte[] key = pdb.CryptDeriveKey("TripleDES", "SHA1", 128, iv);

代码抛出“CryptographicException:指定的标志无效”。尝试执行上面的 CryptDeriveKey 方法时。我正在尝试加密 asp.net 中的数据库条目,这是我第一次尝试加密。如有任何帮助,我将不胜感激。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    根据 MSDN:

    "如果 keySize 参数设置为 0 位,则使用指定算法的默认密钥大小。"

    MSDN PasswordDeriveBytes.CryptDeriveKey Method

    PasswordDeriveBytes pdb = new PasswordDeriveBytes("blahblahblah",null);
    byte[] iv = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
    byte[] key = pdb.CryptDeriveKey("TripleDES", "SHA1", 0, iv);
    

    除非您有充分的理由指定尺寸,否则我建议将其保留为 0。

    【讨论】:

      【解决方案2】:

      TripleDES 的密钥长度为 192 位尝试:

      byte[] key = pdb.CryptDeriveKey("TripleDES", "SHA1", 192, iv);
      

      试试this link 示例代码

      【讨论】:

        【解决方案3】:

        您应该考虑使用Rfc2898DeriveBytes 而不是 PasswordDeriveBytes。

        【讨论】:

          猜你喜欢
          • 2019-08-13
          • 1970-01-01
          • 2015-10-06
          • 2020-12-22
          • 1970-01-01
          • 2010-12-02
          • 2012-04-10
          • 2017-10-28
          • 1970-01-01
          相关资源
          最近更新 更多