如何解密MD5哈希

时间:2015-06-05 05:53:09

标签: vb.net

我使用以下功能加密我的密码:

HashPasswordForStoringInConfigFile(Password, "MD5")

现在我想再次解密密码。

注意我在网格视图中显示加密密码,我想在特定行进入编辑模式时解密。

3 个答案:

答案 0 :(得分:2)

你可以加密和解密这样的功能来加密和解密你的文本,你可以根据需要使用它来显示解密文本 这是函数

Public Function Encrypt(ByVal plainText As String) As String

    Dim passPhrase As String = "yourPassPhrase"
    Dim saltValue As String = "mySaltValue"
    Dim hashAlgorithm As String = "MD5"

    Dim passwordIterations As Integer = 2
    Dim initVector As String = "@1B2c3D4e5F6g7H8"
    Dim keySize As Integer = 256

    Dim initVectorBytes As Byte() = Encoding.ASCII.GetBytes(initVector)
    Dim saltValueBytes As Byte() = Encoding.ASCII.GetBytes(saltValue)

    Dim plainTextBytes As Byte() = Encoding.UTF8.GetBytes(plainText)


    Dim password As New PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations)

    Dim keyBytes As Byte() = password.GetBytes(keySize \ 8)
    Dim symmetricKey As New RijndaelManaged()

    symmetricKey.Mode = CipherMode.CBC

    Dim encryptor As ICryptoTransform = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)

    Dim memoryStream As New MemoryStream()
    Dim cryptoStream As New CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)

    cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length)
    cryptoStream.FlushFinalBlock()
    Dim cipherTextBytes As Byte() = memoryStream.ToArray()
    memoryStream.Close()
    cryptoStream.Close()
    Dim cipherText As String = Convert.ToBase64String(cipherTextBytes)
    Return cipherText
End Function

和解密使用此

Public Function Decrypt(ByVal cipherText As String) As String
    Dim passPhrase As String = "yourPassPhrase"
    Dim saltValue As String = "mySaltValue"
    Dim hashAlgorithm As String = "MD5"

    Dim passwordIterations As Integer = 2
    Dim initVector As String = "@1B2c3D4e5F6g7H8"
    Dim keySize As Integer = 256
    ' Convert strings defining encryption key characteristics into byte
    ' arrays. Let us assume that strings only contain ASCII codes.
    ' If strings include Unicode characters, use Unicode, UTF7, or UTF8
    ' encoding.
    Dim initVectorBytes As Byte() = Encoding.ASCII.GetBytes(initVector)
    Dim saltValueBytes As Byte() = Encoding.ASCII.GetBytes(saltValue)

    ' Convert our ciphertext into a byte array.
    Dim cipherTextBytes As Byte() = Convert.FromBase64String(cipherText)

    ' First, we must create a password, from which the key will be 
    ' derived. This password will be generated from the specified 
    ' passphrase and salt value. The password will be created using
    ' the specified hash algorithm. Password creation can be done in
    ' several iterations.
    Dim password As New PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations)

    ' Use the password to generate pseudo-random bytes for the encryption
    ' key. Specify the size of the key in bytes (instead of bits).
    Dim keyBytes As Byte() = password.GetBytes(keySize \ 8)

    ' Create uninitialized Rijndael encryption object.
    Dim symmetricKey As New RijndaelManaged()

    ' It is reasonable to set encryption mode to Cipher Block Chaining
    ' (CBC). Use default options for other symmetric key parameters.
    symmetricKey.Mode = CipherMode.CBC

    ' Generate decryptor from the existing key bytes and initialization 
    ' vector. Key size will be defined based on the number of the key 
    ' bytes.
    Dim decryptor As ICryptoTransform = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes)

    ' Define memory stream which will be used to hold encrypted data.
    Dim memoryStream As New MemoryStream(cipherTextBytes)

    ' Define cryptographic stream (always use Read mode for encryption).
    Dim cryptoStream As New CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)

    ' Since at this point we don't know what the size of decrypted data
    ' will be, allocate the buffer long enough to hold ciphertext;
    ' plaintext is never longer than ciphertext.
    Dim plainTextBytes As Byte() = New Byte(cipherTextBytes.Length - 1) {}

    ' Start decrypting.
    Dim decryptedByteCount As Integer = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length)

    ' Close both streams.
    memoryStream.Close()
    cryptoStream.Close()

    ' Convert decrypted data into a string. 
    ' Let us assume that the original plaintext string was UTF8-encoded.
    Dim plainText As String = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount)

    ' Return decrypted string.   
    Return plainText
End Function

并调用该函数,您将获得结果。

答案 1 :(得分:1)

简单的答案是"你不能"

散列的想法是生成一个安全的"来自真实密码的代码,其中代码可以以明文形式存储;在数据库(或文本文件)的某个地方,其他用户可能会以某种方式看到它。

当有人尝试登录时,您的系统会从新登录计算另一个哈希值,然后与现有数据库中的现有哈希进行比较,如果哈希匹配,那么您知道它是正确的密码然后您可以允许他们登录,否则,密码/登录失败不一样。

您无法撤消散列的原因是因为通过执行以下步骤来计算散列:

1)将密码输入某些算法: 2)生成一个非常大的字符串,然后: 3)切断该字符串并: 4)将其中的一部分作为"哈希"

所以你看,即使你是超人解码并且可以计算算法,并且知道哈希码,并设法将其反转回原始形式,那么你仍然会丢失部分密码,因此不成功。

这就是Hashes安全的原因。

我希望这可以解释它。

答案 2 :(得分:0)

尚不可能将密码的哈希值放入算法中并以纯文本形式获取密码,因为哈希值是一种方法。但是人们所做的就是生成哈希并将其存储在一个大表中,这样,当您输入特定的哈希时,它将在表中检查与哈希匹配的密码并将该密码返回给您。这样做的网站示例是https://www.cmd5.org/。现代的密码存​​储系统通过使用一种加盐算法来对付这种情况,以便在注册期间在密码框中输入相同的密码时,会生成不同的哈希值。