1

Requirement: I am developing a service that calls an API which has a parameter named token. Token message needs to be signed using digital certificate .pfx file. The digital signature algorithm will be SHA256withRSA, then convert the signed token to base64 encoding and at last pass the signature string to the token field of request message.

Issue : In my console/service application code, it throws an error at line 7: Unable to cast object of type System.Security.Cryptography.RSACng to type System.Security.Cryptography.RSACryptoServiceProvider

public string HBLTxnToken()
   {
        string contentToEncode = "HBL -531,0701,1,01900099870014,1,NPR,HBL-531,2101,1,0830122854100011,1,HBL@999";            
        X509Certificate2 certificate = new X509Certificate2(@"C://Users//nabinpr//Desktop//Code//Test File//HBL.pfx", "123"); //first parameter is path of certificate and second is password.
        if (certificate.HasPrivateKey)
        {
            RSACryptoServiceProvider privateKey = (RSACryptoServiceProvider)certificate.PrivateKey; // Error : System.InvalidCastException: 'Unable to cast object of type 'System.Security.Cryptography.RSACng' to type 'System.Security.Cryptography.RSACryptoServiceProvider'.'


           byte[] utf8Data = Encoding.UTF8.GetBytes(contentToEncode);
            byte[] signature = privateKey.SignData(utf8Data, "sha256");
            return Convert.ToBase64String(signature);
        }
        else
        {
            return "0";
        }
    }

I have been stuck in this from two days, I am getting different token value, valid token is given below. If the token generated from my code matched with below value then we can assume its correct. seek experts help.

Resulting token value should be : LmvlZkZW1K/DPtBZ0d9UIVwByXVPWoApOZsnJFfLw+pD+5Fknt+t9aNkaNUjcNlYdctyTwr+EZ/ZI7IXPr/UQQVinZHiLowHqn9TykRWlDRSQLdoOrYxEMVApk7EzbT49s77FW2WX1IJV7L/Xmti5PZxjkedfDcHBVATyxjH/IE=

user1254261
  • 142
  • 2
  • 3
  • 12
  • I'm going to say that the error points out that your cast is invalid, but I have no idea why you are saying that it is working perfectly fine in your web application. Are you sure it's working fine in your webapplication, or could it be your webapplication simply doesn't have a private key? – Icepickle Dec 15 '22 at 10:47
  • whether it works or not in web, i want this code to work in my service application. Please help or show me where i went wrong – user1254261 Dec 15 '22 at 10:54
  • But, that is what you have mentioned at the start of your post, that the code does work in a different context, which seems weird, especially when you are saying you are not sure it actually does work in that different context – Icepickle Dec 15 '22 at 10:59
  • Ok , i have removed that. could you please help me generate the toke that matches with the resulting/expected token – user1254261 Dec 15 '22 at 11:28
  • `RSACng` is a new implementation, incompatible with `RSACryptoServiceProvider`, hence you can't type cast. Since .NET 4.6 this was discouraged. See also this answer: https://stackoverflow.com/a/49777672/245183 – Ondrej Tucny Dec 15 '22 at 11:40
  • @OndrejTucny, i checked my version, its 4.6. Now what should i do to get the desired output. can you help me with some sample code? – user1254261 Dec 16 '22 at 04:04
  • Read the answer I linked in my previous comment. – Ondrej Tucny Dec 16 '22 at 10:10

0 Answers0