简体   繁体   中英

Decryption with RSA/ECB/PKCS1Padding

Following tag is a part of SAML response. I have to decrypt following CipherValue. I pass CipherValue ( string ) to following function but it did not return correct value, what am I doing wrong?

Documentation says :

  1. Obtain the private key from the certificate.

  2. Decrypt the encrypted key using RSA/ECB/PKCS1Padding.

( key is following value)

private string RSADecryption(string dataToDecrypt) 
{

  String certificateLocation = ConfigurationManager.AppSettings.Get("CertificateLocation");
  String certificateStore = ConfigurationManager.AppSettings.Get("CertificateStore");
  string certificateSubject = ConfigurationManager.AppSettings.Get("CertificateSubject");
  String decryptedKey = string.Empty;
  try
  {
      X509Certificate2 encryptionCertificate = X509Utilities.GetCertificateBySubject(certificateSubject, certificateStore.ParseAsEnum<StoreName>(),
      certificateLocation.ParseAsEnum<StoreLocation>());
      if (encryptionCertificate == null )
          Helper.LogMessage(" Did not find Encryption Certificate on the sserver " );
      RSACryptoServiceProvider rsaProvider = (RSACryptoServiceProvider)encryptionCertificate.PrivateKey;
      byte[] cipherbytes = Convert.FromBase64String(dataToDecrypt);
      byte[] plainbytes = rsaProvider.Decrypt(cipherbytes, false);
      System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
      decryptedKey =  enc.GetString(plainbytes);
  }

  catch (Exception e)
  {
      Helper.LogMessage(e.Message + " Key Parsing error. DataToDecrypt = " + dataToDecrypt);
      throw e;
  }
  return decryptedKey;

<CipherData><CipherValue>HQrhmJpbd0p32QjOFrOKQhNSr4q6bcbg+9AhxJS6yZR19Nqh3VDMwOW2wsCBuSmFlv7WjR7TzW6URjINvNbv2nMfsKu3M9dscxhmGY+jVtUJ6sTf7FHdK9D76miAdq1EIsm0lNtdW5etMx/85iMiFOObbyZuB8oaYeThToVjTUVYfNGQOJ99wvh8OL/40eomDlKfwN4eQ88kVbY1eUO1OxEfQsUbDWyXvZEvBPmYUb7km+C6rX8UjsLUrqOOpXym9oi5dH+T+kAP2Rb5N16VRxJHPGF/gXeuD+Jq4FSGtjiiBE9qvBIFWkt4hYabSgUE3Li5DqMoH/P9/EGqf6mpyvHjv8yJYRTpA//5nM43z/KCUFX8VrFCXBp0N1Y40zpZWJW2y9fSIlyGyMas1ByBlkxpRgjqqnv1L+pZohYmKQsOILhM34TASzddYgeQbPfoAuC8i/4cMWYDrO+NP+n5d97FZDPs3u3gMWefhWoSt0BSEwUCbaNmS9I39ihFwyT/6rmurgLvfY0AaFz16Q3qQlH3yVdGR2j+A2spfJLyMrRcVTSff6HKa6LC7xUO9aBF2WpEw1mxI0QXMY2VMtSwdT4pzMc+itbDe9r4ZW9BZhA7qsBd0oOVRxxxNvOo9eIUlLyGRzvMbX+oOUiHkAm/oil7Vll5JzvlzVrc4dzzOUA=</CipherValue></CipherData>

It's hard to be sure without being able to duplicate this or without the code that produced the encrypted value. My best guess, ie I've seen that too many times, is an text encoding issue. That's even more likely since:

a) there's no exception is the crypto code (eg bad padding);

b) ASCII is not used very much these days so the following:

System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();

might not return what you expect.

So I would try UFT8, Unicode... and all variants (unless you have this documented somewhere).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM