Unicode C
Unicode C
RSA Signature with Certificate's Private Key from PFX
See more RSA Examples
Demonstrates how to use a certificate's private key from a PFX file to create an RSA signature.Chilkat Unicode C Downloads
#include <C_CkCertStoreW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkCertW.h>
#include <C_CkPrivateKeyW.h>
#include <C_CkRsaW.h>
void ChilkatSample(void)
{
BOOL success;
HCkCertStoreW certStore;
HCkJsonObjectW jsonCN;
HCkCertW cert;
HCkPrivateKeyW privKey;
HCkRsaW rsa;
const wchar_t *strData;
const wchar_t *hexSig;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Create an instance of a certificate store object, load a PFX file,
// locate the certificate we need, and use it for signing.
// (a PFX file may contain more than one certificate.)
certStore = CkCertStoreW_Create();
// The 1st argument is the filename, the 2nd arg is the
// PFX file's password:
success = CkCertStoreW_LoadPfxFile(certStore,L"chilkat.pfx",L"test");
if (success == FALSE) {
wprintf(L"%s\n",CkCertStoreW_lastErrorText(certStore));
CkCertStoreW_Dispose(certStore);
return;
}
// Find the certificate by the subject common name:
jsonCN = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(jsonCN,L"CN",L"cert common name");
cert = CkCertW_Create();
success = CkCertStoreW_FindCert(certStore,jsonCN,cert);
if (success == FALSE) {
wprintf(L"%s\n",CkCertStoreW_lastErrorText(certStore));
CkCertStoreW_Dispose(certStore);
CkJsonObjectW_Dispose(jsonCN);
CkCertW_Dispose(cert);
return;
}
privKey = CkPrivateKeyW_Create();
success = CkCertW_GetPrivateKey(cert,privKey);
if (success == FALSE) {
wprintf(L"%s\n",CkCertW_lastErrorText(cert));
CkCertStoreW_Dispose(certStore);
CkJsonObjectW_Dispose(jsonCN);
CkCertW_Dispose(cert);
CkPrivateKeyW_Dispose(privKey);
return;
}
rsa = CkRsaW_Create();
success = CkRsaW_UsePrivateKey(rsa,privKey);
if (success == FALSE) {
wprintf(L"%s\n",CkRsaW_lastErrorText(rsa));
CkCertStoreW_Dispose(certStore);
CkJsonObjectW_Dispose(jsonCN);
CkCertW_Dispose(cert);
CkPrivateKeyW_Dispose(privKey);
CkRsaW_Dispose(rsa);
return;
}
// Encode the signature as a hex string
CkRsaW_putEncodingMode(rsa,L"hex");
strData = L"This is the string to be signed.";
// Sign the string using the sha-1 hash algorithm.
// Other valid choices are "sha-256", "md2" and "md5".
hexSig = CkRsaW_signStringENC(rsa,strData,L"sha-1");
wprintf(L"%s\n",hexSig);
wprintf(L"Success!\n");
CkCertStoreW_Dispose(certStore);
CkJsonObjectW_Dispose(jsonCN);
CkCertW_Dispose(cert);
CkPrivateKeyW_Dispose(privKey);
CkRsaW_Dispose(rsa);
}