仅使用密码的加密示例...无盐。有效吗?
我一直在研究创建用于 .NET 应用程序的加密/解密类。我一次又一次地读到除了秘密密码之外还需要盐。今天我遇到了一种仅使用单个密码的加密/解密方法。该代码使用的加密方法是否有问题,因为它似乎没有使用盐?
Public Shared Function EncryptString(ByRef input As String, ByRef password As String) As String
Dim RijndaelManagedObject As New RijndaelManaged
Dim crypto As ICryptoTransform, MD5Obj As New MD5CryptoServiceProvider
Dim EncryptedBytes As Byte()
Dim HashedBytes As Byte() = New ASCIIEncoding().GetBytes(password)
Dim PlainTextBytes As Byte() = New ASCIIEncoding().GetBytes(input)
RijndaelManagedObject.BlockSize = 128
RijndaelManagedObject.KeySize = 128
RijndaelManagedObject.Mode = CipherMode.ECB
RijndaelManagedObject.Padding = PaddingMode.Zeros
RijndaelManagedObject.Key = MD5Obj.ComputeHash(HashedBytes)
crypto = RijndaelManagedObject.CreateEncryptor()
EncryptedBytes = crypto.TransformFinalBlock(PlainTextBytes, 0, PlainTextBytes.Length)
If EncryptedBytes.Length > 0 Then
Return Convert.ToBase64String(EncryptedBytes)
Else
Return String.Empty()
End If
End Function
I have been doing some research on creating an encryption/decryption class for use in .NET application. Time after time I read that a salt was needed in addition to the secret password. Today I have come across an encryption/decryption method that only makes use of a single password. Is there something wrong with the encryption methods used by this code as it does not seem make use of a salt?
Public Shared Function EncryptString(ByRef input As String, ByRef password As String) As String
Dim RijndaelManagedObject As New RijndaelManaged
Dim crypto As ICryptoTransform, MD5Obj As New MD5CryptoServiceProvider
Dim EncryptedBytes As Byte()
Dim HashedBytes As Byte() = New ASCIIEncoding().GetBytes(password)
Dim PlainTextBytes As Byte() = New ASCIIEncoding().GetBytes(input)
RijndaelManagedObject.BlockSize = 128
RijndaelManagedObject.KeySize = 128
RijndaelManagedObject.Mode = CipherMode.ECB
RijndaelManagedObject.Padding = PaddingMode.Zeros
RijndaelManagedObject.Key = MD5Obj.ComputeHash(HashedBytes)
crypto = RijndaelManagedObject.CreateEncryptor()
EncryptedBytes = crypto.TransformFinalBlock(PlainTextBytes, 0, PlainTextBytes.Length)
If EncryptedBytes.Length > 0 Then
Return Convert.ToBase64String(EncryptedBytes)
Else
Return String.Empty()
End If
End Function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此代码有许多缺陷:
input
或password
不是 ASCII,则会发生静默降级。特别是非 asciiinput
将无法正确解密。This code has a number of flaws:
input
orpassword
are not ASCII a silent degradation occurs. In particular non asciiinput
won't be decrypted correctly.PaddingMode.Zeros
works. But it might not be possible to strip the padding since its length isn't encoded. So you might have some additional 0 bytes after decrypting.不,这没有什么问题。
对密码加盐是为了在存储这些哈希密码时防止彩虹表攻击。在这种情况下,密码将用于生成加密/解密密钥,并且不会被存储。
No, there's nothing wrong with this.
Salting passwords is to prevent rainbow table attacks when you store those hashed passwords. In this case the password is being used to generate an encryption / decryption key and is not being stored.