Hola.
Me pasaron este código en .Net para encriptar y firmar con certificado para una aplicación en Delphi 7 pero no entiendo como poder hacerlo.
Si alguien tiene experiencia en esto de claves publica y privada me podría dar una mano?.
Gracias.
Código:
La función EncriptarConCertificado de la DLL hace lo siguiente:
X509Certificate2 certificado = new X509Certificate2(pathCertificado);
RSACryptoServiceProvider RSAalgPBL = (RSACryptoServiceProvider)certificado.PublicKey.Key;
RSAParameters Key = RSAalgPBL.ExportParameters(false);
ProcesarStringAEncriptar(StringAEncriptar, Key);
private string ProcesarStringAEncriptar(string StringAEncriptar, RSAParameters Key)
{
RSACryptoServiceProvider RSAalg = null;
SHA1CryptoServiceProvider SHA1 = new SHA1CryptoServiceProvider();
string strEncriptado = "";
try
{
// Create a new instance of RSACryptoServiceProvider using the key from RSAParameters.
RSAalg = new RSACryptoServiceProvider(1280);
//Importo la clave
RSAalg.ImportParameters(Key);
//Encripto
strEncriptado = Convert.ToBase64String(RSAalg.Encrypt(ASCIIEncoding.ASCII.GetBytes(StringAEncriptar), true));
return strEncriptado;
}
catch (Exception)
{
throw;
}
finally
{
RSAalg.Dispose();
SHA1.Dispose();
}
}
La función FirmarStringConCertificado de la DLL hace lo siguiente:
X509Certificate2 certificado = new X509Certificate2(pathCertificado, passCertificado, X509KeyStorageFlags.Exportable);
RSACryptoServiceProvider RSAalgPV = (RSACryptoServiceProvider)certificado.PrivateKey;
RSAParameters Key = RSAalgPV.ExportParameters(true);
Convert.ToBase64String(this.ProcesarStringAFirmar(STR_To_Sign, Key));
private byte[] ProcesarStringAFirmar(string StringAFirmar, RSAParameters Key)
{
try
{
//Leo el archivo
return this.HashAndSign(ASCIIEncoding.ASCII.GetBytes(StringAFirmar), Key);
}
catch (Exception)
{
throw;
}
}