Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Firmar RSA con SHA1 (https://www.clubdelphi.com/foros/showthread.php?t=95197)

Neeruu 16-05-2021 23:32:11

Firmar RSA con SHA1
 
Hola a todos...
Tengo que firmar una cadena(string) con la clave privada de un certificado rsa con hash sha1 a base 64.

Esa cadena firmada es la que luego envió a un server para poder tener acceso.

Intente con algunos componentes sin éxito... (IPWorks, TPLockBox, y algunos códigos sueldos encontrados por ahí)

Probé con algunas paginas que hagan el firmado online y solo una me dio resultado: https://8gwifi.org/RSAFunctionality?...s&keysize=1024

Alguien puede ayudarme? tiene alguna forma de firmar?, trabajo con lo que rsa y sha1?
Yo no tengo mucha experiencia en firmar documentos y creo que se me esta escapando alguna variable o algo.

Llevo invirtiendo mucho tiempo y no logro avanzar...

Espero alguien pueda ayudarme.
Saludos.

Garada 17-05-2021 09:04:25

Los algoritmos de cifrado suelen tener variantes que son los esquemas de relleno. Las dos partes deben usar el mismo.
https://es.wikipedia.org/wiki/RSA#Esquemas

Por lo que veo la página que te funciona usa el esquema PSS.

Creo que por ahí van los tiros.

Neeruu 17-05-2021 13:53:56

Muchas gracias por la info... voy a ver si por ahi viene el tema...

Neeruu 19-05-2021 02:55:57

Ya lo solucione...

El problema que tenia es que la firma me venia con string unicode y se modificaba según el código de pagina unicode...
Recupere el valor con Byte y todo de 10!!!...

Ocupe los componentes IPWorks.. lo comento por las dudas si alguien le pasa lo mismo...

Saludos

Neeruu 04-06-2021 08:11:45

Comparto la unidad donde programe la firma...

Código Delphi [-]
unit Unit3;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, 
  Vcl.Menus, Vcl.StdCtrls,
  ipccore, ipctypes, ipcrsa, ipcjws,  System.NetEncoding,
  ipchash, CryptBase, HashObj, RSAObj, TPLB3.BaseNonVisualComponent,
  TPLB3.Signatory, TPLB3.Codec, TPLB3.CryptographicLibrary, TPLB3.OpenSSL;

type
  TForm3 = class(TForm)
    Memo1: TMemo;
    BtnHashSHA1: TButton;
    ipcHash1: TipcHash;
    ipcRSA1: TipcRSA;
    cbHexa: TCheckBox;
    cbPSS: TCheckBox;
    CheckBox8: TCheckBox;
    cbOAEP: TCheckBox;
    CheckBox10: TCheckBox;
    Button3: TButton;
    EditCodePage: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    MemoFirma: TMemo;
    MemoValid: TMemo;
    MemoPrivateKey: TMemo;
    MemoPublicKey: TMemo;
    BtnFirmar: TButton;
    BtnValidar: TButton;
    EditStringToSign: TEdit;
    procedure BtnFirmarClick(Sender: TObject);
    procedure BtnValidarClick(Sender: TObject);
    procedure BtnHashSHA1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    Function Encode64(sText:String):String;
    Function Encode64B(sText:array of Byte):String;
    Function Decode64(sText:String):String;
    Function Decode64B(sText:String):TBytes;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}

procedure TForm3.BtnValidarClick(Sender: TObject);
var myKey:TipcRSAKey;
  sSign:String;
  I: Integer;
begin
  Try
    ipcRSA1.Reset;
    ipcRSA1.UseHex := cbHexa.Checked;
    ipcRSA1.UseOAEP := cbOAEP.Checked;
    ipcRSA1.UsePSS := cbPSS.Checked;
    ipcRSA1.HashAlgorithm := rhaSHA1;

    ipcRSA1.Config('KeySize = 1024');
    ipcRSA1.Config('KeyFormat = 2');
    ipcRSA1.Config('CodePage = '+ EditCodePage.Text);


    myKey := TipcRSAKey.Create;
    //myKey.PrivateKey := cxMemoPrivate.Lines.Text;
    myKey.PublicKey := MemoPublicKey.Lines.Text;

    //ipcRSA1.Key := myKey;
    ipcRSA1.SignerKey := myKey;
    //ipcRSA1.KeyPrivateKey := cxMemoPrivate.Lines.Text;
    //ipcRSA1.KeyPrivateKey := cxMemoPrivate.Lines.Text;
    //ipcRSA1.KeyPublicKey  := cxMemoPublic.Lines.Text;

    ipcRSA1.InputMessage  :=  EditStringToSign.Text;
    sSign := '';
    for I := 0 to MemoFirma.Lines.Count -1 do
      begin
        sSign := sSign + MemoFirma.Lines[i];
      end;

    ipcRSA1.HashSignatureB := Decode64B(sSign);

    if ipcRSA1.VerifySignature then
      MemoValid.Lines.Add('OK')
    ELSE MemoValid.Lines.Add('Error');
  Finally
    FreeAndNil(myKey);
  End;
end;

Function TForm3.Encode64(sText:String):String;
var
  encoder: TBase64Encoding;
begin
  Try
    encoder:= TBase64Encoding.Create();
    Result := encoder.Encode(sText);
  Finally
    FreeAndNil(encoder);
  End;
end;

Function TForm3.Encode64B(sText:array of Byte):String;
var
  encoder: TBase64Encoding;
begin
  Try
    encoder:= TBase64Encoding.Create();
    //Result := encoder.Encode(sText);
    Result := encoder.EncodeBytesToString(sText)
  Finally
    FreeAndNil(encoder);
  End;
end;


Function TForm3.Decode64(sText:String):String;
var
  encoder: TBase64Encoding;
begin
  Try
    encoder:= TBase64Encoding.Create();
     Result := encoder.Decode(sText);
  Finally
    FreeAndNil(encoder);
  End;
end;

Function TForm3.Decode64B(sText:String):TBytes;
var
  encoder: TBase64Encoding;
begin
  Try
    encoder:= TBase64Encoding.Create();
    //Result := encoder.Decode(sText);
    Result := encoder.DecodeStringToBytes(sText);
  Finally
    FreeAndNil(encoder);
  End;
end;

procedure TForm3.BtnHashSHA1Click(Sender: TObject);
begin
  ipcHash1.InputMessage := EditStringToSign.Text;
  ipcHash1.EncodeHash := cbHexa.Checked;
  ipcHash1.ComputeHash;
  Memo1.Lines.Add('Hash SHA1 de Cadena a Firmar:' + Encode64B(ipcHash1.HashValueB));
end;

procedure TForm3.Button3Click(Sender: TObject);
begin
  MemoPrivateKey.Lines.Clear;
  MemoPublicKey.Lines.Clear;
end;

procedure TForm3.BtnFirmarClick(Sender: TObject);
var myKey:TipcRSAKey;
begin
  Try
    ipcRSA1.Reset;
    ipcRSA1.UseHex := cbHexa.Checked;
    ipcRSA1.UseOAEP := cbOAEP.Checked;
    ipcRSA1.UsePSS := cbPSS.Checked;
    ipcRSA1.HashAlgorithm := rhaSHA1;

    ipcRSA1.Config('KeySize = 1024');
    ipcRSA1.Config('KeyFormat = 2');
    ipcRSA1.Config('CodePage = ' + EditCodePage.Text);


    myKey := TipcRSAKey.Create;
    myKey.PrivateKey := MemoPrivateKey.Lines.Text;
    //myKey.PublicKey := cxMemoPublic.Lines.Text;

    ipcRSA1.Key := myKey;
    //ipcRSA1.KeyPrivateKey := cxMemoPrivate.Lines.Text;
    //ipcRSA1.KeyPublicKey  := cxMemoPublic.Lines.Text;

    ipcRSA1.InputMessage  := EditStringToSign.Text;
    ipcRSA1.Sign;

    MemoFirma.Lines.Clear;
    MemoValid.Lines.Clear;
    Memo1.Lines.Clear;

    MemoFirma.Lines.Add(Encode64B(ipcRSA1.HashSignatureB));
    Memo1.Lines.Add('Hash SHA1 de la Firma       :' + Encode64B(ipcRSA1.HashValueB));
  finally
    FreeAndNil(myKey);
  End;
end;


end.


La franja horaria es GMT +2. Ahora son las 16:03:32.

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