【问题标题】:One-way encryption methods单向加密方法
【发布时间】:2011-07-13 00:57:48
【问题描述】:

这只是一个理论问题。我正在开始编写一个巨大的多服务器/多客户端网络视图。

问题:
不可逆加密或又名单向加密有哪些可能的方法?什么是最适合在我的案例和 .NET 中实现的?

谁能提供给我一个方法名称列表!

【问题讨论】:

  • md5 还是 sha256?真的有什么要说的吗?
  • 如果加密是不可逆的,那有什么意义呢? (区分散列算法...)
  • 只需要列表而不是比较!

标签: c# encryption cryptography


【解决方案1】:
byte[] data = new byte[DATA_SIZE];
byte[] result;
SHA256 shaM = new SHA256Managed();
result = shaM.ComputeHash(data);

Here 是概述,here 是具有标准功能的命名空间。只需查看HashAlgorithm 及其后代。

【讨论】:

  • 只需 8 分钟 ,,, :D 你是我的答案。无论如何谢谢
  • 是的,我只是等了 10 分钟才能将您的答案应用为完美:D
【解决方案2】:

您基本上想使用 MD5 或 SHA-256。哦,仅供参考,如果是一种方式,则称为 hash。 MSDN 文档广泛涵盖了这两种哈希。

【讨论】:

    【解决方案3】:

    正如其他人所提到的,md5 和 sha 是可以用于此的哈希算法。在你选择之前必须考虑的一件事是,它被“解密”是多么重要(散列不能在这个词的正常意义上被解密)。 MD5 和 SHA 的设计速度很快,这意味着创建包含大量哈希的彩虹表 (http://en.wikipedia.org/wiki/Rainbow_tables) 也会很快。以现代 GPU 的速度,每秒可以生成数亿个哈希值,这意味着可以相当快地暴力破解 MD5 和 SHA。

    如果您要存储密码之类的内容,最好使用设计缓慢的哈希算法,例如 bcrypt (http://bcrypt.codeplex.com/)

    【讨论】:

      【解决方案4】:

      对于此问题的任何新访问者,crackstation.net 有一个完整的 .NET 实现at the bottom,以及关于你应该做什么以及它是如何工作的相当详细的解释

      以下代码复制自crackstation.net,未经修改

      /* 
       * Password Hashing With PBKDF2 (http://crackstation.net/hashing-security.htm).
       * Copyright (c) 2013, Taylor Hornby
       * All rights reserved.
       *
       * Redistribution and use in source and binary forms, with or without 
       * modification, are permitted provided that the following conditions are met:
       *
       * 1. Redistributions of source code must retain the above copyright notice, 
       * this list of conditions and the following disclaimer.
       *
       * 2. Redistributions in binary form must reproduce the above copyright notice,
       * this list of conditions and the following disclaimer in the documentation 
       * and/or other materials provided with the distribution.
       *
       * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
       * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
       * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
       * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
       * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
       * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
       * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
       * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
       * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
       * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
       * POSSIBILITY OF SUCH DAMAGE.
       */
      
      using System;
      using System.Text;
      using System.Security.Cryptography;
      
      namespace PasswordHash
      {
          /// <summary>
          /// Salted password hashing with PBKDF2-SHA1.
          /// Author: havoc AT defuse.ca
          /// www: http://crackstation.net/hashing-security.htm
          /// Compatibility: .NET 3.0 and later.
          /// </summary>
          public class PasswordHash
          {
              // The following constants may be changed without breaking existing hashes.
              public const int SALT_BYTE_SIZE = 24;
              public const int HASH_BYTE_SIZE = 24;
              public const int PBKDF2_ITERATIONS = 1000;
      
              public const int ITERATION_INDEX = 0;
              public const int SALT_INDEX = 1;
              public const int PBKDF2_INDEX = 2;
      
              /// <summary>
              /// Creates a salted PBKDF2 hash of the password.
              /// </summary>
              /// <param name="password">The password to hash.</param>
              /// <returns>The hash of the password.</returns>
              public static string CreateHash(string password)
              {
                  // Generate a random salt
                  RNGCryptoServiceProvider csprng = new RNGCryptoServiceProvider();
                  byte[] salt = new byte[SALT_BYTE_SIZE];
                  csprng.GetBytes(salt);
      
                  // Hash the password and encode the parameters
                  byte[] hash = PBKDF2(password, salt, PBKDF2_ITERATIONS, HASH_BYTE_SIZE);
                  return PBKDF2_ITERATIONS + ":" +
                      Convert.ToBase64String(salt) + ":" +
                      Convert.ToBase64String(hash);
              }
      
              /// <summary>
              /// Validates a password given a hash of the correct one.
              /// </summary>
              /// <param name="password">The password to check.</param>
              /// <param name="correctHash">A hash of the correct password.</param>
              /// <returns>True if the password is correct. False otherwise.</returns>
              public static bool ValidatePassword(string password, string correctHash)
              {
                  // Extract the parameters from the hash
                  char[] delimiter = { ':' };
                  string[] split = correctHash.Split(delimiter);
                  int iterations = Int32.Parse(split[ITERATION_INDEX]);
                  byte[] salt = Convert.FromBase64String(split[SALT_INDEX]);
                  byte[] hash = Convert.FromBase64String(split[PBKDF2_INDEX]);
      
                  byte[] testHash = PBKDF2(password, salt, iterations, hash.Length);
                  return SlowEquals(hash, testHash);
              }
      
              /// <summary>
              /// Compares two byte arrays in length-constant time. This comparison
              /// method is used so that password hashes cannot be extracted from
              /// on-line systems using a timing attack and then attacked off-line.
              /// </summary>
              /// <param name="a">The first byte array.</param>
              /// <param name="b">The second byte array.</param>
              /// <returns>True if both byte arrays are equal. False otherwise.</returns>
              private static bool SlowEquals(byte[] a, byte[] b)
              {
                  uint diff = (uint)a.Length ^ (uint)b.Length;
                  for (int i = 0; i < a.Length && i < b.Length; i++)
                      diff |= (uint)(a[i] ^ b[i]);
                  return diff == 0;
              }
      
              /// <summary>
              /// Computes the PBKDF2-SHA1 hash of a password.
              /// </summary>
              /// <param name="password">The password to hash.</param>
              /// <param name="salt">The salt.</param>
              /// <param name="iterations">The PBKDF2 iteration count.</param>
              /// <param name="outputBytes">The length of the hash to generate, in bytes.</param>
              /// <returns>A hash of the password.</returns>
              private static byte[] PBKDF2(string password, byte[] salt, int iterations, int outputBytes)
              {
                  Rfc2898DeriveBytes pbkdf2 = new Rfc2898DeriveBytes(password, salt);
                  pbkdf2.IterationCount = iterations;
                  return pbkdf2.GetBytes(outputBytes);
              }
          }
      }
      

      注意几点:

      • 原帖和OWASP guidelines都建议使用HMAC加强加密
      • 上述实现中的迭代次数可能有点少。 OWASPthis answer 建议使用自我平衡的工作负载
      • 上述实现中的盐大小设置为 24,但如果我正确阅读 this answer,它不应该超过 20。也就是说,我不是密码学专家,所以我真的不能说是否该声明是真是假

      【讨论】:

      • 在比较本质上是随机字节数组的哈希时,为什么需要使用恒定时间比较?
      猜你喜欢
      • 2011-10-18
      • 2013-04-14
      • 2010-10-31
      • 1970-01-01
      • 2016-01-10
      • 2012-02-27
      • 2011-04-23
      • 1970-01-01
      • 2013-11-01
      相关资源
      最近更新 更多