Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4231252
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T01:56:57+00:00 2026-05-21T01:56:57+00:00

While upgrading a Windows Forms application from the .NET 1.1 framework to the .NET

  • 0

While upgrading a Windows Forms application from the .NET 1.1 framework to the .NET 3.5 framework, I have an issue with the RSACryptoServiceProvider. In a database I have stored binary files which contains an encrypted signature (SHA1 hash) and I have a public key stored in the filesystem as a text document containing a hexadecimal string. I have no issues in reading the file from the filesystem and getting the public key coded in a certificate.

From the public key I get the modules and exponent field as part of the public key. I assign these values to a new instance of the RSAParameters object. Next I create a new instance of the RSACryptoServiceProvider class and call the ImportParameters. In that call the 3.5 code throws an exception. Note that the 1.1 code has no issues at all.

Does anyone know if the byte arrays given to the properties of the RSAParameters needs to be converted, so I can use them in the 3.5 code also? I already tried to reversing the array or converting the byte array to BigInteger, but that is not solving my problems.

For reference the full stacktrace:

System.Security.Cryptography.CryptographicException: Bad Data.

   at System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 hr)
   at System.Security.Cryptography.Utils._ImportKey(SafeProvHandle hCSP, Int32 keyNumber, CspProviderFlags flags, Object cspObject, SafeKeyHandle& hKey)
   at System.Security.Cryptography.RSACryptoServiceProvider.ImportParameters(RSAParameters parameters)

Source code where it until the error:
//——————————————————————*
// Verify certificates. *
//——————————————————————*

            // verify the CA certificate (output = public key van CA)
            byte[] lv_PkCA = VerifyCertificate(byte[], in_PkEUR);

            // verify the VU certificate (output = public key van VU)
            byte[] lv_PkVU = VerifyCertificate(byte[], lv_PkCA);


            //------------------------------------------------------------------
            // Verify the signatures
            //------------------------------------------------------------------

            // calculate hash with the SHA1 algorithm
            SHA1 lv_HashAlgoritm = new SHA1CryptoServiceProvider();

            byte[] lv_Modulus  = new byte[128];
            byte[] lv_Exponent = new byte[8];
            Buffer.BlockCopy(lv_PkVU, lv_PkVU.Length - 128 - 8, lv_Modulus , 0, lv_Modulus.Length);
            Buffer.BlockCopy(lv_PkVU, lv_PkVU.Length - 8      , lv_Exponent, 0, lv_Exponent.Length);

            // init RSA parameters
            RSAParameters lv_RSAKeyInfo = new RSAParameters();
            lv_RSAKeyInfo.Modulus  = lv_Modulus;
            lv_RSAKeyInfo.Exponent = lv_Exponent;

            // init RSA algoritme
            RSACryptoServiceProvider lv_RSA = new RSACryptoServiceProvider();
            lv_RSA.ImportParameters(lv_RSAKeyInfo);

For more reference, the content of the VerifyCertificate method.

private byte[] VerifyCertificate(
    byte[] in_Certificate,
    byte[] in_PublicKey)
{
    byte[] lv_CHR      = new byte[8];
    byte[] lv_Modulus  = new byte[128];
    byte[] lv_Exponent = new byte[8];
    Buffer.BlockCopy(in_PublicKey, in_PublicKey.Length - 128 - 8 - 8, lv_CHR     , 0, lv_CHR.Length);
    Buffer.BlockCopy(in_PublicKey, in_PublicKey.Length - 128 - 8    , lv_Modulus , 0, lv_Modulus.Length);
    Buffer.BlockCopy(in_PublicKey, in_PublicKey.Length - 8          , lv_Exponent, 0, lv_Exponent.Length);

    byte[] lv_Signature = new byte[128];
    byte[] lv_Cn        = new byte[58];
    byte[] lv_CAR       = new byte[8];
    Buffer.BlockCopy(in_Certificate,   0, lv_Signature, 0, lv_Signature.Length);
    Buffer.BlockCopy(in_Certificate, 128, lv_Cn       , 0, lv_Cn.Length);
    Buffer.BlockCopy(in_Certificate, 186, lv_CAR      , 0, lv_CAR.Length);

    for (int lv_Index = 0; lv_Index < lv_CAR.Length; lv_Index++)
    {
        if (lv_CAR[lv_Index] != lv_CHR[lv_Index])
            throw new Exception("Validation error: CAR not in public key.");
    }

    BigInteger lv_BiModulus   = new BigInteger(lv_Modulus);
    BigInteger lv_BiExponent  = new BigInteger(lv_Exponent);
    BigInteger lv_BiSignature = new BigInteger(lv_Signature);
    byte[] lv_Sr = lv_BiSignature.modPow(lv_BiExponent, lv_BiModulus).getBytes(lv_Signature.Length);

    if (lv_Sr.Length != 128)
        throw new Exception("The certificate coult not be validated: size of signature should be 128 bytes.");
    if ((lv_Sr[0]   != (byte)0x6A) ||
        (lv_Sr[127] != (byte)0xBC))
        throw new Exception("The certificate coult not be validated: invalid format.");

    byte[] lv_Cr = new byte[106];
    byte[] lv_H  = new byte[20];
    Buffer.BlockCopy(lv_Sr,   1, lv_Cr, 0, lv_Cr.Length);
    Buffer.BlockCopy(lv_Sr, 107, lv_H , 0, lv_H.Length);

    byte[] lv_C = new byte[164];
    Buffer.BlockCopy(lv_Cr, 0, lv_C,   0, lv_Cr.Length);
    Buffer.BlockCopy(lv_Cn, 0, lv_C, 106, lv_Cn.Length);

    // bereken de Hash van de public key
    SHA1CryptoServiceProvider lv_SHA1 = new SHA1CryptoServiceProvider();
    byte[] lv_Hash = lv_SHA1.ComputeHash(lv_C);

    // vergelijk de berekende hash met de hash in het certificaat
    if (lv_Hash.Length != lv_H.Length)
        throw new Exception("The certificate coult not be verified: hash length invalid.");
    for (int lv_Index = 0; lv_Index < lv_Hash.Length; lv_Index++)
    {
        if (lv_Hash[lv_Index] != lv_H[lv_Index])
            throw new DiantaException("The certificate coult not be verified: hash not invalid.");
    }

    return lv_C;
}

Thanks for your help in advance.

Kind regards,

René

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T01:56:58+00:00Added an answer on May 21, 2026 at 1:56 am

    The exponent can have one prepended 0, but not two or more.

    You are probably using an exponent of 65537 (99% of all RSA keypairs use that), so lv_RSAKeyInfo.Exponent is {0, 0, 0, 0, 0, 1, 0, 1}.

    Remove the extra zeroes, and your code should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that will upload files from my client to a web
I have a legacy VB6 application that uploads file attachments to a database BLOB
I have been using phpunit for a while, starting with 3.5.8, subsequently upgrading to
While in the final throws of upgrading MS-SQL Server 2005 Express Edition to MS-SQL
I use VS2008 at home and love it, while approaching work about upgrading 2005
While going through university and from following the development of SO, I've heard a
While Ctrl X works fine in vim under windows, Ctrl A selects all (duh).
I have been using Uploadify in my PHP application for the last couple months,
It's been bugging me for a while now, but the ASP.NET designer in VS
In Upgrading to Oracle JDBC thin driver results in SQLException: Unexpected exception while enlisting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.