AES 函数总是返回空结果
AES-functions always reply with empty results
今天我有一个关于我当前要进行的 Visual Basic 项目的问题。我的意图是在可执行文件所在的同一目录中为我的程序提供一个或多个加密的配置文件。因为很可能 (en/de) 加密将被处理两次(用户名和密码),我想到 (en/de)crypt 直接从 String 到 String。我希望这将减少临时文件的麻烦,并且以后可能会有用。
在这个很棒的网站和我到目前为止的另一个教程的帮助下,至少编译器不再抱怨了。 :) 到目前为止,还不错...
我的代码如下:
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
这些对我这个项目帮助很大。
缺少说明。根据需要 this website FlushFinalBlock(),以便最终确定对 CryptoStream 所做的更改。那边的人还建议将数据实际存储在字节数组中,而不是转换后的字符串中。我将在进一步的开发中做什么,只有最后,如果配置需要简单易读,它将被转换回 "readable" 文本。
这是我之前源代码的 "fixed" 版本:
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
如果您认为代码中存在问题或缺陷,请为我指出。我会非常感激。 :)
今天我有一个关于我当前要进行的 Visual Basic 项目的问题。我的意图是在可执行文件所在的同一目录中为我的程序提供一个或多个加密的配置文件。因为很可能 (en/de) 加密将被处理两次(用户名和密码),我想到 (en/de)crypt 直接从 String 到 String。我希望这将减少临时文件的麻烦,并且以后可能会有用。
在这个很棒的网站和我到目前为止的另一个教程的帮助下,至少编译器不再抱怨了。 :) 到目前为止,还不错...
我的代码如下:
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 这些对我这个项目帮助很大。
缺少说明。根据需要 this website FlushFinalBlock(),以便最终确定对 CryptoStream 所做的更改。那边的人还建议将数据实际存储在字节数组中,而不是转换后的字符串中。我将在进一步的开发中做什么,只有最后,如果配置需要简单易读,它将被转换回 "readable" 文本。
这是我之前源代码的 "fixed" 版本:
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
如果您认为代码中存在问题或缺陷,请为我指出。我会非常感激。 :)