I am trying to decrypt signed and encrypted emails using a certificate and private key. This works perfectly on encrypted emails. If the email is signed and encrypted it adds extra characters to the begining and end of any file attachment data. For example, ?/?zHlTVqQAAMAAAAEAAAA//8AALgAAA...
where ?/?z
are the extra characters. The exact characters will differ depending on the type and size of the attachments. I can look for and strip those characters out, but I would need to know every possible combination.
Does anyone know on an easier way to remove these characters? Here is the code I am using to decrypt the email, where sInput
is the raw encrypted string of the email:
Dim EncryptCert As New X509Certificate2(sCERTIFICATE, sCERTIFICATE_PASS)
data = Convert.FromBase64String(sInput)
Dim envData As New EnvelopedCms(New ContentInfo(data))
Dim Recipient As New CmsRecipient(SubjectIdentifierType.IssuerAndSerialNumber, EncryptCert)
Dim col As New X509Certificate2Collection(EncryptCert)
envData.Decode(data)
envData.Decrypt(col)
Dim decData As Byte() = envData.ContentInfo.Content
message = Encoding.ASCII.GetString(decData)
Again, this works great as long as the email is not signed. When the email is signed those extra characters get added to the attachment data, and I cannot properly decode the base64 data until they are removed.
By the way, in the string ?/?z
above, the /
character is actually a square. This editor changed it to a forward slash.