Error parsing in RSACryptoServiceProvider.ImportParameters parameters with zero byte elimination?

I had some problems when RSA keys created using Security.Cryptography.RSAParameters parameters worked in most cases with RSACryptoServiceProvider . ImportParameters .

After a debugging heap, it turns out that the properties of this object require very specific byte buffer sizes. My ASN.1 parsing code has zero byte elimination. In fact, some RSAParameters fields work only after the zero byte is removed, while others do not work at all if the zero byte is removed.

Each so often one of the parameters has more leading zeros due to normal randomization and causes the received key to not work properly.

Is this something that is considered a mistake?

+4
source share
2 answers

Why are you messing with these null bytes? Proper DER encoding of a positive ASN.1 number may include one leading zero byte. Simply put, if the high byte of an integer is 128 or greater, then the high byte must be added to the encoding. Without this null byte, you have DER encoding of a negative integer.

+2
source

.NET requires that the size of each RSA parameter be the exact size (the size of a response key pair).

Therefore, sometimes you need to delete the leading 0x00 byte (for example, if the data comes from ASN.1, which requires a positive number 0).

But in other cases, you need to add an extra 0x00 bytes, because bytes are a (huge) number that can correspond to fewer bytes (1 in reality). This happens when analyzing data from PEM encoded files (base64), where the leading 0 is usually deleted.

Final answer: make sure you are given the expected length (gasket or gasket removal).

+2
source

All Articles