Skip to content
Guides

Create Digital Certificates

PKCS12 (Public Key Cryptography Standard #12) format digital certificates usually contain a public key, a private key, and other information related to the certificate. PKCS12 is a standard format used to store security certificates, private keys, and other related information. This format is commonly used to export, backup, and share digital certificates and private keys which are used in secure communications and identity verification.

When creating a PKCS12 standard certificate, in addition to the data confirming your identity, a password is typically required to protect your certificate. Only those who possess the password can access the private key contained within and perform actions such as signing documents through the certificate.

This example shows how to create digital certificates:

swift
// Generate certificate.
// 
// Password: ComPDFKit
// 
// info: /C=SG/O=ComPDFKit/D=R&D Department/CN=Alan/[email protected]
// 
// C=SG: This represents the country code "SG," which typically stands for Singapore.
// O=ComPDFKit: This is the Organization (O) field, indicating the name of the organization or entity, in this case, "ComPDFKit."
// D=R&D Department: This is the Department (D) field, indicating the specific department within the organization, in this case, "R&D Department."
// CN=Alan: This is the Common Name (CN) field, which usually represents the name of the individual or entity. In this case, it is "Alan."
// [email protected]: Email is [email protected]
// 
// CPDFCertUsage.CPDFCertUsageAll: Used for both digital signing and data validation simultaneously
// 
// is_2048 = true: Enhanced security encryption
//

 var certificateInfo = [String: Any]()
 certificateInfo["CN"] = "Alan"
 certificateInfo["emailAddress"] = emailTextField?.inputTextField?.text
 certificateInfo["C"] = coutryCode

 let path = NSHomeDirectory() + "/Documents"
 let writeDirectoryPath = "\(path)/Signature"
 let tempFilePath = "\(writeDirectoryPath)/\(URL(fileURLWithPath: filePath ?? "").lastPathComponent)"

 CPDFSignature.generatePKCS12Cert(withInfo: certificateInfo, password: "123", toPath: tempFilePath, certUsage: .digSig)
objective-c
// Generate certificate.
// 
// Password: ComPDFKit
// 
// info: /C=SG/O=ComPDFKit/D=R&D Department/CN=Alan/[email protected]
// 
// C=SG: This represents the country code "SG," which typically stands for Singapore.
// O=ComPDFKit: This is the Organization (O) field, indicating the name of the organization or entity, in this case, "ComPDFKit."
// D=R&D Department: This is the Department (D) field, indicating the specific department within the organization, in this case, "R&D Department."
// CN=Alan: This is the Common Name (CN) field, which usually represents the name of the individual or entity. In this case, it is "Alan."
// [email protected]: Email is [email protected]
// 
// CPDFCertUsage.CPDFCertUsageAll: Used for both digital signing and data validation simultaneously
// 
// is_2048 = true: Enhanced security encryption
//

NSMutableDictionary * cer = [NSMutableDictionary dictionary];
[cer setValue:@"Alan" forKey:@"CN"];
[cer setValue:@"[email protected]" forKey:@"emailAddress"];
[cer setValue:@"CN" forKey:@"C"];

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Signature"];

  if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
      [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];

  NSString *currentDateString = [self tagString];

  NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@-%@.pfx",writeDirectoryPath,@"CSignature",currentDateString];


BOOL save = [CPDFSignature generatePKCS12CertWithInfo:cer password:@"123" writeFilePathtoPath:writeFilePath certUsage:CPDFCertUsageDigSig];