I'm currently working on a class that encrypts large amounts of text with a randomly generated encryption key, encrypted with an X509 certificate, using a smart card, using RSACryptoServiceProvider to perform encryption and decryption of the master key. However, when I have the add-on option fOEAP set to true, every time I decrypt I get the error "OAEP decoding error" during decryption. I checked the key size and it is within acceptable limits. And I went through the breakpoints to make sure that the Base64 line that is returned from the encryption function is exactly the same as the encrypted Base64 line that returns to the decryption function when the file is downloaded again.
The key pair is definitely correct, as it works fine without OAEP. And I also checked the text encoding.
EDIT: Turns out it could be a smart card issue, when I tried decryption with a local X509 certificate, decryption succeeded.
EDIT: this is a decryption code that does not work:
string TestString = "Hello World!"; X509Certificate2 cert = DRXEncrypter.GetCertificate("Select a test certificate", "Select a certificate to use for this test from the local store."); string key = DRXEncrypter.GenerateEncryptionKey(214); Console.WriteLine("Encryption Key: " + key); string encrypted = DRXEncrypter.EncryptBody(TestString, key); Console.WriteLine("Encrypted Body: " + encrypted); string cryptokey = DRXEncrypter.EncryptWithCert(cert, key); Console.WriteLine("Encrypted Decryption Key: " + cryptokey); string decrypted = DRXEncrypter.DecryptBody(encrypted, cryptokey, cert); Console.WriteLine("Decrypted Body: " + decrypted); Console.WriteLine("Output String: " + decrypted + ".");
Here is the code from the cryptography provider class I wrote. Iām stuck with this issue for hours, so it would be great if someone could help me.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; using System.IO; namespace CoreDRXEditor { public class DRXEncrypter { private byte[] Salt = Encoding.ASCII.GetBytes("81PO9j8I1a94j"); private string EncryptionKey; private const bool UseOAEP = true; public DRXEncrypter(string EncryptionKey) { this.EncryptionKey = EncryptionKey; } public static string EncryptBody(string body, string encryptionkey) {
source share