today I've got a question about my current Visual Basic project I'm going for. My intention is to serve one or more encrypted Configuration-files for my program in the same directory where the executable is placed in. Because most likely the (en/de)cryption will be processed twice (username and password), I want to (en/de)crypt directly from String to String. I hope that this will reduce the hassles with temporary files and might be useful later on.
With the help of this great site and another tutorial I came as far, that at least the compiler doesn't complain anymore. :) So far, so good...
My code is the following:
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
The problem is if I call the function and let me Print give the results, they are always empty. Maybe somebody else got an idea why? :)
Thanks in advance for your help...
Useful resources:
- how to generate a stream from a string?
- http://www.codeproject.com/Articles/12092/Encrypt-Decrypt-Files-in-VB-NET-Using-Rijndael Those helped me a lot with this project.