Making and Extracting the content of a .p7m file.

 
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.
 
The given Delphi program utilizes the TclEncryptor component from the  Clever Internet Suite library.
 
 
Watch on YouTube
 
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?
Join us Facebook   YouTube   Twitter   Telegram   Newsletter
 
Kind regards
Clever Components team
www.CleverComponents.com