Archived Forum Post

Index of archived forum posts


RSA export and Public Key Export

Sep 24 '14 at 10:55

Hi, I'm generating a 4096 bit RSA key. The process succeed and I use (IOS but same result on C#) the exportPublicKey. I after add the KEY into a Public Key object (CkoPublicKey) using LoadXML again succeed. Now I extract the publickey from the newly generated CkoPublicKey object using the GetXml and I got a different key.

any idea why? I suppose to get the same key.

Here the code CkoRsa rsa = [[CkoRsa alloc]init]; CkoPublicKey PubKey = [[CkoPublicKey alloc]init];

BOOL success;


rsa.EncodingMode = @"base64"; success = [rsa GenerateKey: [NSNumber numberWithInteger: 4096]];

NSString *Xml = [rsa ExportPublicKey]; success = [PubKey LoadXml: Xml];

NSString *Xml1 = [PubKey GetXml];

// NOW XML and XML1 are different.

Accepted Answer

There is actually no problem. The Modulus of the public key appears to be different, but in fact it is not. The Modulus is an extremely large positive integer. (You might hear it referred to as a "BigNum" or a "BigInt".) Assuming the same byte ordering, an integer can be represented with any number of leading 0 bytes, and it is still the same integer. For example, the number 4 is represented in a 4-byte integer as 0x00000004. It is represented as a 2-byte integer as 0x0004. Both are the same integer.

If you base64 encode different representations of the same integer, the result is that the base64 encoding looks entirely different.

If you decode from base64 the Modulus in each case, and then encode each to a hex representation, you'll find something like this:

Modulus1:  CD900A0F53B6E...
Modulus2:  00CD900A0F53B6E...

One has a harmless leading 0 byte, but it is still the same Modulus (it just looks completely different when base64 encoded.