我刚刚看了一下AES加密算法并提出了一些问题。
Function AESEncryption(ByVal input As String, ByVal pass As String) As String
Dim AES As New System.Security.Cryptography.RijndaelManaged
Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim encrypted As String = ""
Try
Dim hash(31) As Byte
Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
Array.Copy(temp, 0, hash, 0, 16)
Array.Copy(temp, 0, hash, 15, 16)
AES.Key = hash
AES.Mode = Security.Cryptography.CipherMode.ECB
Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
Return encrypted
Catch ex As Exception
End Try
End Function
我想知道参数输入是什么以及传递是什么。传递密码,如果是,输入是什么。
答案 0 :(得分:0)
像AES这样的对称密码使您可以使用生成密文(input
)的某个密钥隐藏某些明文(encrypted
)。为了从密文中恢复明文,使用相同的密钥。
AES支持128,192和256位的密钥,但密码通常具有任意长度(并且熵不多)。此代码使用密码参数(pass
)作为单个MD5哈希调用的输入,以便派生128位密钥。这不是很安全,因为MD5和AES非常快,并且攻击者可能每秒尝试大量密码(蛮力)以解密密文。更好的方法是使用迭代的基于密码的密钥派生函数(如PBKDF2,bcrypt,scrypt)从密码中派生密钥。查看更多:How to securely hash passwords?
ECB模式不是很安全,因为它会将明文的某些结构泄露给ECB penguin所见的密文。应使用具有随机初始化向量(IV)的CBC模式以实现语义安全性。
更好的方法是通过使用GCM或EAX等经过身份验证的模式或使用具有强大MAC功能的加密 - 然后MAC方案(如HMAC-SHA256)来添加真实性。