Sample code for 30+ languages & platforms
C#

S/MIME Decrypt, Verify Signatures, and Get Algorithms

Demonstrates how to get the digest and encryption algorithms encountered when verifying signatures and decrypting S/MIME.

Chilkat C# Downloads

C#
bool success = false;

// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

Chilkat.Mime mime = new Chilkat.Mime();

success = mime.LoadMimeFile("qa_data/smime/signed_and_encrypted_mime.txt");
if (success == false) {
    Debug.WriteLine(mime.LastErrorText);
    return;
}

success = mime.AddPfxSourceFile("qa_data/smime/secret.pfx","secret");
if (success == false) {
    Debug.WriteLine(mime.LastErrorText);
    return;
}

// Call UnwrapSecurity to decrypt and verify signatures..
success = mime.UnwrapSecurity();
if (success == false) {
    Debug.WriteLine(mime.LastErrorText);
    return;
}

// The information about what digest and encryption algorithms were encountered are
// in the last JSON data.  This is JSON that contains various pieces of information 
// depending on the method call.  
Chilkat.JsonObject json = new Chilkat.JsonObject();
mime.GetLastJsonData(json);

json.EmitCompact = false;

Debug.WriteLine(json.Emit());

// The JSON contains information such as the following:

// 	{
// 	  "pkcs7": {
// 	    "decrypt": [
// 	      {
// 	        "alg": {
// 	          "name": "aes",
// 	          "keySize": 128
// 	        }
// 	      }
// 	    ],
// 	    "verify": {
// 	      "digestAlgorithms": [
// 	        "sha256"
// 	      ]
// 	    }
// 	  }
// 	}

// To iterate over digest and encryption algorithms:
int numEncAlgs = json.SizeOfArray("pkcs7.decrypt");
int i = 0;
while (i < numEncAlgs) {
    json.I = i;
    Debug.WriteLine("encAlg = " + json.StringOf("pkcs7.decrypt[i].alg.name"));
    Debug.WriteLine("keySize = " + Convert.ToString(json.IntOf("pkcs7.decrypt[i].alg.keySize")));
    i = i + 1;
}

int numDigestAlgs = json.SizeOfArray("pkcs7.verify.digestAlgorithms");
i = 0;
while (i < numDigestAlgs) {
    json.I = i;
    Debug.WriteLine("digest = " + json.StringOf("pkcs7.verify.digestAlgorithms[i]"));
    i = i + 1;
}