Как преобразовать байт в строку в vb.net? - PullRequest
0 голосов
/ 29 сентября 2008

У меня есть функция ниже ENCRYPT.

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

Как посмотреть значение байта текста?

Ответы [ 2 ]

3 голосов
/ 29 сентября 2008

Вы можете использовать Кодировка класс.

Для преобразования массива байтов в строку вы можете использовать Encoding.GetString метод

Существует специальная версия для UTF8: UTF8Encoding.GetString

2 голосов
/ 29 сентября 2008

Не уверен на 100%, что вы спрашиваете, если вы хотите отобразить ваш зашифрованный байтовый массив в виде строки, то я бы сказал, не делайте этого, поскольку ваша строка не будет «строковыми» данными, она будет зашифрована байт и не будут отображаться (обычно)

если вы спрашиваете, как я могу увидеть байтовые значения в виде строки ... т.е. 129,45,24,67 и т. Д. Затем (при условии .net 3,5)

string.Join(",", byteArray.Select(b => b.ToString()).ToArray());

И если вы спрашиваете о преобразовании обратно зашифрованного байтового массива, то вам нужно использовать тот же класс кодирования, который вы использовали для создания исходного байтового массива, в вашем случае класс кодирования UTF8.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...