技术开发 频道

Duwamish密码分析篇(一)

  2,调用BusinessFacade\CustomerSystem类,对散列执行Salt运算。

  到目前为止,散列算法暴露出来的问题之一是,如果两个用户碰巧使用相同的密码,那么散列值将完全相同。如果黑客看到您存储密码的表格,会从中找到规律并明白您很可能使用了常见的词语,然后黑客会开始词典攻击以确定这些密码。要确保任何两个用户密码的散列值都不相同,一种方法是在加密密码之前,在每个用户的密码中添加一个唯一的值。这个唯一值称为值(Salt)。
 
  虽然对密码执行散列运算是一个好的开端,但若要增加免受潜在攻击的安全性,则可以对密码散列执行 Salt 运算。Salt 就是在已执行散列运算的密码中插入的一个随机数字。这一策略有助于阻止潜在的攻击者利用预先计算的字典攻击。字典攻击是攻击者使用密钥的所有可能组合来破解密码的攻击。当您使用 Salt 值使散列运算进一步随机化后,攻击者将需要为每个 Salt 值创建一个字典,这将使攻击变得非常复杂且成本极高。
 
  Salt 值随散列存储在一起,并且未经过加密。所存储的 Salt 值可以在随后用于密码验证。
 
  下面看看Duwamish 7.0中是如何实现Salt运算:

  (1)BusinessFacade\CustomerSystem class中Create Customer()方法

public bool CreateCustomer(String emailAddress, byte [] password,String name,String address, String country,String phoneNumber,String fax, out CustomerData custData) { // create a salted password byte [] saltedPassword = CreateDbPassword(password); // // Create a new row // custData = new CustomerData(); DataTable table=custData.Tables[CustomerData.CUSTOMERS_TABLE]; DataRow row = table.NewRow(); // // Fill input data into new row // row[CustomerData.EMAIL_FIELD] = emailAddress; row[CustomerData.PASSWORD_FIELD] = saltedPassword; row[CustomerData.NAME_FIELD] = name; row[CustomerData.ADDRESS_FIELD] = address; row[CustomerData.COUNTRY_FIELD] = country; row[CustomerData.PHONE_FIELD] = phoneNumber; row[CustomerData.FAX_FIELD] = fax; // // Add it to the table // table.Rows.Add(row); // 调用Business rules tier的Customer Class // Insert the customer using the business rules // return (new Customer()).Insert(custData); }
  首先调用Facade\CustomerSystem 类的私有方法CreateDbPassword(),获取对散列执行Salt运算结果(长度为24个字节的byte数组),然后调用Business rules tier中的Customer class的Insert()方法,将用户信息,包括密码存放在数据库中。
 
  (2)Facade\CustomerSystem 类的私有方法 CreateDbPassword()
// create salted password to save in Db private byte [] CreateDbPassword(byte[] unsaltedPassword) { //Create a salt value byte[] saltValue = new byte[saltLength]; RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); //用加密型强随机字节填充的数组 rng.GetBytes(saltValue); return CreateSaltedPassword(saltValue, unsaltedPassword); }
  上述代码片断使用 .NET Framework 类 RNGCryptoServiceProvider 创建一个随机的数字字符串。RNG 表示随机数生成器。该类可以创建一个任意长度的随机字节数组,长度由您指定。您可以使用此随机字节数组作为散列算法的Salt值。要采用这种方法,必须安全地存储该Salt值。saltLength=4(常量),Duwamish 7 示例用RNGCryptoServiceProvider创建一个 4 字节 Salt 值。然后调用Facade\CustomerSystem 类的私有方法CreateSaltedPassword(),获取对散列执行Salt运算后的结果。
 
  3,调用BusinessRules\Customer类的Insert()方法。
 
  Insert()方法根据传入的CustomerData对象,验证数据的合法性,然后调用Data Access tier的Customers对象的InsertCustomer()方法。
 
  4,调用DataAccess\Customers类的InsertCustomer()方法。
 
  InsertCustomer()方法根据传入的CustomerData对象,调用Database端的Stored Procedure,执行真正的数据库insert操作。可以观察到Duwamish7 Database中Customers表的Password字段类型为binary且长度为24。
 
  下一篇POST《Duwamish密码分析篇(二)》将分析【用户登录】流程的密码验证过程。

0
相关文章