2016-07-10 24 views
-4

我遇到了解密問題,我的目標是能夠使用/不使用base64編碼對加密字符串進行加密/解密。到目前爲止,我可以使用base64進行加密/解密,並在沒有它的情況下進行加密,但沒有它就不能解密。我得到關於填充不正確的錯誤。VB.Net加密功能不解密不使用Base64Encode

提前致謝!

這裏是我的加密/解密功能:

Public Function DoCryptWork(Type As String, Data As String) As String 

    Dim Pass As String = Hasher.TextBoxPassword.Text 
    Dim Salt As String = Hasher.TextBoxSalt.Text 
    Dim Vect As String = Hasher.TextBoxIntVector.Text 

    Select Case Type 

     Case "e" 

      Try 

       Dim PassPhrase As String = Pass 
       Dim SaltValue As String = Salt 
       Dim HashAlgorithm As String = My.Settings.HashAlgorithm 
       Dim PasswordIterations As Integer = 2 
       Dim InitVector As String = Vect 
       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(Data) 
       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 = Nothing 

       If My.Settings.Base64EncodeMD5Hash = True Then 

        CipherText = Convert.ToBase64String(CipherTextBytes) 

        Return CipherText 

       Else 

        Dim TextCipher As New StringBuilder() 

        For n As Integer = 0 To CipherTextBytes.Length - 1 

         TextCipher.Append(CipherTextBytes(n).ToString("X2")) 

        Next n 

        CipherText = TextCipher.ToString() 

        Return CipherText 

       End If 

      Catch ex As Exception 

       MsgBox("Encryption was unsuccessfull!", MsgBoxStyle.Critical, "Error") 

       Return "Encryption was unsuccessfull!" 

      End Try 

     Case "d" 

      Try 

       Dim PassPhrase As String = Pass 
       Dim SaltValue As String = Salt 
       Dim HashAlgorithm As String = My.Settings.HashAlgorithm 
       Dim PasswordIterations As Integer = 2 
       Dim InitVector As String = Vect 
       Dim KeySize As Integer = 256 
       Dim InitVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitVector) 
       Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(SaltValue) 
       Dim CipherTextBytes As Byte() = Nothing 

       If My.Settings.Base64EncodeMD5Hash = True Then 

        CipherTextBytes = Convert.FromBase64String(Data) 

       Else 

        Dim bytedata As Byte() = Encoding.UTF8.GetBytes(Data) 

        CipherTextBytes = bytedata 

       End If 

       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 Decryptor As ICryptoTransform = SymmetricKey.CreateDecryptor(KeyBytes, InitVectorBytes) 
       Dim MemoryStream As New MemoryStream(CipherTextBytes) 
       Dim CryptoStream As New CryptoStream(MemoryStream, Decryptor, CryptoStreamMode.Read) 
       Dim PlainTextBytes As Byte() = New Byte(CipherTextBytes.Length - 1) {} 
       Dim DecryptedByteCount As Integer = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length) 

       MemoryStream.Close() 
       CryptoStream.Close() 

       Dim PlainText As String = Encoding.UTF8.GetString(PlainTextBytes, 0, DecryptedByteCount) 

       Return PlainText 

      Catch Ex As Exception 

       MsgBox("Decryption was unsuccessfull!" & vbNewLine & vbNewLine & Ex.ToString(), MsgBoxStyle.Critical, "Error") 

       Return "Decryption was unsuccessfull!" 

      End Try 

     Case Else 

      Return "Error! Invalid Case Selected We should never see this but just to be safe we'll show this message if the wrong case is selected!" 

    End Select 

    Return True 

End Function 

我調用該函數像這樣:

TextBoxOutput.Text = Encryption.DoCryptWork("e", TextBoxInput.Text) ' encrypt data. 
TextBoxOutput.Text = Encryption.DoCryptWork("d", TextBoxInput.Text) ' decrypt data. 
+0

_「但解密失敗。」_ - 你能詳細說明嗎?任何錯誤消息? - 另外,你有沒有嘗試使用調試器逐步完成代碼? –

+0

我得到一個填充是無效的錯誤。我在講話時正在調試,但還沒有發現任何東西。 –

回答

2

當你轉換字節爲十六進制每個字節,你輸出兩個十六進制數字。當您將該十六進制轉換回字節時,您將每個十六進制數字轉換爲一個字節而不是每對十六進制數字。

其實,我只是再看一眼,注意到你甚至沒有保留更早的字節。這個循環:

For n As Integer = 0 To Data.Length - 1 
    CipherTextBytes = Convert.ToByte(Data(n)) 
Next n 

CipherTextBytes在每次迭代所以你會在每次更換前一個字節,所以你最終只會從最後一位數字保持字節。

+0

謝謝! @ jmcilhinney解決了太棒了! –

+0

再次嗨!我認爲我做了事情,結果我其實沒有。我提出了整個職能,以便您最終能夠更好地理解我的目標。如果你可以有另一種看起來真棒!和以前一樣的問題。再次感謝,傑森 –