Use "private key encryption / public key decryption". I am working on a project where there is a specific context in which we must do this. I know there is a lot of discussion about this, but I will continue to explain how to do this. I think there are many questions that explain when we should use "sign / verification" or "public key encryption / decryption of the secret key" or not.
First of all, I got the same solution as you, but it didnโt work, I checked many CspParameters options. I think this should work, but it is not!
So my final solution was to use the BouncyCastle libraries:
RsaPrivateCrtKeyParameters privateKeyParameters = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(prvKey)); AsymmetricKeyParameter publicKeyInfoParameters = PublicKeyFactory.CreateKey(Convert.FromBase64String(pubKey)); byte[] clearData = Encoding.UTF8.GetBytes("..."); string algorithm = "RSA/ECB/PKCS1Padding"; var cipherOne = Org.BouncyCastle.Security.CipherUtilities.GetCipher(algorithm); cipherOne.Init(true, privateKeyParameters); byte[] signedData = cipherOne.DoFinal(clearData); var clientTwo = CipherUtilities.GetCipher(algorithm); clientTwo.Init(false, publicKeyInfoParameters); var clearDataTwo = clientTwo.DoFinal(signedData); Assert.IsTrue(Convert.ToBase64String(clearData) == Convert.ToBase64String(clearDataTwo));
source share