Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting "Invalid algorithm specified" exception

Tags:

Here is my code.

X509Certificate pXCert = new X509Certificate2(@"keyStore.p12", "password"); RSACryptoServiceProvider csp = (RSACryptoServiceProvider)pXCert.PrivateKey; string id = CryptoConfig.MapNameToOID("SHA256"); return csp.SignData(File.ReadAllBytes(filePath), id); 

On the last line I'm getting the exception:

System.Security.Cryptography.CryptographicException "Invalid algorithm specified."

What am I doing wrong?

UPDATE:

id = 2.16.840.1.101.3.4.2.1

like image 275
scott Avatar asked Sep 15 '11 15:09

scott


2 Answers

There is no issue with .NET code or the CSP code you provided.

Your problem is that CSP just doesn’t support SHA 256. You can get further information here

like image 192
Patrick Desjardins Avatar answered Oct 24 '22 10:10

Patrick Desjardins


For dot net framework 4.7.0 or higher is not taking the sha1 so configure the below in application start. it's worked fine for me.

 AppContext.SetSwitch("Switch.System.Security.Cryptography.Xml.UseInsecureHashAlgorithms", true);  AppContext.SetSwitch("Switch.System.Security.Cryptography.Pkcs.UseInsecureHashAlgorithms", true); 
like image 31
alwayssai Avatar answered Oct 24 '22 09:10

alwayssai