签署Apple的存折通行证

时间:2014-03-21 13:27:24

标签: c# signing passbook

我正在尝试在C#中签署一个用于Apple Passbook传递的manifest.json文件。我已关注Apple's guide for signing passes并查看了帖子herehere。当我执行代码时,在signedCms.ComputeSignature(cmsSigner);行发生CryptographicException,表示

  

密钥不存在

这是我目前的代码:

    private void button2_Click(object sender, EventArgs e)
    {

        String manifest = System.IO.File.ReadAllText(@"C:\Users\Administrator\Downloads\Testpass\Testpass\manifest.json");
        byte[] msgBytes = Encoding.Unicode.GetBytes(manifest);
        byte[] sign = SignMsg(msgBytes, findCertificate(true));

        //byte[] sign = signit(manifest);
        File.WriteAllBytes(@"C:\Users\Administrator\Downloads\Testpass\Testpass", sign);


    }

    private byte[] SignMsg(Byte[] msg, X509Certificate2 signerCert)
    {
        ContentInfo contentInfo = new ContentInfo(msg);
        SignedCms signedCms = new SignedCms(contentInfo, true);

        X509Certificate2 developerCertificate = new X509Certificate2(@"C:\Users\Administrator\Downloads\pass.cer");
        X509Certificate2 appleWWDRCA = new X509Certificate2(@"C:\Users\Administrator\Downloads\AppleWWDRCA.cer");
        CmsSigner cmsSigner = new CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber, developerCertificate);
        cmsSigner.IncludeOption = X509IncludeOption.EndCertOnly;
        cmsSigner.Certificates.Add(appleWWDRCA);
        cmsSigner.SignedAttributes.Add(new Pkcs9SigningTime(DateTime.Now));

        //  Sign the CMS/PKCS #7 message.
        signedCms.ComputeSignature(cmsSigner);

        //  Encode the CMS/PKCS #7 message.
        return signedCms.Encode();
    }

是否需要从Windows证书存储区获取证书,或者从文件系统获取证书是否正常?任何有关如何解决问题的帮助表示赞赏!

PS:我也看了DotNet-Passbook,但首先是created some problems对我来说,其次,我真的只需要签署清单文件,而不是库提供的其他所有内容

1 个答案:

答案 0 :(得分:3)

我是dotnet-passbook的创建者。

此错误看起来像您的证书不包含私钥组件。

在Windows中打开证书时,它将指示是否包含私钥。