Archived Forum Post

Index of archived forum posts

Question:

decrypt s/mime message issue

Dec 10 '12 at 17:24

I have an issue decrypting the message. I created a certificate using the following command. makecert -sk Mynewkey -n "CN=TestCert" test.cer -ss my -pe

Error at this following line. mime.GetPart(1) is null. Error message with LastErrorText is : Failed to get MIME message part

Here is my decryption code.

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

        // Any string passed to UnlockComponent begins the 30-day trial
        mime.UnlockComponent("30-day trial");
        if (result != null)
        {
            mime.LoadMime(Encoding.UTF8.GetString(result.Body));
        }
        // Unwrap the security.  
        // The results are stored within the Mime object and can be queried
        // afterwards. 
        // The UnwrapSecurity does all decryption and signature validation,
        // regardless of how complex the MIME message may be.
        bool decryptSuccess = mime.UnwrapSecurity();

        if (decryptSuccess == false)
        {
            Console.WriteLine(mime.LastErrorText);
            return;
        }

        // Save the decrypted MIME message.
        mime.SaveMime("decrypted.txt");
        // (You can convert any MIME message to XML too.)
        mime.SaveXml("decrypted.xml");

        // Get the first encrypting certificate.
        // In complex cases where the MIME is nested and contains
        // other encrypted MIME messages, there will be more than
        // one encrypting certificate.
        Chilkat.Cert cert;
        cert = mime.GetEncryptCert(0);

        // This example uses a MIME message that included a GIF attachment.
        // Save the GIF to a file.
        Chilkat.Mime gifAttachment;
        gifAttachment = mime.GetPart(1);
        gifAttachment.SaveBody("sample.gif");

Accepted Answer

Look at the contents of the MIME after unwrapping the security layers (i.e. after calling UnwrapSecurity). Is the MIME multipart? If not, then obviously there are no sub-parts. If so, is there only 1 sub-part? The first sub-part is at index 0.

Also, make sure you understand the very basics of MIME -- such as: What is a multipart MIME message.

Also, to view the MIME, either save it to a file via the SaveMime method, or write it to a string via the GetMime method.