2015-06-04 40 views
1

今天我有一個關於我目前的Visual Basic項目我要去的問題。我的意圖是在放置可執行文件的同一目錄中爲我的程序提供一個或多個加密的配置文件。因爲很可能(en/de)加密將被處理兩次(用戶名和密碼),所以我想( en/de)crypt直接從String轉換爲String。我希望這會減少臨時文件帶來的麻煩,並且稍後可能會有用。AES函數總是回覆空結果

隨着這個偉大的網站和另一個教程的幫助,我來到目前爲止,至少編譯器不再抱怨了。 :)到目前爲止,一切都很好...

我的代碼如下:

Private Function produceKeyandIV(ByVal Password As String) As Object() 
    Dim ByteArray(Password.Length) As Byte, Key(31) As Byte, IV(15) As Byte 
    ByteArray = System.Text.Encoding.ASCII.GetBytes(Password) 
    Dim SHA_FUNCTION As New System.Security.Cryptography.SHA512Managed 
    Dim HashResult As Byte() = SHA_FUNCTION.ComputeHash(ByteArray) 
    Array.Copy(HashResult, Key, 32) 
    Array.Copy(HashResult, 32, IV, 0, 16) 
    Dim obj(2) As Object 
    obj(0) = Key 
    obj(1) = IV 
    Return obj 
End Function 
Private Function GenerateStreamFromString(ByVal myString As String) As Stream 
    Dim ret_stream As MemoryStream = New MemoryStream() 
    Dim stream_writer As StreamWriter = New StreamWriter(ret_stream, Encoding.Default) 
    stream_writer.Write(myString) 
    stream_writer.Flush() 
    ret_stream.Position = 0 
    Return ret_stream 
End Function 
Public Function DecryptStringReturnString(ByVal EncryptedString As String, ByVal Key As String) As String 
    Dim CryptographyParameters As Object() = produceKeyandIV(Key) 
    Try 
     Dim byteBuffer(4096) As Byte 
     Dim memoryStreamIn As Stream = GenerateStreamFromString(EncryptedString) 
     Dim memoryStreamOut As MemoryStream = New MemoryStream() 
     Dim positionInString As Long = 0 
     Dim StringLength As Long = EncryptedString.Length() 
     Dim bytesInBlockProcessed As Integer = 0 
     Dim cryptServiceProvRijn As New System.Security.Cryptography.RijndaelManaged 
     Using sCryptoStream = New CryptoStream(memoryStreamOut, _ 
               cryptServiceProvRijn.CreateDecryptor(CryptographyParameters(0), CryptographyParameters(1)), _ 
               CryptoStreamMode.Write) 
      While positionInString < StringLength 
       bytesInBlockProcessed = memoryStreamIn.Read(byteBuffer, 0, 4096) 
       sCryptoStream.Write(byteBuffer, 0, bytesInBlockProcessed) 
       positionInString = positionInString + (bytesInBlockProcessed) 
      End While 
     End Using 
     memoryStreamIn.Close() 
     memoryStreamOut.Position = 0 
     Dim stream_reader As StreamReader = New StreamReader(memoryStreamOut) 
     Return stream_reader.ReadToEnd() 
    Catch ex As Exception 

    End Try 
    Return Nothing 
End Function 
Public Function EncryptStringReturnString(ByVal DecryptedString As String, ByVal Key As String) As String 
    Dim CryptographyParameters As Object() = produceKeyandIV(Key) 
    Try 
     Dim byteBuffer(4096) As Byte 
     Dim memoryStreamIn As Stream = GenerateStreamFromString(DecryptedString) 
     Dim memoryStreamOut As MemoryStream = New MemoryStream() 
     Dim positionInString As Long = 0 
     Dim StringLength As Long = DecryptedString.Length() 
     Dim bytesInBlockProcessed As Integer = 0 
     Dim cryptServiceProvRijn As New System.Security.Cryptography.RijndaelManaged 
     Using sCryptoStream = New CryptoStream(memoryStreamOut, _ 
               cryptServiceProvRijn.CreateEncryptor(CryptographyParameters(0), CryptographyParameters(1)), _ 
               CryptoStreamMode.Write) 
      While positionInString < StringLength 
       bytesInBlockProcessed = memoryStreamIn.Read(byteBuffer, 0, 4096) 
       sCryptoStream.Write(byteBuffer, 0, bytesInBlockProcessed) 
       positionInString = positionInString + (bytesInBlockProcessed) 
      End While 
      memoryStreamIn.Close() 
      memoryStreamOut.Position = 0 
      Dim stream_reader As StreamReader = New StreamReader(memoryStreamOut) 
      Return stream_reader.ReadToEnd() 
     End Using 
    Catch ex As Exception 

    End Try 
    Return Nothing 
End Function 

的問題是,如果我調用函數,讓我打印給出結果,他們總是空的。也許別人知道爲什麼? :)

在此先感謝您的幫助...

有用的資源:

回答

0

有一條指令丟失。根據this website需要FlushFinalBlock(),以完成對CryptoStream的更改。那邊的人還建議實際將數據存儲在Byte-Arrays中,而不是轉換後的字符串。我將在進一步的發展中做到這一點,只有在最後,如果配置需要清晰可讀,它將被轉換回「可讀」文本。

我的繼承人「固定」版本的我以前的源代碼:

Imports System.IO 
Imports System.Security.Cryptography 
Imports System.Text 

Module sandbox 
    Private Function produceKeyandIV(ByVal Password As String) As Object() 
     Dim ByteArray(Password.Length) As Byte, Key(31) As Byte, IV(15) As Byte 
     ByteArray = System.Text.Encoding.ASCII.GetBytes(Password) 
     Dim SHA_FUNCTION As New System.Security.Cryptography.SHA512Managed 
     Dim HashResult As Byte() = SHA_FUNCTION.ComputeHash(ByteArray) 
     Array.Copy(HashResult, Key, 32) 
     Array.Copy(HashResult, 32, IV, 0, 16) 
     Dim obj(2) As Object 
     obj(0) = Key 
     obj(1) = IV 
     Return obj 
    End Function 
    Private Function GenerateStreamFromString(ByVal myString As String) As Stream 
     Dim ret_stream As MemoryStream = New MemoryStream() 
     Dim stream_writer As StreamWriter = New StreamWriter(ret_stream, Encoding.Default) 
     stream_writer.Write(myString) 
     stream_writer.Flush() 
     ret_stream.Position = 0 
     Return ret_stream 
    End Function 
    Public Function DecryptByteArrayReturnByteArray(ByVal Encrypted As Byte(), ByVal Key As String) As Byte() 
     Dim CryptographyParameters As Object() = produceKeyandIV(Key) 
     Try 
      Dim byteBuffer(4096) As Byte 
      Dim memoryStreamIn As Stream = New MemoryStream(Encrypted) 
      Dim memoryStreamOut As MemoryStream = New MemoryStream() 
      Dim position As Long = 0 
      Dim bytesInBlockProcessed As Integer = 0 
      Dim cryptServiceProvRijn As New System.Security.Cryptography.RijndaelManaged 
      Using sCryptoStream = New CryptoStream(memoryStreamOut, _ 
                cryptServiceProvRijn.CreateDecryptor(CryptographyParameters(0), CryptographyParameters(1)), _ 
                CryptoStreamMode.Write) 
       While position < Encrypted.Count() 
        bytesInBlockProcessed = memoryStreamIn.Read(byteBuffer, 0, 4096) 
        sCryptoStream.Write(byteBuffer, 0, bytesInBlockProcessed) 
        sCryptoStream.Flush() 
        position = position + (bytesInBlockProcessed) 
       End While 
       memoryStreamIn.Close() 
       sCryptoStream.FlushFinalBlock() ' Needed to close the CryptoStream properly (a simple flush is not enough) 
       Return memoryStreamOut.ToArray ' Independent from actual Streamposition 
      End Using 
     Catch ex As Exception 

     End Try 
     Return Nothing 
    End Function 
    Public Function EncryptByteArrayReturnByteArray(ByVal Decrypted As Byte(), ByVal Key As String) As Byte() 
     Dim CryptographyParameters As Object() = produceKeyandIV(Key) 
     Try 
      Dim byteBuffer(4096) As Byte 
      Dim memoryStreamIn As Stream = New MemoryStream(Decrypted) 
      Dim memoryStreamOut As MemoryStream = New MemoryStream() 
      Dim position As Long = 0 
      Dim bytesInBlockProcessed As Integer = 0 
      Dim cryptServiceProvRijn As New System.Security.Cryptography.RijndaelManaged 
      Using sCryptoStream = New CryptoStream(memoryStreamOut, _ 
                cryptServiceProvRijn.CreateEncryptor(CryptographyParameters(0), CryptographyParameters(1)), _ 
                CryptoStreamMode.Write) 
       While position < Decrypted.Count() 
        bytesInBlockProcessed = memoryStreamIn.Read(byteBuffer, 0, 4096) 
        sCryptoStream.Write(byteBuffer, 0, bytesInBlockProcessed) 
        sCryptoStream.Flush() 
        position = position + (bytesInBlockProcessed) 
       End While 
       memoryStreamIn.Close() 
       sCryptoStream.FlushFinalBlock() ' Needed to close the CryptoStream properly (a simple flush is not enough) 
       Return memoryStreamOut.ToArray ' Independent from actual Streamposition 
      End Using 
     Catch ex As Exception 

     End Try 
     Return Nothing 
    End Function 
    Sub Main() 
     Console.Write("Passwort: ") 
     Dim pwd = Console.ReadLine() 
     Dim x As String = New String((System.Text.Encoding.Default.GetChars(DecryptByteArrayReturnByteArray(EncryptByteArrayReturnByteArray(System.Text.Encoding.Default.GetBytes(pwd), pwd), pwd)))) 
     Console.Write(x) 
     Console.ReadLine() 
    End Sub 

End Module 

如果您覺得有代碼的問題或缺陷,請指出來給我。我非常感激。 :)