17. Примеры
|
|
C# code sample (Signing E-document by a digital certificate)
private void SignData()
{
// Create new Taxcom CryptoAX
IEncryptor encryptor = null;
// Set data for signing and a digital certificate thumbprint
string _dataForSigning = @"Data";
string _certificateThumbprint = @"88 49 31 f7 1d 4f b3 ae da 00 5d ed 29 dc 12 2f fa c9 08 e1";
try
{
// Initialize Taxcom CryptoAX
encryptor = new Encryptor();
// Set certificate storage where CryptoAX will search certificates
encryptor.StoreName = "MY";
try
{
// Sign the data in format of string by the digital certificate (certificate is found by its thumbprint
var _signedData = encryptor.MrSign(Convert.ToBase64String(Encoding.UTF8.GetBytes(_dataForSigning)), _certificateThumbprint);
}
catch (Exception exc)
{
ExceptionToLog(exc);
}
}
finally
{
if (encryptor != null) Marshal.ReleaseComObject(encryptor);
}
}
C# code sample (E-signature of E-document verification)
private void SignatureVerification(byte[] signature)
{
// Create new Taxcom CryptoAX
IEncryptor encryptor = null;
// Set data that was singed
string _dataForSigning = @"Data";
// Set a digital signature
string _signature = @"xyzxyzxyzxyzxyzxyzxyzxyzxyzxyz";
try
{
// Initialize Taxcom CryptoAX
encryptor = new Encryptor();
// Set certificate storage where CryptoAX will search certificates
encryptor.StoreName = "MY";
// Declare a variable of ISignCheckResult type
ISignCheckResult result;
try
{
// check digital signature using signed data and store results
result = encryptor.MrCheckSign(_dataForSigning, _signature);
}
catch (Exception e)
{
// Signature verification is failed (error reading signature and etc) or could not be completed successfully
}
if (!result.IsSignOk)
{
var errorMessage = new StringBuilder();
bool addNewLine = false;
// Parse results of signature verification, get an error message
foreach (var signResult in result)
{
if (signResult.IsSignOk || string.IsNullOrEmpty(signResult.ErrorMessage) ||
string.IsNullOrWhiteSpace(signResult.ErrorMessage))
continue;
if (addNewLine) errorMessage.AppendLine();
errorMessage.AppendFormat(
"The following error occurred as result of certificate with thumbprint {0} verification: ",
signResult.CertificateThumbprint);
errorMessage.AppendLine();
errorMessage.Append(signResult.ErrorMessage);
addNewLine = true;
}
}
}
finally
{
if (encryptor != null) Marshal.ReleaseComObject(encryptor);
}
}