The tutorial shows how to create and extract the contents of a .p7m file, load a signing certificate from a .pfx file or Windows registry storage, verify the digital signature, and extract certificates from a .p7m file. The program signs the selected file, adds information about the user's certificate, digital signature, and saves this enveloped-data to a .p7m file. Also, the program can load an enveloped-data from a .p7m file, verify included digital signature, and extract both the signing data and included certificates.
procedure TForm1.btnSignClick(Sender: TObject);
var
src, dst: TStream;
cert: TclCertificate;
begin
src := nil;
dst := nil;
if (cbCertificate.ItemIndex < 0) then Exit;
try
src := TFileStream.Create(edtSourceFile.Text, fmOpenRead or fmShareDenyWrite);
dst := TFileStream.Create(edtDestinationFile.Text, fmCreate);
cert := clCertificateStore1.Items[cbCertificate.ItemIndex];
clEncryptor1.Sign(src, dst, False, True, cert, nil);
ShowMessage('Done');
finally
dst.Free();
src.Free();
end;
end;
procedure TForm1.btnLoadCertificateClick(Sender: TObject);
var
i: Integer;
begin
clCertificateStore1.Close();
cbCertificate.Clear();
case cbCertificateLocation.ItemIndex of
0: clCertificateStore1.ImportFromPFX(edtCertifFileName.Text, edtCertifPassword.Text);
1: clCertificateStore1.Open('MY');
end;
for i := 0 to clCertificateStore1.Items.Count - 1 do
begin
if (clCertificateStore1.Items[i].FriendlyName <> '') then
begin
cbCertificate.Items.Add(clCertificateStore1.Items[i].FriendlyName);
end else
begin
cbCertificate.Items.Add(clCertificateStore1.Items[i].IssuedTo);
end;
end;
if (cbCertificate.Items.Count > 0) then
begin
cbCertificate.ItemIndex := 0;
end;
end;
procedure TForm1.btnVerifyFileClick(Sender: TObject);
begin
clEncryptor1.ExtractCertificates(edtSRC.Text);
if clEncryptor1.ExtractedCertificates.Items.Count > 0 then
begin
lbResult.Items.Add(clEncryptor1.ExtractedCertificates.Items[0].IssuedTo);
end;
clEncryptor1.VerifyEnveloped(edtSRC.Text, edtDST.Text);
end;
Have questions?
Article ID: 161, Created: April 14, 2020 at 4:33 PM, Modified: June 4, 2020 at 4:27 PM