Unicode C Requires Chilkat v11.8.0+
Unicode C
Decrypt a .p7m File (using a Certificate and Private Key from a PEM File)
See more Encryption Examples
_LANGUAGE_ sample code showing how to decrypt a .p7m using a certificate w/ private key from a PEM file.Chilkat Unicode C Downloads
#include <C_CkCrypt2W.h>
#include <C_CkPemW.h>
void ChilkatSample(void)
{
BOOL success;
HCkCrypt2W crypt;
const wchar_t *pemPath;
const wchar_t *pemPassword;
HCkPemW pem;
const wchar_t *inPath;
const wchar_t *outPath;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
crypt = CkCrypt2W_Create();
// Use a digital certificate and private key from a PEM file.
// The 2nd argument of LoadPemFile is the password of the private key.
// (Pass an empty string if the private key is not encrypted.)
pemPath = L"myCertAndKey.pem";
pemPassword = L"test123";
pem = CkPemW_Create();
success = CkPemW_LoadPemFile(pem,pemPath,pemPassword);
if (success != TRUE) {
wprintf(L"%s\n",CkPemW_lastErrorText(pem));
CkCrypt2W_Dispose(crypt);
CkPemW_Dispose(pem);
return;
}
// Give the certificate and private key to the Crypt2 object.
success = CkCrypt2W_AddPemSource(crypt,pem);
if (success != TRUE) {
wprintf(L"%s\n",CkCrypt2W_lastErrorText(crypt));
CkCrypt2W_Dispose(crypt);
CkPemW_Dispose(pem);
return;
}
// Set the outPath to whatever is appropriate.
// If you are decrypting a PDF, your output might be "out.pdf"...
inPath = L"smime.p7m";
outPath = L"original.txt";
// Indicate that we're doing public key decryption (i.e. PKCS7)
CkCrypt2W_putCryptAlgorithm(crypt,L"pki");
success = CkCrypt2W_CkDecryptFile(crypt,inPath,outPath);
if (success == FALSE) {
wprintf(L"%s\n",CkCrypt2W_lastErrorText(crypt));
CkCrypt2W_Dispose(crypt);
CkPemW_Dispose(pem);
return;
}
wprintf(L"Success.\n");
CkCrypt2W_Dispose(crypt);
CkPemW_Dispose(pem);
}