Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Encriptar con certificado (https://www.clubdelphi.com/foros/showthread.php?t=95210)

jars 26-05-2021 21:23:16

Encriptar con certificado
 
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;
            }
}


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


La franja horaria es GMT +2. Ahora son las 10:08:30.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi