Note that there are some explanatory texts on larger screens.

plurals
  1. POExtracting RSA public key modulus and exponent stored in unsigned char arrays
    text
    copied!<p>So, I'm using a proprietary library that has its own implementation for the creation of RSA key pairs. The public key struct looks like this:</p> <pre><code>typedef struct { unsigned int bits; //Length of modulus in bits unsigned char modulus[MAX_RSA_MOD_LEN]; //Modulus unsigned char exponent[MAX_RSA_MOD_LEN]; //Exponent } RSA_PUB_KEY </code></pre> <p>I need to figure out a way to extract both the exponent and the module so I can send them to a server as part of a validation scheme. I guess that this is a pretty standard procedure (or so I hope). I've already read these two similar questions:</p> <ul> <li><p><a href="https://stackoverflow.com/questions/5661101/how-to-convert-an-unsigned-character-array-into-a-hexadecimal-string-in-c">How to convert an Unsigned Character array into a hexadecimal string in C</a></p></li> <li><p><a href="https://stackoverflow.com/questions/10375932/printing-the-hexadecimal-representation-of-a-char-array">Printing the hexadecimal representation of a char array[]</a></p></li> </ul> <p>But so far I've had no luck. I'm also not sure of how to use if at all necessary the "bits" field to extract the modulus. In short what I have to do is be able to recreate this public key in Java:</p> <pre><code>BigInteger m = new BigInteger(MODULUS); BigInteger e = new BigInteger(EXPONENT); RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e); KeyFactory fact = KeyFactory.getInstance("RSA"); PublicKey pubKey = fact.generatePublic(keySpec); return pubKey; </code></pre> <p><strong>Edit:</strong></p> <p>This is what I'm doing right now: (RSAPublic is a RSA_PUB_KEY struct as described above).</p> <pre><code>//RSAPublic.bits = length of modulus in bits log("Modulus length: "+std::to_string(RSAPublic.bits)); log("Key length: "+std::to_string(keyLengthInBits)); //Calculating buffer size for converted hexadec. representations int modulusLengthInBytes = (RSAPublic.bits+7)/8 ; int exponentLengthInBytes = (keyLengthInBits+7)/8; char convertedMod[modulusLengthInBytes*2+1]; char convertedExp[exponentLengthInBytes*2+1]; //Conversion int i; for(i=0; i&lt;modulusLengthInBytes ; i++){ sprintf(&amp;convertedMod[i*2], "%02X", RSAPublic.modulus[i]); } for(i=0; i&lt;exponentLengthInBytes ; i++){ sprintf(&amp;convertedExp[i*2], "%02X", RSAPublic.exponent[i]); } //Print results printf("Modulus: %s\n", convertedMod); printf("Exponent: %s\n", convertedExp); </code></pre> <p>And this is the output:</p> <pre><code>Modulus length: 16 Key length: 512 Modulus: 0000 Exponent: 0A000200FFFFFFFFFFFF0000600007004DDA0100B01D0000AEC642017A4513000000000000000000000000000000000000000000000000000000000000000000 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload