tags:

views:

661

answers:

1

in a short question: How can I fill in RSAParameters if I have having the following input from the third party?:

Modulus: 123456

Exponet: 111

In a long story, I use the following code:

RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(2048);

RSAParameters rsaPublic = RSAalg.ExportParameters(false);

then I can get modulus and exponent of the rsa public key in byte[]. In order to write these information in the asn.1 format

      RSAPublicKey ::= SEQUENCE {
           modulus           INTEGER,  -- n
           publicExponent    INTEGER   -- e
       }

I use a asn.1 library to convert byte[] to its bigInteger format, but this byte[] should be in the format like {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'} assuming it is decimal.

But it seems like rsaPublic.modulus and rsaPublic.exponent are not in this format, there are many not-digits in the byte[] of the modulus and exponent. So what is the format of rsaPublic.modulus and rsaPublic.exponent, and how convert them into a byte[] with the format like {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}?

Thanks a lot

A: 

The format is specified in the ASN.1 reference. The format used is called the DER encoding, and for Integers. A reference is available to these ugly encodings. Are you sure there isn't an easier way to get these values as bigints?

EDIT

It looks like you can get the byte[] arrays by getting an RSAParameters object via RSACryptoServiceProvider.ExportParameters() and then extracting from this the Exponent and Modulus fields.

GregS
I use a asn.1 library. It can easliy handle asn.1 public key format. But the problem is I don't know how to convert .net modulus in byte[] to a reasonable byte[] like {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}, rsa modulus in byte[] seems unmeanlingful, is it encoded in some format?
travellover
It is basically the integer as a big-endian byte array, with some header bytes at the front. Equivalently, it is the integer represented as a base-256 number. What do you want to do with the byte[], then maybe I can give a better clue.?
GregS
I need them to generate PKCS#1 asn.1 RSA public key by a .net RSA modulus and exponent.my way is to use a asn.1 libray. it can accept modulus and exponent in byte[] in base-2, base-18, base-10, or base-16, and then generate his own big integer, then it can generate PKCS #1 public key format.so first I want to generate biginteger by .net RSA modulus and exponent..net RSA modulus and exponent are in byte[], but not all elements in these byte[] are ASCII digits, right?I hope you can understand what I am saying.
travellover
in a short question: How can I fill in RSAParameters if I have having the following input from the third party?: Modulus: 123456Exponet: 111
travellover
GregS, I think you maybe right, RASParameters.modulus and RASParameters.exponent seems like only the big-endian base-256 numuber in byte[], that is RASParameters.modulus[0] is the most significant bit, there is no any information. Am I right?
travellover
Correct, modulus[0] is the most significant byte, modulus[modulus->Length - 1] is the least significant.
GregS