0

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

テキストのバイト値を確認するにはどうすればよいですか?

4

2 に答える 2

3

Encodingクラスを使用できます。

バイト配列を文字列に変換するには、Encoding.GetStringメソッドを使用できます。

UTF8 には特別なバージョンがあります: UTF8Encoding.GetString

于 2008-09-29T04:24:16.310 に答える
2

暗号化されたバイト配列を文字列として表示したい場合、あなたが何を求めているのか100%確信が持てません。表示不可(一般的に)

バイト値を文字列として表示する方法を尋ねている場合...つまり、129、45、24、67など(.net 3.5を想定)

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

また、復号化されたバイト配列を元に戻すことについて質問している場合は、元のバイト配列の作成に使用したのと同じエンコーディング クラス (この場合は UTF8 エンコーディング クラス) を使用する必要があります。

于 2008-09-29T04:54:44.800 に答える