# RSA modulus (N) from public key and calculating N from p, q not equal [closed]

I have a RSA public key in the form of public exponent and modulus as follows:

String exponent = "0000000000000003";
String modulus = "D8016FFDDFBA4B144B0AB13FF01968048B2FC9AA269311D11357DC8CEDCABA232BEC118D3B05AFA2406D27BFB6602B45B80E91D4F446E4A753C251EA6EB8AB8E3F304585DD202D8B04538EB4DD1F87D9A27E1E7B34A304396BBC38EE669E823BD03C1F43698B4B3128F3770C465CE415FD0F965F3170CED1470ED777751DE59D";


And also I have a private CRT key of the corresponding RSA key as follows:

String p = "F401F9E76A0E65D80AA8CF0D526D8D8747E53A3E1223B143AA73F675708ED966AB96965040907CCDF3D5C77904AA0906A6941E3A9C69AEC1F99E73E6EDB07191";
String dq = "9714C3F2C2BF5FF3C8C5D0936932727E044A34E3A2EFC99C1DEDF378A690CC3A975085CF496A519AFED047FF46815D77B02594571FC31017AAA5568C576B9E33";


This kind of key was generated for testing purposes.

So I would like to check if it is correct and I have found out that mulitplying of p with q would give different modulus N, resp. N is different only at one half. I used the following calculation:

byte[] bp = hexToByteArray(p);
byte[] bq = hexToByteArray(q);
byte[] bdp = hexToByteArray(dp);
byte[] bdq = hexToByteArray(dq);
byte[] bqinv = hexToByteArray(qinv);

BigInteger pBI = new BigInteger(bp);
BigInteger qBI = new BigInteger(bq);
BigInteger modulusBI = pBI.multiply(qBI);


And the resulting modulus modulusBI has the following value in HEX:

0160502A518CD54E933929557FE02EC03CDB4016A007B2233BFEF8E28362AE649D5CB2860C55B86BCE5EF447C7F4160B89421517A8389DC1DA2BDC30FDE6CCB03F304585DD202D8B04538EB4DD1F87D9A27E1E7B34A304396BBC38EE669E823BD03C1F43698B4B3128F3770C465CE415FD0F965F3170CED1470ED777751DE59D


The value is not same as original modulus N from public key, but one half of it is correct, as you can see:

Computed N :0160502A518CD54E933929557FE02EC03CDB4016A007B2233BFEF8E28362AE649D5CB2860C55B86BCE5EF447C7F4160B89421517A8389DC1DA2BDC30FDE6CCB0 3F304585DD202D8B04538EB4DD1F87D9A27E1E7B34A304396BBC38EE669E823BD03C1F43698B4B3128F3770C465CE415FD0F965F3170CED1470ED777751DE59D
Original N :D8016FFDDFBA4B144B0AB13FF01968048B2FC9AA269311D11357DC8CEDCABA232BEC118D3B05AFA2406D27BFB6602B45B80E91D4F446E4A753C251EA6EB8AB8E 3F304585DD202D8B04538EB4DD1F87D9A27E1E7B34A304396BBC38EE669E823BD03C1F43698B4B3128F3770C465CE415FD0F965F3170CED1470ED777751DE59D


The second half of modulus is correct. Why is that? Am i doing some mistake in multiplying primes p and q?

• This is a programming question mostly unrelated to cryptography and should therefore be asked on Stack Overflow. (The numbers are correct, though — that is, $n=pq$ and your code is broken.) – yyyyyyy Apr 16 '15 at 10:29
• Meh, try new BigInteger(1, bp);, same for all other BigInteger calls. – Maarten Bodewes Apr 16 '15 at 10:46
• Endianness or sign bits are two common error sources. – CodesInChaos Apr 16 '15 at 10:51

bc on unix:

obase=16

ibase=16

p=F401F9E76A0E65D80AA8CF0D526D8D8747E53A3E1223B143AA73F675708ED966AB96965040907CCDF3D5C77904AA0906A6941E3A9C69AEC1F99E73E6EDB07191