Contenidos
Información
Enviar mensajes UDP
Cifrar un mensaje con Cryptographic Message Syntax (CMS) usando certificados
Importar un certificado para descifrar un mensaje cifrado con Cryptographic Message Syntax (CMS)
Servidor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
##Server $port=2021 $endpoint = new-object System.Net.IPEndPoint ([IPAddress]::Any,$port) $udpclient=new-Object System.Net.Sockets.UdpClient $port #Comprobar que el hash recibido y el hash del fichero recbido es correcto $content=$udpclient.Receive([ref]$endpoint) $convert=[Text.Encoding]::ASCII.GetString($content) $convert | Out-File ficherorecibido.txt #Para descifrar el mensaje es necesario tener el certificado # https://www.jesusninoc.com/11/20/imports-certificates-and-private-keys-from-a-personal-information-exchange-pfx-file-to-the-destination-store/ Unprotect-CmsMessage -Path ficherorecibido.txt $udpclient.Close() |
Cliente
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
##Client $port=2021 $endpoint = new-object System.Net.IPEndPoint ([IPAddress]::Loopback,$port) $udpclient=new-Object System.Net.Sockets.UdpClient #Crear un certificado para firmar el mensaje #https://www.jesusninoc.com/11/18/exports-a-certificate-to-a-personal-information-exchange-pfx-file/ $cert = New-SelfSignedCertificate -DnsName jesusninoc2 -CertStoreLocation "Cert:\CurrentUser\My" -KeyUsage KeyEncipherment,DataEncipherment, KeyAgreement -Type DocumentEncryptionCert -KeyExportPolicy ExportableEncrypted #Encrypts content by using the Cryptographic Message Syntax format "Hello" | Protect-CmsMessage -To cn=jesusninoc2 -OutFile secret.txt $val=[string](Get-Content secret.txt) $b=[Text.Encoding]::ASCII.GetBytes($val.ToString()) $bytesSent=$udpclient.Send($b,$b.length,$endpoint) $udpclient.Close() |
