Contenidos
Servidor que envía un mensaje cifrado al cliente
Utiliza un certificado X509 generado anteriormente https://www.jesusninoc.com/11/18/exports-a-certificate-to-a-personal-information-exchange-pfx-file/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$Certificate = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2(,"C:\Users\juan\cert.pfx","1234") $port = "1234" $TcpListener = [System.Net.Sockets.TcpListener][int]$port $TcpListener.start() $TcpStream = $TcpListener.AcceptTcpClient() $SslStream = New-Object System.Net.Security.SslStream $TcpStream.GetStream(), $false, ({$True} -as [Net.Security.RemoteCertificateValidationCallback]) $SslStream.AuthenticateAsServer($Certificate, $false, [System.Security.Authentication.SslProtocols]::tls, $false) $mensaje = (New-Object System.IO.StreamWriter $SslStream) $mensaje.Write("hola") $mensaje.Dispose() $TcpListener.Stop() |
Cliente que recibe un mensaje cifrado de parte del servidor
Utiliza un certificado X509 generado anteriormente https://www.jesusninoc.com/11/18/exports-a-certificate-to-a-personal-information-exchange-pfx-file/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
$Certificate = $null $TcpClient = New-Object -TypeName System.Net.Sockets.TcpClient $TcpClient.Connect("127.0.0.1", "1234") $TcpStream = $TcpClient.GetStream() $SslStream = New-Object System.Net.Security.SslStream $TcpStream, $false, ({$True} -as [Net.Security.RemoteCertificateValidationCallback]) $SslStream.AuthenticateAsClient((New-Object System.Security.Cryptography.X509Certificates.X509Certificate2(,"C:\Users\juan\cert.pfx","1234"))) $Certificate = $SslStream.RemoteCertificate if ($Certificate) { if ($Certificate -isnot [System.Security.Cryptography.X509Certificates.X509Certificate2]) { $Certificate = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Certificate2 -ArgumentList $Certificate } Write-Output $Certificate } $mensaje = New-Object System.IO.StreamReader $SslStream $mensaje.ReadLine() $SslStream.Dispose() $TcpClient.Dispose() |