.net pkcs#7/12 sign detached

  public static string Sign(string myString)
  {
      X509Certificate2 cert = new X509Certificate2(@"xxxxx.pfx", "123456");
      CmsSigner signer = new CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber, cert);
      signer.DigestAlgorithm = new Oid(Oids.Sha1);

      ContentInfo content = new ContentInfo(new Oid(Oids.Pkcs7Data), Encoding.UTF8.GetBytes(myString));
      SignedCms signedCms = new SignedCms(content, true);

      signedCms.ComputeSignature(signer, false);

      var asig = signedCms.Encode();
      return Convert.ToBase64String(asig);
  }
posted @ 2022-08-17 21:04  gui.h  阅读(49)  评论(1编辑  收藏  举报