【发布时间】:2015-06-04 13:10:15
【问题描述】:
今天我有一个关于我当前要进行的 Visual Basic 项目的问题。我的意图是在可执行文件所在的同一目录中为我的程序提供一个或多个加密的配置文件。因为很可能(en/de)加密将被处理两次(用户名和密码),我想( en/de)crypt 直接从字符串到字符串。我希望这将减少临时文件的麻烦,并且以后可能会有用。
在这个伟大的网站和另一个教程的帮助下,至少编译器不再抱怨了。 :) 到目前为止,一切都很好......
我的代码如下:
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
问题是如果我调用函数并让我打印给出结果,它们总是空的。也许其他人知道为什么? :)
提前感谢您的帮助...
有用的资源:
【问题讨论】:
标签: .net vb.net encryption aes rijndael