views:

1310

answers:

2

I have the function below 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

How do i see the byte value of the text?

+1  A: 

You can use Encoding class.

To convert array of bytes to a string you can use Encoding.GetString method

There is a special version for UTF8: UTF8Encoding.GetString

aku
A: 

Not 100% sure what you are asking, if you want to display your encrypted byte array as a string, then I would say, don't do that as your string won't be "string" data it will be encryted bytes and won't be displayable (generally)

if you are asking how can I see the byte values as a string...i.e. 129,45,24,67 etc then (assuming .net 3.5)

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

And if you are asking about converting back your de-crypted byte array, then you need to use the same encoding class that you used to create the original byte array, in your case the UTF8 encoding class.

Tim Jarvis