2016-07-04 7 views
0

PKCS#7 X509証明書を使用して独自のバイナリファイルに署名するルーチンを作成しました。ルーチンは、魔法のように機能している:X509認証ファイルのクリーニング方法

public static byte[] SignFile(X509Certificate2Collection certs, byte[] data, bool Tipo_A3 = false) 
    { 
     try 
     { 
      ContentInfo content = new ContentInfo(data); 
      SignedCms signedCms = new SignedCms(content, false); 
      if (VerifySign(data)) 
      { 
       signedCms.Decode(data); 
      } 
      foreach (X509Certificate2 cert in certs) 
      { 

       CmsSigner signer = new CmsSigner(cert); 
       signer.IncludeOption = X509IncludeOption.WholeChain; 
       signer.SignerIdentifierType = SubjectIdentifierType.IssuerAndSerialNumber; 

       signer.SignedAttributes.Add(new Pkcs9SigningTime(System.DateTime.Now)); 

       if (Type_A3 == true) 
       { 
        signedCms.ComputeSignature(signer, false); 
       } 
       else 
       { 
        signedCms.ComputeSignature(signer); 
       } 

      } 
      return signedCms.Encode(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Error: " + ex.Message); 
      return null; 
     } 

    } 

私の問題は、元の情報を回復するために関連しています。 1Kbファイルは、このファイル内の署名のため、〜8Kbファイルに変換されます。

の署名/証明書なしでファイル内にというファイルを読み込む必要があります。署名する前に元のデータを復元する必要があります。その方法はわかりません。

署名されたファイルの元の内容の前と後にバイトがあることがわかりました(私は "abcd"という小さなTXTファイルを使ってテストを行いました)が、前と同じデータ長を考慮することに感謝しています元のデータを抽出します。

私はDATAが署名付きファイルです。この機能を使って、オリジナルコンテンツ、取得を知っている:

using System; 
using System.Collections.Generic; 
using System.Security.Cryptography.X509Certificates; 
using System.Security.Cryptography.Pkcs; 
using System.IO; 
using System.Windows.Forms; 

    public static Int VerifyContentInfo(byte[] data) 
     { 
      try 
      { 
      SignedCms signed = new SignedCms(); 
      signed.Decode(data); 
      signed.CheckSignature(true); 

      return signed.ContentInfo.Content.Length 

      } 
      catch 
      { 
      return null; 
      } 

     } 

問題:でも見つけるどのように安全に、署名付きファイル内の元のデータの長さを知っているのと.NET関数を使用して抽出しますか?

ありがとうございました!

+1

「signed.ContentInfo.Content」(その長さを取った値)は元のコンテンツでなければなりません。そこに何か他のものがありますか? – bartonjs

+0

WOW!どのように私はこれを見ていないのですか? @バートンズ、私の投票を許可する答えにそれを書いてください。そして、ありがとう! –

+0

ええ、それは明らかに答えではなくコメントで始まったので、明らかに動作していないように思えました。 – bartonjs

答えて

1

signed.ContentInfo.Content(その長さを取った値)は元の内容です。

+0

ありがとう! –

関連する問題