Question about RSA Decrypt using private key RRS feed

  • Question

  • Hello,

         I am doing a project to communiate with a bank. I send a message which was sign by my private key to the bank and get a response from the bank. The message like below: PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iR0JLIj8+CjxtZXNzYWdlIG1ldGhvZD0iZXJyb3IiIHR5cGU9InJlc3BvbnNlIj4KICAgIDxkYXRlPjIwMTAwNTI4PC9kYXRlPgogICAgPHRpbWU+MTEwMzQ1PC90aW1lPgogICAgPHJldENvZGU+QVVUSDBFSTwvcmV0Q29kZT4KICAgIDxjb21tZW50UmVzPuacquW8gOWNoTwvY29tbWVudFJlcz4KPC9tZXNzYWdlPgo=

    Then I try to decrypt the message. The code like below:

           public static string ParsePayment(HttpRequest request)
            string CertificatePW = "123456";
            string prikey_path = HttpContext.Current.Server.MapPath("~/App_Data") + "\\certificate\\test.pfx";

            return CerRSADecrypt(Convert.FromBase64String(aa), prikey_path, CertificatePW);     
        public static string CerRSADecrypt(byte[] DataToDecrypt, string prikey_path, string CertificatePW)

                MemoryStream reader = new MemoryStream(DataToDecrypt);
                MemoryStream result = new MemoryStream();
                X509Certificate2 x509_Cer2 = new X509Certificate2(prikey_path, CertificatePW);

                RSACryptoServiceProvider oRSA1 = (RSACryptoServiceProvider)x509_Cer2.PrivateKey;
                int blocksize = 128;      

                byte[] buffer, buffer1, encryblock;
                bool Closed = true;
                while (Closed)
                    buffer = null;
                    buffer = new byte[blocksize];
                    int k = reader.Read(buffer, 0, buffer.Length);
                    if (k > 0)
                        if (blocksize == k)
                            encryblock = oRSA1.Decrypt(buffer, false);
                            result.Write(encryblock, 0, encryblock.Length);
                            buffer1 = new byte[k];
                            for (int i = 0; i < k; i++)
                                buffer1[i] = buffer[i];
                            encryblock = oRSA1.Decrypt(buffer1, false);
                            result.Write(encryblock, 0, encryblock.Length);
                        Closed = false;
                return Encoding.UTF8.GetString(result.ToArray());     

    But when I run the code, I get an error.

    [CryptographicException: Bad Data.
     System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 hr) +33
     System.Security.Cryptography.Utils._DecryptKey(SafeKeyHandle hPubKey, Byte[] key, Int32 dwFlags) +0
     System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] rgb, Boolean fOAEP) +268
     CitiService.CerRSADecrypt(Byte[] DataToDecrypt, String prikey_path, String CertificatePW) in c:\ExpressMarketing\ExpressMarketingWeb\App_Code\TService.cs:638
     CitiService.ParsePayment(HttpRequest request) in c:\ExpressMarketing\ExpressMarketingWeb\App_Code\TService.cs:587
     TestWebService.Login_Click(Object sender, EventArgs e) in c:\ExpressMarketing\ExpressMarketingWeb\TestWebService.aspx.cs:30
     System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
     System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
     System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
     System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
     System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
     System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

    Could anyone tell me whether my function is wrong or the orginal message is wrong? Thanks a lot.

    Friday, May 28, 2010 7:05 AM

All replies

  • Does the bank have your public key? Did you agree with the bank on the block size? Did you also agree on the encoding? Ecryption/Decryption depends on both parties agreeing to the same way of doing things. If they can't agree, then one party provides the other party with a library that can be called to encrypt and decrypt the message. Otherwise, nothing works.
    Thursday, September 9, 2010 2:25 AM