今天我有一個關於我目前的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
的問題是,如果我調用函數,讓我打印給出結果,他們總是空的。也許別人知道爲什麼? :)
在此先感謝您的幫助...
有用的資源:
- how to generate a stream from a string?
- http://www.codeproject.com/Articles/12092/Encrypt-Decrypt-Files-in-VB-NET-Using-Rijndael 那些幫助了我很多這個項目。