System.Security.Cryptography
命名空间中的加密类,请使用 Rfc2898DeriveBytes
类(@CodeInChaos 指出它取代了 PasswordDeriveBytes
)从密码派生一个密钥。SymmetricAlgorithm
类的 GenerateKey
方法。Rfc2898DeriveBytes
http://msdn.microsoft.com/en-us/library/system.security.cryptography.rfc2898derivebytes.aspx - CodesInChaosRfc2898DeriveBytes
是 PBKDF2, PasswordDeriveBytes
是 PBKDF1。我认为2比1更好。 - CodesInChaosPasswordDeriveBytes
中实现(扩展)它的方式,参见:http://stackoverflow.com/a/8459312/220643。 - poupou这取决于您如何处理密钥。
如果您自动生成密钥并通过某个密钥交换方法在某个渠道上交换密钥,则应使用像RNGCryptoServiceProvider
这样的强随机数生成器生成密钥。实际上.NET中的大多数密码都会自动生成随机密钥。
如果要使用某种用户输入的密码,则建议您使用Rfc2898DeriveBytes
类。关于Rfc2898DeriveBytes
也有一篇教程,可以在.NET安全博客上找到。