Archived Forum Post

Index of archived forum posts

Question:

PKCS7 Digital Signature Function

Mar 13 '13 at 10:23

hi..

i'm tryimg to test one of your functions (Digital Sigantures), which is: "PKCS7 SignedData Detached Signature" as follow:

void ChilkatSample(void) { CkCrypt2 crypt;

//  Any string argument automatically begins the 30-day trial.
bool success;
success = crypt.UnlockComponent("30-day trial");
if (success != true) {
    printf("Crypt component unlock failed\n");
    return;
}

CkCert cert;
success = cert.LoadByCommonName("Merchant");
if (success != true) {
    printf("%s\n",cert.lastErrorText());
    return;
}

//  Make sure this certificate has a private key available:
bool bHasPrivateKey;
bHasPrivateKey = cert.HasPrivateKey();
if (bHasPrivateKey != true) {
    printf("No private key available for signing.\n");
    return;
}

//  Tell the encryption component to use this cert.
crypt.SetSigningCert(cert);

const char * strData;
strData = "This is the data to be signed.";

//  Indicate that the PKCS7 signature should be returned
//  as a base64 encoded string:
crypt.put_EncodingMode("base64");

//  The EncodingMode may be set to other values such as
//  "hex", "url", "quoted-printable", etc.

const char * strSignature;
strSignature = crypt.SignStringENC(strData);

printf("%s\n",(const char *)strSignature);

//  Now verify the signature against the original data.

//  Tell the component what certificate to use for verification.
crypt.SetVerifyCert(cert);

success = crypt.VerifyStringENC(strData,strSignature);
if (success == true) {
    printf("digital signature verified\n");
}
else {
    printf("digital signature invalid\n");
}

//  Try it with incorrect data:
success = crypt.VerifyStringENC("This is not the signed data",strSignature);
if (success == true) {
    printf("digital signature verified\n");
}
else {
    printf("digital signature invalid\n");
}

}

Evebthough i did not change anything in the code (Ecxept for adding the digital certificate), i keep getting the following 2 errors:

[bcc32 Error] digital signature PKCS7.cpp(61): E2193 Too few parameters in call to 'CkCrypt2::SignStringENC(const char ,CkString &)' [bcc32 Error] digital signature PKCS7.cpp(61): E2034 Cannot convert 'bool' to 'const char '

So, can you please tell me what is the problem!! Regards, rania


Answer

Each Chilkat C++ method that returns a string has two versions — an upper-case version that returns the string in a CkString (always the last argument), and a lower-case version that returns a "const char *".

For example, in the CkCrypt2 class:

bool CkCrypt2::SignStringENC(const char str, CkString &outStr);
const char CkCrypt2::signStringENC(const char str);
The lower-case method returning a "const char " returns a pointer to memory that may be overwritten in subsequent calls. Therefore, make sure to copy the string to a safe place immediately before making additional calls on the same Chilkat object instance. (Only methods that also return "const char *" would overwrite the memory from a previous call.)

The upper-case version of the method returns the string in a CkString object. It is an output-only argument, meaning that the CkString contents are replaced, not appended.