如何使用x509Certificate2在C#中签名数据?

时间:2018-10-24 09:58:03

标签: c# x509certificate2

我正在开发一个应用程序,其中我必须对数据(字符串)进行签名,然后在该应用程序中稍后使用该数据。问题是,每次尝试时,输出都会显示为空字符串。进行签名的代码写在下面...

private string SignData(string dataToSign)
        {
            log.Info("About to sign data");
            string certPath = BankWebUtil.CertPath;
            string certPassword = BankWebUtil.CertPassword;

            string result = "";

            this.log.InfoFormat("The certificate path is : {0}", certPath);
            this.log.InfoFormat("The certificate password is : {0}", certPassword);
            X509Certificate2 x509Certificate2 = null;
            try
            {
                this.log.Info("Trying to get the certificate object with password");

                x509Certificate2 = new X509Certificate2(certPath, certPassword, X509KeyStorageFlags.Exportable);

                if (x509Certificate2 == null)
                {
                    throw new Exception("Trying to get the certificate object with password returned null");
                }

            }
            catch
            {
                this.log.Info("Trying to get the certificate object with only filename");

                x509Certificate2 = new X509Certificate2(certPath);
            }
            finally
            {
                try
                {
                    if (x509Certificate2 != null)
                    {
                        byte[] hash = new SHA256Managed().ComputeHash(new ASCIIEncoding().GetBytes(dataToSign));
                        byte[] rgbHash = hash;
                        string str = CryptoConfig.MapNameToOID("SHA256");

                        if (x509Certificate2.PrivateKey == null)
                        {
                            throw new Exception("Certificate PrivateKey is null");
                        }

                        var certifiedRSACryptoServiceProvider = x509Certificate2.PrivateKey as RSACryptoServiceProvider;

                        RSACryptoServiceProvider defaultRSACryptoServiceProvider = new RSACryptoServiceProvider();

                        defaultRSACryptoServiceProvider.ImportParameters(certifiedRSACryptoServiceProvider.ExportParameters(true));

                        byte[] inArray = defaultRSACryptoServiceProvider.SignHash(rgbHash, str);

                        result = Convert.ToBase64String(inArray);
                    }
                    else
                    {

                        throw new Exception("Certificate object is null");
                    }
                }
                catch (Exception ex)
                {

                    this.log.Error(ex.Message,ex);
                }
            }

            return result;

        }

这就是我打算测试代码的方式。

        var customerRef = "200937943";
        var customerName = "KAWEESI  MARTIN";
        var customerTel = "256774018257";
        var customerType = "POSTPAID";
        var vendorTranId = "UMEME280918200001";
        var VendorCode = "Vendor Code";
        var pPassword = "ECO-TEST";
        var paymentDate = "28/09/2018";
        var paymentType = "2";
        var teller = "899";
        var tranAmount = "48445.51";
        var tranNarration = "BC|UMEME280918200001|200937943|0001";
        var tranType = "Cash";

        var digitalSignature = SignData(customerRef + customerName + customerTel + customerType + vendorTranId + VendorCode +
                 pPassword + paymentDate + paymentType + teller + tranAmount + tranNarration + tranType);

变量“ digitalSignature”以空字符串形式出现...请帮助我!

0 个答案:

没有答案