Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't Decrypt with Oaep algorithm #33

Closed
bauland opened this issue Dec 24, 2022 · 4 comments
Closed

Can't Decrypt with Oaep algorithm #33

bauland opened this issue Dec 24, 2022 · 4 comments

Comments

@bauland
Copy link

bauland commented Dec 24, 2022

Hello,
I would like to decrypt some random byte (fixed little string for test purpose) using piv certificate. All is working good with Pcks algorithm but not with Oaep. What I'm missing (I'm using Wpf .Net framework project)?

private void GetCert(PivSession session)
        {
            RSAEncryptionPadding encryptPaddingScheme = lbPadding.SelectedItems[0] as RSAEncryptionPadding;
            if (encryptPaddingScheme == null) return;

            byte[] encryptedData;
            byte[] rawDecryptedData;
            var slot = PivSlot.Signing;
            string data = "Yes ! It's working...";
            var plainTextBytes = Encoding.ASCII.GetBytes(data);
            int rsaFormatOaep = 0;
            bool isValid;
            byte[] decryptedData;
            switch (encryptPaddingScheme.OaepHashAlgorithm.Name)
            {
                case "SHA1":
                    rsaFormatOaep = RsaFormat.Sha1;
                    break;
                case "SHA256":
                    rsaFormatOaep = RsaFormat.Sha256;
                    break;
                case "384": //Unsupported from TryParsePkcs1Oaep
                    rsaFormatOaep = RsaFormat.Sha384;
                    break;
                case "SHA512":
                    rsaFormatOaep = RsaFormat.Sha512;
                    break;
            }

            try
            {
                // Get public key from slot
                var pivMetadata = session.GetMetadata(slot);
                if (pivMetadata.Algorithm.IsRsa()) Debug.WriteLine("Algorithm: ok (RSA)"); else return;

                var rsaPublic = (PivRsaPublicKey)pivMetadata.PublicKey;

                var rsaParams = new RSAParameters
                {
                    Modulus = rsaPublic.Modulus.ToArray(),
                    Exponent = rsaPublic.PublicExponent.ToArray()
                };
                // Encrypt data
                using (var rsa = RSA.Create(rsaParams))
                {
                    encryptedData = rsa.Encrypt(plainTextBytes, encryptPaddingScheme); // Encrypt with padding
                }

                // Decrypt data

                rawDecryptedData = session.Decrypt(slot, encryptedData);

                // Now unpadding
                isValid = encryptPaddingScheme.Mode == RSAEncryptionPaddingMode.Pkcs1 ? RsaFormat.TryParsePkcs1Decrypt(rawDecryptedData, out decryptedData) : RsaFormat.TryParsePkcs1Oaep(rawDecryptedData, rsaFormatOaep, out decryptedData);
                if (isValid)
                {
                    MessageBox.Show("Decrypted Message: " + Encoding.ASCII.GetString(decryptedData));
                }
                else
                {
                    MessageBox.Show("Decrypted failed.");
                }

            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }

        }

@GregDomzalski
Copy link
Collaborator

Hi @bauland

Thanks for your detailed bug report! Now that the team is back from winter holidays, I will have someone look into this issue.

@GregDomzalski
Copy link
Collaborator

Hey @bauland - a quick update. The team found a subtle difference in behavior in the .NET crypto APIs between .NET Framework 4.x and .NET 5+

Thankfully it's a straightforward fix and we will be releasing it in the next update (due out in less than a week).

@bauland
Copy link
Author

bauland commented Jan 16, 2023

Thanks for the infos. Hope it will be released soon.

@GregDomzalski
Copy link
Collaborator

This issue has been addressed in the latest release (1.6.0) which has now been released. Please give that a try.

I'm resolving this issue - however if you continue to encounter this issue, please feel free to reactivate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants