如何在vb.net中将字节转换为字符串?
我有下面的函数加密如何在vb.net中将字节转换为字符串?,vb.net,encryption,Vb.net,Encryption,我有下面的函数加密 Public Function Encrypt(ByVal plainText As String) As Byte() Dim key() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24} Dim iv() As Byte = {65, 110, 68, 26, 69, 178, 200, 219} ' Decla
Public Function Encrypt(ByVal plainText As String) As Byte()
Dim key() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}
Dim iv() As Byte = {65, 110, 68, 26, 69, 178, 200, 219}
' Declare a UTF8Encoding object so we may use the GetByte
' method to transform the plainText into a Byte array.
Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
Dim inputInBytes() As Byte = utf8encoder.GetBytes(plainText)
' Create a new TripleDES service provider
Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
' The ICryptTransform interface uses the TripleDES
' crypt provider along with encryption key and init vector
' information
Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateEncryptor(Me.key, Me.iv)
' All cryptographic functions need a stream to output the
' encrypted information. Here we declare a memory stream
' for this purpose.
Dim encryptedStream As MemoryStream = New MemoryStream()
Dim cryptStream As CryptoStream = New CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Write)
' Write the encrypted information to the stream. Flush the information
' when done to ensure everything is out of the buffer.
cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
cryptStream.FlushFinalBlock()
encryptedStream.Position = 0
' Read the stream back into a Byte array and return it to the calling
' method.
Dim result(encryptedStream.Length - 1) As Byte
encryptedStream.Read(result, 0, encryptedStream.Length)
cryptStream.Close()
Return result
End Function
如何查看文本的字节值?您可以使用类
要将字节数组转换为字符串,可以使用
UTF8有一个特殊版本:您可以使用类
要将字节数组转换为字符串,可以使用
UTF8有一个特殊版本:不是100%确定你在问什么,如果你想把加密的字节数组显示为字符串,那么我会说,不要这样做,因为你的字符串不会是“字符串”数据,它会是加密的字节,不会显示(通常) 如果您询问如何将字节值视为字符串…即。129,45,24,67等(假设.net 3.5)
如果你想转换回解密的字节数组,那么你需要使用与创建原始字节数组相同的编码类,在你的例子中是UTF8编码类。不是100%确定你在问什么,如果你想将加密的字节数组显示为字符串,那么我会说,不要这样做,因为您的字符串不是“字符串”数据,它将是加密字节,并且不可显示(通常) 如果您询问如何将字节值视为字符串…即。129,45,24,67等(假设.net 3.5) 如果您想转换回已解密的字节数组,那么需要使用与创建原始字节数组相同的编码类,在您的例子中是UTF8编码类
string.Join(",", byteArray.Select(b => b.ToString()).ToArray());