我正在使用SSLStream和Socket类在SSL Server Enpoint上工作,遇到的麻烦是,在成功握手之后,我无法解码服务器接收到的消息:
static string ReadMessage(SslStream sslStream )
{
byte[] buffer = new byte[2048];
StringBuilder messageData = new StringBuilder();
int bytes = -1;
do
{
bytes = sslStream.Read(buffer, 0, buffer.Length);
// Use Decoder class to convert from bytes to UTF8
// in case a character spans two buffers.
Decoder decoder = Encoding.UTF8.GetDecoder();
char[] chars = new char[decoder.GetCharCount(buffer, 0, bytes)];
decoder.GetChars(buffer, 0, bytes, chars, 0);
messageData.Append(chars);
// Check for EOF.
if (messageData.ToString().IndexOf("<EOF>") != -1)
{
break;
}
} while (bytes != 0);
return messageData.ToString();
}
此后,messageData.ToString()返回:
"��\u0019I�\nbk�imk�(L9�IK\u0004�d;e�(P\r�XTk�((x�&;\u001a�y;s�~k<�(d��q�uO\n�4,\u0005�Om$�\u0011\f#�<!S�Um8�6\u001d<�Om@�WcS�\f<S�W;\u0003�\u0010m\f�"
我是否缺少从SSLStream解密的步骤?