0

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);
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
KiLa
  • 133
  • 2
  • 7
  • Do you mean actual encryption of data or do you mean to verify a signature? If you mean the former, then **no**. Data can only be decrypted with the private key. – Artjom B. Mar 04 '15 at 15:50
  • @ArtjomB. Hu, forgot about that gold medal, question on hold... – Maarten Bodewes Mar 04 '15 at 16:26
  • Yes, actual encryption. It seems that in order to achieve this, we must use the digital signature model. This question was asked so many times that i thought that i could combine the decryption and validation – KiLa Mar 05 '15 at 10:50

0 Answers0