2008-09-29 6 views
0

아래에 ENCRYPT 기능이 있습니다.바이트를 vb.net에서 문자열로 변환하는 방법은 무엇입니까?

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

100 % 확실 당신이 당신의 암호화를 표시 할 경우, 요청하고 있습니다 바이트 배열을 문자열로 쓰면 문자열을 "문자열"데이터가 아닌 바이트로 암호화하고 표시 할 수 없습니다 (일반적으로)

바이트 값을 문자열로 볼 수 있습니까? 당신이 당신의 드 암호화 되 바이트 배열을 다시 변환하는 방법에 대한 요구되는 경우 129,45,24,67 등 다음

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

(.NET 3.5을 가정) 그리고, 당신은 당신이 사용하는 것과 동일한 인코딩 클래스를 사용할 필요가 귀하의 경우 UTF8 인코딩 클래스 원래 바이트 배열을 만듭니다.

관련 문제