2013-12-18 2 views
0

아래 코드를 사용하여 asp.net 웹 서비스에서 디지털 인증서를 사용하여 메시지에 서명하고 있습니다. 서명이 잘 작동하고 있습니다. signedMessage.ComputeSignature 행이 얼굴 시간 초과 예외이므로 30-40 초가 소요됩니다. Windows Forms 응용 프로그램에서 실행될 때 같은 코드가 초 단위로 결과를 생성합니다. 어떤 단서 또는 도움.메시지 X509 인증서를 사용하여 서명

public static string Encrypt(string fullMessage, string certificateName, bool deAttch) 
    { 
     X509Certificate2 signer = GetCertificate(certificateName); 
     byte[] contentBytes = Encoding.ASCII.GetBytes(fullMessage); 
     Oid contentOid = new Oid("1.2.840.113549.1.7.1", "PKCS 7 Data"); 
     SignedCms signedMessage = new SignedCms(new ContentInfo(contentOid, contentBytes), deAttch); 

     signedMessage.ComputeSignature(new CmsSigner(signer)); 

     byte[] signedBytes = signedMessage.Encode(); 
     return Convert.ToBase64String(signedBytes).Trim(); 
     } 

답변

3

답변이 필요한지는 확실하지 않습니다. (어떤 영향인지는 모르지만 알게 될 것입니다.) 이미 저는 객체를 생성 생성자를 사용 및 방법에 직접 전달 된 위치 만

CmsSigner cert = new CmsSigner(signer); 

의 속성

cert.IncludeOption = X509IncludeOption.EndCertOnly;  

설정. 이제는 잘 작동하고 많은 시간을 소비하지 않습니다.

public static string Encrypt(string fullMessage, string certificateName, bool deAttch) 
    { 
     X509Certificate2 signer = GetCertificate(certificateName); 
     byte[] contentBytes = Encoding.ASCII.GetBytes(fullMessage); 
     Oid contentOid = new Oid("1.2.840.113549.1.7.1", "PKCS 7 Data"); 
     SignedCms signedMessage = new SignedCms(new ContentInfo(contentOid, contentBytes), deAttch); 
     CmsSigner cert = new CmsSigner(signer); 
     cert.IncludeOption = X509IncludeOption.EndCertOnly;    
     signedMessage.ComputeSignature(cert); 
     byte[] signedBytes = signedMessage.Encode(); 
     return Convert.ToBase64String(signedBytes).Trim(); 
     } 


     private static X509Certificate2 GetCertificate(string certificateName) 
    { 
     X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine); 
     store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly); 
     X509Certificate2 certificate = store.Certificates.Cast<X509Certificate2>().Where(cert => cert.Subject.IndexOf(certificateName) >= 0).FirstOrDefault(); 
     if (certificate == null) 
      throw new Exception("Certificate " + certificateName + " not found."); 

     return certificate; 
    } 
+0

나는 그것을 철저히 검사했다. X509IncludeOption.EndCertOnly 그것은 서명의 길이를 줄인다. 개인 인증서 만이 서명에 포함된다는 것을 의미한다. – adam

+0

포럼 사이트와 달리 "감사"또는 "모든 도움을 주셨습니다"또는 [그래서]의 서명을 사용하지 않습니다. "[안녕하세요, '고마워,'태그 라인 및 인사말을 게시물에서 삭제해야합니까?] (http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be 참조) -removed-from-posts). –

+0

@JohnSaunders ok는 이것을 다음 번에 고려할 것입니다 – adam

관련 문제