TaxPro GST Lib
User guide (Under Construction)
 
×
Menu
Index
C# Sample to DSC Sign AspID + TimeStamp
 
The below method and other Cryptographic methods are available in
FREE library TaxProGST.API in TPCrypto class
 
//Sign String with Digital Signature
// PfxFile (full .pfx file path) may also be replaced with byte[] of pfxfile
/// <returns>Digital Signature Hash</returns>
public static string DSCSignForSession(string ContentToSign, string PfxFile, string DSCPassword)
{
    try
    {
        X509Store store = new X509Store(StoreLocation.CurrentUser);
        store.Open(new OpenFlags());
        X509Certificate2 AspCert = null;
        X509Certificate2Collection certificates = new X509Certificate2Collection();
        if (File.Exists(PfxFile))
            certificates.Import(PfxFile, DSCPassword, X509KeyStorageFlags.PersistKeySet);
        if (certificates.Count == 0)
            return "Certificate not found";
 
        foreach (X509Certificate2 cert in certificates)
        {
            foreach (string cerProp in cert.Subject.Split(','))
            {
                if(cerProp.Trim().StartsWith("OU") && cerProp.Split('=')[1].ToUpper().Contains("GST"))
                {
                    AspCert = cert;
                    break;
                }
            }
        }
        if (AspCert != null)
        {
            RSACryptoServiceProvider rsaEncryptor = (RSACryptoServiceProvider)AspCert.PrivateKey;
            byte[] CalcHash = (new SHA256Managed().ComputeHash(Encoding.UTF8.GetBytes(ContentToSign)));
            return (Convert.ToBase64String(rsaEncryptor.SignHash(CalcHash, CryptoConfig.MapNameToOID("SHA256"))));
        }
        else
            return "Certificate not found";
    }
    catch(Exception ex)
    {
        return ex.Message;
    }
}