2015-09-03 142 views
1

我想數字簽署一個PDF文件,但我越來越期待DER長度超過4個字節?

DER長度超過4個字節。

這裏是我的代碼:

public static Asn1EncodableVector GetTimestamp(byte[] signature) 
{ 

     ITSAClient tsc = new TSAClientBouncyCastle("https://wstsa.kibs.mk/wsTSA.asmx", null, null); 
     //return tsc.GetTimeStampToken(null, tsImprint); 
     HashAlgorithm sha = new SHA1CryptoServiceProvider(); 


     //byte[] hash = sha1.ComputeHash(bytData); 
     String ID_TIME_STAMP_TOKEN = "1.2.840.113549.1.9.16.2.14"; // RFC 3161 id-aa-timeStampToken 
     mk.kibs.wstsatest.wsTSATest oWS1 = new mk.kibs.wstsatest.wsTSATest(); 
    // HashAlgorithm sha = new SHA1CryptoServiceProvider(); 

     mk.kibs.wstsatest.TSCheck_Bytes bytes = new mk.kibs.wstsatest.TSCheck_Bytes(); 
     mk.kibs.wstsatest.TSResponse_Bytes b = new mk.kibs.wstsatest.TSResponse_Bytes(); 
     byte[] filename = File.ReadAllBytes(@"C:\Users\nikola.nedelkovski\Desktop\nalozinovi.pdf"); 

     SHA1CryptoServiceProvider shax = new SHA1CryptoServiceProvider(); 
     byte [] hashx = shax.ComputeHash(filename); 
    // Asn1InputStream tempstream = new Asn1InputStream(new MemoryStream(tsc.GetTimeStampToken(null, hashx))); 
     // mk.kibs.wstsatest.TSResponse_Bytes resp1 = oWS1.funGenerateTS_Bytes(hashx); 

     oWS1.Dispose(); 
    //  hashx = b.bytTSToken; 
     //hashx = bytes.bytHashMessage; 
     bytes.bytHashMessage = hashx; 
     Asn1InputStream tempstream = new Asn1InputStream(new MemoryStream(hashx)); 

     Asn1EncodableVector unauthAttributes = new Asn1EncodableVector(); 

     Asn1EncodableVector v = new Asn1EncodableVector(); 
     v.Add(new DerObjectIdentifier(ID_TIME_STAMP_TOKEN)); // id-aa-timeStampToken 
     Asn1Sequence seq = (Asn1Sequence)tempstream.ReadObject(); 
     v.Add(new DerSet(seq)); 

     unauthAttributes.Add(new DerSequence(v)); 
     //return unauthAttributes; 
    // return unauthAttributes; 
     return unauthAttributes; 
    } 

    public static X509Certificate2 GetCertificate() 
    { 
     X509Store st = new X509Store(StoreName.My, StoreLocation.CurrentUser); 
     st.Open(OpenFlags.ReadOnly); 
     X509Certificate2Collection col = st.Certificates; 
     X509Certificate2 card = null; 
     X509Certificate2Collection sel = X509Certificate2UI.SelectFromCollection(col, "Certificates", "Select one to sign", X509SelectionFlag.SingleSelection); 
     if (sel.Count > 0) 
     { 
      X509Certificate2Enumerator en = sel.GetEnumerator(); 
      en.MoveNext(); 
      card = en.Current; 
     } 
     st.Close(); 
     return card; 
} 

唯一的例外是在下面的行拋出:你可以找到它在代碼中提到

Asn1Sequence seq = (Asn1Sequence)tempstream.ReadObject(); 

任何幫助或建議嗎?

回答

1

那麼,你創建一個哈希,它包含可以有任何值的二進制字節。然後執行:

Asn1Sequence seq = (Asn1Sequence)tempstream.ReadObject(); 

關於與隨機無法區分的二進制數據。隨機二進制數據不代表ASN.1序列。

你需要重新設計你的方法,並理解你在做什麼。很可能你應該自己生成一個ASN.1 SEQUENCE而不是解析它。

相關問題