/////////////////////////////////////////////////////////// // CRIPTOGRAFIA - Técnicas de desarrollo para profesionales /////////////////////////////////////////////////////////// // Capítulo 4: Criptografía en entornos .NET // Sección 4.4: Codificación de encriptación asimétrica /////////////////////////////////////////////////////////// // Listado 4.4: Ejemplo que implementa la firma digital // utilizando el algoritmo DSA. Versión C#. /////////////////////////////////////////////////////////// using System; using System.Security.Cryptography; using System.Text; class EjemploDSA { static void Main() { try { // Crear una nueva instancia de DSACryptoServiceProvider para // la generación de un nuevo par de llaves DSACryptoServiceProvider DSA = new DSACryptoServiceProvider(); // Crear la instancia de SHA1CryptoServiceProvider para generar el hash SHA-1 SHA1 sha = new SHA1CryptoServiceProvider(); // Obtener el resultado del hash de nuestro ejemplo, que luego // firmaremos byte[] HashValue = sha.ComputeHash(Encoding.Default.GetBytes("Contenido de prueba")); // Almacenar el valor firmado byte[] SignedHashValue = DSASignHash(HashValue, DSA.ExportParameters(true), "SHA1"); // Verificar el hash y mostrar los resutados if(DSAVerifyHash(HashValue, SignedHashValue, DSA.ExportParameters(false), "SHA1")) { Console.WriteLine("El resultado del hash fue verificado."); } else { Console.WriteLine("El resultado del hash NO fue verificado."); } } catch(ArgumentNullException e) { Console.WriteLine(e.Message); } } public static byte[] DSASignHash(byte[] HashToSign, DSAParameters DSAKeyInfo, string HashAlg) { try { // Crear una nueva instancia de DSACryptoServiceProvider DSACryptoServiceProvider DSA = new DSACryptoServiceProvider(); // Importar la información de la llave DSA.ImportParameters(DSAKeyInfo); // Crear una instancia de DSASignatureFormatter y pasar la // DSACryptoServiceProvider para transferir la llave privada DSASignatureFormatter DSAFormatter = new DSASignatureFormatter(DSA); // Establecer el algoritmo de hashing según parámetro DSAFormatter.SetHashAlgorithm(HashAlg); // Crear la firma para HashValue y devolverlo return DSAFormatter.CreateSignature(HashToSign); } catch(CryptographicException e) { Console.WriteLine(e.Message); return null; } } public static bool DSAVerifyHash(byte[] HashValue, byte[] SignedHashValue, DSAParameters DSAKeyInfo, string HashAlg) { try { // Crear una nueva instancia de DSACryptoServiceProvider DSACryptoServiceProvider DSA = new DSACryptoServiceProvider(); // Importar información de la llave DSA.ImportParameters(DSAKeyInfo); // Crear una instancia de DSASignatureFormatter y pasar la // DSACryptoServiceProvider para transferir la llave privada DSASignatureDeformatter DSADeformatter = new DSASignatureDeformatter(DSA); // Establecer el algoritmo de hashing según parámetro DSADeformatter.SetHashAlgorithm(HashAlg); // Verificar la firma y devolver el resultado return DSADeformatter.VerifySignature(HashValue, SignedHashValue); } catch(CryptographicException e) { Console.WriteLine(e.Message); return false; } } }