is there a way to DECRYPT data using the public key that is get from the RSACryptoServiceProvider, via its ToXmlString(false) method?
If i ran the code below, i get an "Error occured while decoding OAEP padding." If i use the private key for decryption, it works fine.
I need to create an system where we can encrypt certain data, using companys private key, and then decrypt it using the public key that is inside the application, and be sure that the data is secure and that it was send from us.
// For creating keys
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(512);
string keyPrivate = rsa.ToXmlString(true);
string keyPublic = rsa.ToXmlString(false);
byte[] dataToEncrypt = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// For encrypting using private key
RSACryptoServiceProvider rsaE = new RSACryptoServiceProvider(2048);
rsaE.FromXmlString(keyPrivate);
byte[] encrypted = rsaE.Encrypt(dataToEncrypt, true);
// For decrypting using public key
RSACryptoServiceProvider rsaD = new RSACryptoServiceProvider();
rsaD.FromXmlString(keyPublic);
byte[] decrypted = rsaD.Decrypt(encrypted, true);