Namespace: System.Net.Security
Provides a stream that encrypts and decrypts data using an SslStream.
The EncryptedStream
class allows you to wrap an existing stream to add encryption and decryption capabilities on top of an established SSL/TLS connection. This is typically used in network communication protocols where secure data transfer is required.
EncryptedStream(Stream innerStream, bool leaveInnerStreamOpen)
EncryptedStream
class with the specified inner stream and a value indicating whether to leave the inner stream open upon closing.
EncryptedStream(Stream innerStream, bool leaveInnerStreamOpen, RemoteCertificateValidationCallback userCertificateValidationCallback)
EncryptedStream
class with the specified inner stream, a value indicating whether to leave the inner stream open upon closing, and a callback delegate for validating the remote certificate.
CanRead
: bool
CanSeek
: bool
false
for EncryptedStream
.
CanTimeout
: bool
CanWrite
: bool
InnerStream
: Stream
EncryptedStream
.
Length
: long
EncryptedStream
.
Position
: long
EncryptedStream
.
ReadTimeout
: int
WriteTimeout
: int
AuthenticateAsClient(string targetHost)
AuthenticateAsClient(string targetHost, System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates, System.Security.Cryptography.OidCollection acceptableProtocols, bool checkCertRevocation)
AuthenticateAsServer(System.Security.Cryptography.X509Certificates.X509Certificate serverCertificate, bool clientCertificateRequired, System.Security.Cryptography.OidCollection acceptableProtocols, bool checkCertRevocation)
BeginAuthenticateAsClient(string targetHost, AsyncCallback callback, object state)
BeginAuthenticateAsServer(System.Security.Cryptography.X509Certificates.X509Certificate serverCertificate, AsyncCallback callback, object state)
Close()
Dispose()
EndAuthenticateAsClient(IAsyncResult asyncResult)
EndAuthenticateAsServer(IAsyncResult asyncResult)
Flush()
Lock(long position, long length)
EncryptedStream
.
Read(byte[] buffer, int offset, int count)
: int
Seek(long offset, SeekOrigin origin)
: long
EncryptedStream
.
SetLength(long value)
EncryptedStream
.
Unlock(lockToken)
EncryptedStream
.
Write(byte[] buffer, int offset, int count)
The following code example demonstrates how to use EncryptedStream
to wrap a TCP client's network stream for secure communication.
using System;
using System.Net.Sockets;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.IO;
using System.Text;
// This is a simplified example. Real-world SSL/TLS implementation
// requires careful certificate management and error handling.
public class SecureClient
{
public static void ConnectAndCommunicate(string hostname, int port)
{
using (TcpClient client = new TcpClient())
{
try
{
client.Connect(hostname, port);
using (NetworkStream stream = client.GetStream())
{
// Use EncryptedStream to wrap the NetworkStream
// For client authentication, typically you don't provide client certs unless required by server
// targetHost should match the certificate's common name or subject alternative name
EncryptedStream encryptedStream = new EncryptedStream(stream, true);
// Authenticate as client. Null certificate means no client certificate is presented.
encryptedStream.AuthenticateAsClient(hostname);
// Now you can use encryptedStream for sending and receiving encrypted data
string messageToSend = "Hello, secure server!";
byte[] messageBytes = Encoding.ASCII.GetBytes(messageToSend);
encryptedStream.Write(messageBytes, 0, messageBytes.Length);
encryptedStream.Flush();
//Console.WriteLine($"Sent: {messageToSend}");
// Example: Reading response (simplified)
byte[] buffer = new byte[1024];
int bytesRead = encryptedStream.Read(buffer, 0, buffer.Length);
string response = Encoding.ASCII.GetString(buffer, 0, bytesRead);
//Console.WriteLine($"Received: {response}");
}
}
catch (Exception ex)
{
//Console.Error.WriteLine($"An error occurred: {ex.Message}");
}
}
}
}