473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Digital Signatures

Hey everyone.
Im coming from this thread: http://www.thescripts.com/forum/thread117164.html
Cant reply there, so im making a new one, also the last post is very old.

Im trying to sing an email message.
Ive tryed with System.net.mail, and i can do it, but it dont has support for attachments (that i know of).
So i went to CDO.
After checking a lot in the net, come up with that post, and try it.

It does send the mail, and when i open it with Evolution Mail (Linux) the signature is recognized and it says valid signature.
But when i send the mail, and open it with Outlook, it does recognize the signature, but it says the mail has been altered (and it dont)
Also a "funny" thing, the signing date for that mail, in details, is somewhere in 1601...
Do my only gess is that the signature is bad.

if anyone can help, i really apreciate.

Regards

Following is the code im usign:

Expand|Select|Wrap|Line Numbers
  1. using System.Collections.Generic;
  2. using System;
  3. using System.IO;
  4. using System.Text;
  5. using System.Net.Mail;
  6. using System.Security.Cryptography.Pkcs;
  7. using System.Security.Cryptography.X509Certificates;
  8. using System.Collections;
  9. using System.ComponentModel;
  10. using System.Data;
  11. using System.Security.Cryptography;
  12. using System.Runtime.InteropServices;
  13. using System.Diagnostics;
  14.  
  15.  
  16. /*  ADD REFERENCES TO
  17.  *          ADOBD
  18.  *          Interop.CDO
  19.  * 
  20.  */
  21.  
  22.  
  23. namespace EmailTesteCsharp1
  24. {
  25.     class Program
  26.     {
  27.         static void Main(string[] args)
  28.         {
  29.  
  30.             Program x = new Program();
  31.             x.mySendMail("askme@ask.pt", "askme@ask.pt", "askme@ask.pt", "Novo mail", "body do email");
  32.  
  33.         }
  34.  
  35.         private void mySendMail(string to, string from, string cc, string subject,string body)
  36.         {
  37.             CDO.Message oSignedMsg = new CDO.Message();
  38.             oSignedMsg.From = "askme@ask.pt";
  39.             oSignedMsg.To = to;
  40.             if(cc != null && cc.Trim() != "")
  41.             {
  42.                 oSignedMsg.CC = cc;
  43.             }
  44.             oSignedMsg.Subject = subject;
  45.             oSignedMsg.MimeFormatted = true;
  46.             oSignedMsg.BodyPart.ContentMediaType = "multipart/signed;\rprotocol=" +
  47.                                         '\u0022' + "application/x-pkcs7-signature" + '\u0022' + ";\rmicalg=SHA1;";
  48.  
  49.             oSignedMsg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;
  50.             oSignedMsg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = "10.0.0.2";
  51.             oSignedMsg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = 25;
  52.             oSignedMsg.Configuration.Fields.Update();
  53.  
  54.             CDO.IBodyPart oBodyPart = oSignedMsg.BodyPart.AddBodyPart(oSignedMsg.BodyPart.BodyParts.Count + 1);
  55.             oBodyPart.ContentMediaType = "multipart/mixed";
  56.  
  57.             CDO.IBodyPart oBodyPart2 = oBodyPart.AddBodyPart(oBodyPart.BodyParts.Count + 1);
  58.             oBodyPart2.ContentMediaType = "text/plain;charset=\"iso-8859-1\"";
  59.             ADODB.Stream oStream = oBodyPart2.GetDecodedContentStream();
  60.             oStream.WriteText(body, 0);
  61.             oStream.Flush();
  62.             oStream.Close();
  63.  
  64.             CDO.IBodyPart oSigniturePart = oSignedMsg.BodyPart.AddBodyPart(oSignedMsg.BodyPart.BodyParts.Count + 1);
  65.             oSigniturePart.ContentMediaType = "application/x-pkcs7-signature";
  66.             oSigniturePart.ContentTransferEncoding = "base64";
  67.             oSigniturePart.Fields["urn:schemas:mailheader:content-disposition"].Value
  68.                                                 = "attachment;\rFileName=" + '\u0022' + "smime.p7s" + '\u0022' + "";
  69.             oSigniturePart.Fields.Update();
  70.  
  71.  
  72.             oStream = oSignedMsg.GetStream();
  73.             string messageToSign = oStream.ReadText(oStream.Size);
  74.             byte[] byteSignature = signMessage1(messageToSign);
  75.  
  76.  
  77.             oStream = oSigniturePart.GetDecodedContentStream();
  78.             oStream.Type = ADODB.StreamTypeEnum.adTypeBinary;
  79.             oStream.Write(byteSignature);
  80.             oStream.Flush();
  81.             oStream.Close();
  82.  
  83.             oSignedMsg.Send();
  84.         }
  85.  
  86.  
  87.         public byte[] signMessage1(string msg)
  88.         {
  89.             X509Certificate2 signerCert = new X509Certificate2("MyPfxFile.pfx", "MyPfxPassword");                       
  90.             byte[] msgBytes = Encoding.Unicode.GetBytes(msg);
  91.  
  92.  
  93.             byte[] encodedSignedCms = SignMsg1(msgBytes, signerCert);
  94.             return encodedSignedCms;
  95.         }
  96.  
  97.         static public byte[] SignMsg1(Byte[] msg, X509Certificate2 signerCert)
  98.         {
  99.             //  Place message in a ContentInfo object.
  100.             //  This is required to build a SignedCms object.
  101.             ContentInfo contentInfo = new ContentInfo(msg);
  102.  
  103.             //  Instantiate SignedCms object with the ContentInfo above.
  104.             //  Has default SubjectIdentifierType IssuerAndSerialNumber.
  105.             //  Has default Detached property value false, so message is
  106.             //  included in the encoded SignedCms.
  107.             SignedCms signedCms = new SignedCms(contentInfo);
  108.  
  109.             //  Formulate a CmsSigner object for the signer.
  110.             CmsSigner cmsSigner = new CmsSigner(signerCert);
  111.  
  112.             //  Sign the CMS/PKCS #7 message.
  113.             //Console.Write("Computing signature with signer subject " + "name {0} ... ", signerCert.SubjectName.Name);
  114.             signedCms.ComputeSignature(cmsSigner);
  115.             //Console.WriteLine("Done.");
  116.  
  117.             //  Encode the CMS/PKCS #7 message.
  118.             return signedCms.Encode();
  119.         }
  120.  
  121.  
  122.  
  123.     }
  124. }
  125.  
Oct 24 '07 #1
0 1353

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Guangxi Wu | last post by:
Hi all, Happy New Year. I am using SignedXML and an X509 certificate to digitally sign a SOAP message body and put the signature in the SOAP header for a B2B business application. Can you...
5
by: John Campbell | last post by:
Hi everyone I've been doing my best to understand the specifics of implimentating XML Digital Signatures, but I seem to be missing a fundamental concept. Let me start with a description of the...
0
by: Bradley Ward | last post by:
I have been searching and searching online for code examples of using both ..net framework and also WSE 2.0 classes to digitally sign a SOAP document and then verify the document. I got a few...
0
by: dixie | last post by:
My application developed in A2k has recently been run on an Access 2003 system and when it starts up there is a message that suggests that the macros need digital signatures. How do I create...
2
by: Martin Høst Normark | last post by:
Hi everyone Has anyone got the least experience in integrating the Digital Signature with an ASP.NET Web Application? Here in Denmark, as I supose in many other countries, they're promoting...
6
by: Matt Frame | last post by:
I have a client that has asked us to get a digital signature certificate and start digitally signing all files we pass between each other. I have heard of the subject and know about the certs but...
1
by: karflips33 | last post by:
Is it possible to automate the signing of Word 2003 docs with Digital Signatures? My problem with the code approach using ActiveDocument.Signatures.Add
6
by: simon | last post by:
I created an access database and digitally signed it. I then gave a copy of this Database to a friend. A few months later he wanted me to make some changes to it so i took an up-to-date copy from...
1
by: ckpoll2 | last post by:
Hi, I have a rookie question for you. I have a database where people create a form that has to be signed. Rather than print off a hard copy, I'd like the spot for the signature to have a digital...
5
by: troy_lee | last post by:
We have an application developed by one developer. He compiled the database into an application and digitally signed the application. He doesn't know if he used selfcert of makecert to make the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.