Extracting Modules and Component(RSAParameter) from X509Certificate PublicKey

Description

In some applications, it might be necessary to extract the modulus and exponent from the X509Certificate PublicKey. The X509Certificate.GetPublicKey() returns a byte array that contains the ASN.1 Encoding information along with the modulus and exponent as described in the RFC2459.

According to the RFC, Section 7.3, the RSA public key shall be encoded using the ASN.1 type RSAPublicKey:

RSAPublicKey::= SEQUENCE {modulus INTEGER, --npublicExponent INTEGER -- e -- }

where modulus is the modulus n, and publicExponent is the public exponent e.

The X509PublicKeyParser parses this information and returns the modulus and exponent as a RSAParameters object.

The following code snippet shows how to use the parser

BinaryReader br = new BinaryReader(File.OpenRead(certfile[0]));
byte[] buf = new byte[br.BaseStream.Length];
br.Read(buf, 0, buf.Length);
X509Certificate cert =
new X509Certificate(buf);
RSAParameters p = 509PublicKeyParser.GetRSAPublicKeyParameters(cert);

The Zip file contains two project, one for the Parser and the other is a ParserTester. To Run the Tester type,

X509ParserTester cert1.cer

cert1.cer is the filename of the Certificate. Two certificates cert1.cer and cert2.cer is included in the Debug folder to testing.

RSapPa1.jpg


Similar Articles