Chilkat
HOME
Android™
ASP
Visual Basic
VB.NET
C#
iOS (IPhone)
Objective-C
C++
C
MFC
Delphi
FoxPro
Java
Perl
PHP Extension
PHP ActiveX
Python
PowerShell
Ruby
SQL Server
VBScript
Create PKCS7 Signature using .cer and .key FilesUses a digital certificate (.cer file) and private key file to create a PKCS7 signature.
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, CHILKATFILEACCESSLib_TLB, CHILKATCRYPT2Lib_TLB, CHILKATCERTIFICATELib_TLB, OleCtrls; ... procedure TForm1.Button1Click(Sender: TObject); var cert: TChilkatCert; success: Integer; privKey: CHILKATCERTIFICATELib_TLB.IPrivateKey; password: String; crypt: TChilkatCrypt2; pkcs7Sig: Array of Byte; textToSign: String; fac: TCkFileAccess; facSuccess: Integer; begin // First, load the .cer and .key files into Chilkat objects... cert := TChilkatCert.Create(Self); success := cert.LoadFromFile('myCert.cer'); if (success <> 1) then begin Memo1.Lines.Add(cert.LastErrorText); Exit; end; privKey := TprivateKey.Create(Self).ControlInterface; password := 'myPassword'; // The private key object provides different methods for // loading keys of many different formats. // This example loads a PKCS8 encrypted private key. success := privKey.LoadPkcs8EncryptedFile('myPrivateKey.key',password); if (success <> 1) then begin Memo1.Lines.Add(privKey.LastErrorText); Exit; end; // NOTE: In this example, the .cer should contain the public key // that corresponds to the private key. crypt := TChilkatCrypt2.Create(Self); // Any string argument automatically begins the 30-day trial. success := crypt.UnlockComponent('30-day trial'); if (success <> 1) then begin Memo1.Lines.Add(crypt.LastErrorText); Exit; end; // Set the certifcate + private key to be used for signing: crypt.SetSigningCert2(cert.ControlInterface As CHILKATCRYPT2Lib_TLB.IChilkatCert,privKey); textToSign := 'This is the text to be signed.'; pkcs7Sig := crypt.SignString(textToSign); if ( VarArrayHighBound(pkcs7Sig,1) == -1 ) then begin Memo1.Lines.Add(crypt.LastErrorText); Exit; end; // Save the PKCS7 signature to a file. fac := TCkFileAccess.Create(Self); facSuccess := fac.WriteEntireFile('sig.p7s', pkcs7Sig); if (facSuccess = 0) then begin ShowMessage(fac.LastErrorText); Exit; end; end; |
© 2000-2010 Chilkat Software, Inc. All Rights Reserved.