Я работаю над приложением, в котором мне придется подписывать данные (строку), которые затем будут использоваться в приложении позже.Проблема в том, что каждый раз, когда я пытаюсь это сделать, вывод выводится в виде пустой строки.код для подписи написан ниже ...
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" выходит пустойстрока ... пожалуйста, помогите мне!