PDA

Ver la Versión Completa : Encriptar con certificado


jars
26-05-2021, 21:23:16
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.


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;
}
}

Punzo
27-05-2021, 19:17:29
Dale una leída a este link, a ver si te puede ayudar
https://github.com/bambucode/tfacturaelectronica

jars
31-05-2021, 15:06:55
Gracias Punzo, lo voy a checkear