Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSACryptoServiceProvider - Decrypt - The parameter is incorrect

I need decrypt text but I am getting the message The parameter is incorrect.

The certificate is getting correctly, it is registered in windows.

enter image description here

enter image description here

Searching about the problem, I can read comment that suggest change the user in the application pool assigned to the web site in the IIS. I tried it but not work.

In the second image shows the size calculation, but I don't sure because subtract 11 to the KeySize divition per 8.

I'm using VS2010 with .net 4. The asp.net proyect uses IIS like server, doesn't use Visual Studio Development Server

What can I try to solve the problem?

[SOLUTION]

The encrypted text assigned in Decrypt method was generated with a certificate obsolete.

The text was encrypted with a certificate but then it was changed, when tried decrypt the encripted text with other certificate the method fails.

like image 309
Leandro Tuttini Avatar asked May 04 '16 18:05

Leandro Tuttini


2 Answers

This Exception happens if the private or public keys are changed.

like image 124
Ghebrehiywet Avatar answered Nov 19 '22 08:11

Ghebrehiywet


When encrypting data using an asymmetric cryptosystem (i.e RSA in your case), the size of the output is the same as the modulus size. In addition, the size of the input must be less than or equals the modulus size. Therefore when you try to decrypt a cipher text, the size of the cipher text must equal the size of the modulus (i.e. 128 bytes in your case since the key size is 1024 bits).

So, you shouldn't be segmenting the data variable, just feed it directly to the RSACryptoServiceProvider.Decrypt.

Another thing to try is to supply true for the fOAEP parameter in the rsa.Decrypt since decrypting an OAEP padded cipher text with fOAEP set to false will cause the The parameter is incorrect exception to be thrown.

like image 4
Timothy Ghanem Avatar answered Nov 19 '22 10:11

Timothy Ghanem