CryptographicException in RSA.ImportParameters() - Bad Data in special 1024 keys
Asked Answered
B

1

3

We have a C#/.Net 4.0 application which imports RSA Private Keys from a String in Base64 received in a WebService.

This application works perfectly for RSA-Keys in 1024 bits, but doesn't with a special kind of rsa private keys (around 1% of keys).

Here are the byte lengths:

Working-Key:

  • Modulus => 128 Bytes
  • Exponent => 3 Bytes
  • D => 128 Bytes
  • P => 64 Bytes
  • Q => 64 Bytes
  • DP => 64 Bytes
  • DQ => 64 Bytes
  • IQ => 64 Bytes

Not-Working-Key:

  • Modulus => 128 Bytes
  • Exponent => 3 Bytes
  • D => 127 Bytes
  • P => 64 Bytes
  • Q => 64 Bytes
  • DP => 64 Bytes
  • DQ => 64 Bytes
  • IQ => 64 Bytes

The difference is in the lenght of D (128 working, 127 not working). The not-working key is 1 byte shorter than the working key.

The parameters are set but when doing RSA.ImportParameters(rsaParams) it throws a CryptographicException with a "Bad Data" Message.

What should be included to solve this problem?

By answered 24/8, 2016 at 23:51 Comment(0)
W
9

RSACryptoServiceProvider has some assumptions on the data lengths which are:

  • Modulus: any even size, let's call the length n
  • Exponent: (<= 4 bytes; though RSACng allows "any size"), let's call the length e
  • D: n
  • P: n/2
  • Q: n/2
  • DP: n/2
  • DQ: n/2
  • InverseQ: n/2

So, assuming that your second key is actually Modulus: 128 bytes (because a 64-byte P times a 64-byte Q isn't a 256 byte number), you just need to left-pad the D array with a zero to bring it up to the proper length.

byte[] newD = new byte[modulus.Length];
Buffer.BlockCopy(d, 0, newD, newD.Length - d.Length, d.Length);

.NET Core has the source code available showing that relationship. In .NET Framework it's buried inside the CLR, so not available on referencesource.

Wilkie answered 25/8, 2016 at 2:55 Comment(1)
Yep. This usually forms because D is stored in as an INTEGER in ASN.1, and ASN.1 encoding rules specify that integers must be encoded in the shortest form possible. So in those cases where an integer is < the required size, it's likely because the integer has insignificant zeros.Hartfield

© 2022 - 2024 — McMap. All rights reserved.