Definción
El Cryptographic Message Syntax (CMS) es un estándar de la IETF para proteger mensajes criptográficamente. En él se definen las sintaxis para firmar digitalmente, realizar hash, realizar mac o para cifrar cualquier tipo de dato. CMS se basa en la sintaxis de PKCS#7, que a su vez se basa en el estándar Privacy-Enhanced Mail. La versión más moderna de CMS se define en la RFC 5652 (ver también la RFC 5911 de módulos ASN.1 acordes al estándar ASN.1 2002).
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)
Funcionamiento
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() |
