I need to be able to decrypt a string on the server using C#, but the string was encrypted using public key encryption with cryptico.js on the client. For details, see context at the end.
Cryptico gives me a private RSA key like this (note - 'like' this - I created a new one for this question):
Array ( [n] => 8029845567507477803775928519657066509146751167600087041355508603090505634905205233922950527978886894355290423984597739819216469551137046641801207199138209 [e] => 3 [d] => 5353230378338318535850619013104711006097834111733391360903672402060337089936682996269976597251251223844095913209399106464214877696419418951728015128013411 [p] => 102067954277225510613941189336789903269738979633396754230261162567549753196947 [q] => 78671563708406591396117399809764267229341143260756252277657051641634753921147 [dmp1] => 68045302851483673742627459557859935513159319755597836153507441711699835464631 [dmq1] => 52447709138937727597411599873176178152894095507170834851771367761089835947431 [coeff] => 26458340158787140383846156526777567128582042036682248240414722856369310516021
...plus a bunch of methods.
I am trying to decrypt it thusly:
RSAParameters parameters = new RSAParameters();
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
parameters.Exponent = encoding.GetBytes("3");
//dmp1
parameters.DP =
encoding.GetBytes("68045302851483673742627459557859935513159319755597836153507441711699835464631");
//dmq1
parameters.DQ =
encoding.GetBytes("52447709138937727597411599873176178152894095507170834851771367761089835947431");
//d
parameters.D =
encoding.GetBytes(
"5353230378338318535850619013104711006097834111733391360903672402060337089936682996269976597251251223844095913209399106464214877696419418951728015128013411");
//p
parameters.P =
encoding.GetBytes("102067954277225510613941189336789903269738979633396754230261162567549753196947");
//q
parameters.Q =
encoding.GetBytes("78671563708406591396117399809764267229341143260756252277657051641634753921147");
//n
parameters.InverseQ =
encoding.GetBytes(
"8029845567507477803775928519657066509146751167600087041355508603090505634905205233922950527978886894355290423984597739819216469551137046641801207199138209");
//coeff
parameters.Modulus =
encoding.GetBytes("26458340158787140383846156526777567128582042036682248240414722856369310516021");
RSA rsa = new RSACryptoServiceProvider();
rsa.ImportParameters(parameters);
var decryptThis = encoding.GetBytes(ciphertext);
var result = rsa.DecryptValue(decryptThis);
resultString = encoding.GetString(result);
But this chucks the Exception 'Bad data'.
Has anyone more experienced with C# got any ideas where I'm going wrong?
Thanks,
G
Details of context: I am attempting to implement a password strength checking function on both the client and server side of an app, but using only code on the server side. To achieve this on the client side, I want to send the putative password to the server, judge its strength, and then return a score which is displayed on the client. This means I only have to maintain password strength checking code on the server. As an extra security measure, I am encrypting the putative password using the cryptico.js library before sending it to the server to be judged.