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;
public
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.PublicKey := MemoPublicKey.Lines.Text;
ipcRSA1.SignerKey := myKey;
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.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.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;
ipcRSA1.Key := myKey;
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.