Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

cryptography - Private key generated by openssl does not satisfy n = p * q

I've generated a key with openssl by using the following command

openssl genrsa 1024

I've got the following result

-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQC2Lh4HLcCR76Wv3oXl6sZ7pv8l9b/66H+I6Bb86tz8RhWCmTCG
xPVgtZ+w9WutU/rqBHHZOpotX4QDksRD8dRFh6a3HwkIFQdTcDoiD39yNP8F02Gd
dAl8p/URC5jNCLMSfUK38wMocmoI1I5vqdMNrzUnOup18rl4089Z+faMKQIDAQAB
AoGBAJrL4z5iWiengxqV8yETBeU8WcJft+n0dapXzHDNAUo8Izr+AIYEBp1Ot4se
f4igu6zuae80JJ45c2u14p/5dWzN9/URmhTP8xLGjGCjltOJLLyhBPP+ZsLjqu6l
57MNV6jDqDLdRC66w4NMRCN3FACxcldIC5L6B9OA7UvO1ugBAkEA8s1o6BvufTeD
ktyOMfW0ZeLGk/6EXp8nf7BIzYhAAPtSt8DegnfYFx2XDgKnYSB15dTDIDMe/KaA
GcuUb/ZjiQJBAMATb1fFdl7PGccVPgeTaupJayHPySc9PSsACV6VIAnpcU/3NZzd
MkdIL/JsOsD+1M9uQJqvRZO4qQdjcR5Om6ECQEJGgYlB/pJdcePHomTOvcRF55CE
G9u8M8rt8qFvvJDICWcxFUulrO16XT4syUWA1825it2iNqYeSL9By63YIokCQBnm
RPw71xM/r8UleyDAYwlGbxi3EPOmkUnsDldfmltby/ixZ9xIA1CTTkvNBjsh4YY6
4qE5AxPBMaGaahVhGiECQHyQvXUNE2IUco+ZwmmLnb2ey1s5tBg7i0lEZG47G7GV
o+yKdJHpToJD8eRJA/2D+pMhQIZm8X/XDIoi1AQ/TRY=
-----END RSA PRIVATE KEY-----

By using http://lapo.it/asn1js/ I've decompiled above pem and found out that

p = 12716592588957205057720227362856602359162165918588008158928004904780617816265357754968000322907349867949577092305558696339499022301055839537975977118688137

q = 10059832080410675679274931119486090017360564066559599189867309386706046720813347145402059918876964980032136051476889631968099168811652389989474036177869729

But in the mean time the modulus

n = 127931248913253271289716500205639992466284651287857358177145251825312810367993147780821624164062592337708505486275588028025122128928225266601591073331803580493341719724935049328478344297205955905466581637169109448199715137939448946445804542355907923908845024638480376219852266194827768486624319018352514599977

When it should equal:

N = 127926786079904340486324750015310657558743663036816604629670248604945247855865212421871439992814824964459404054462886419168595224468110358113599485755243718536935880977456835036272916419632947427885989469252242704342884476060911074859116149906854382812141105735147419775039435625802885168567201322191763704873

Why the hell N is not equal to p * q ?


EDIT:

Here is bunch of keys generated by my openssl, I'm using Mac OS X Mavericks, the first key has correct N, the second one incorrect, I haven't checked the others, but anyway this issue seems to be reproducing, I'm using the following openssl version "OpenSSL 0.9.8y 5 Feb 2013":

-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDb4ZTDCASpFV+89RzD8MYR/xASV/piyXB3+qVq+PueDqq+45EL
Xt5UE2jXanfOh3uE3G3Fi76rm08XUC0CvFB7DxFJAMvchr5LbE9x2EMDmfpuZPOI
bDI+r+XZ+KJF++h296TOnncgnXILoD1gihkld7MS9g49TwsBPOtsMJzDRQIDAQAB
AoGBAMpGFfTormjhacZJcqHqIDwz+RP27TbtetJUuNHUpJw9f+PFMwxTxJFxnZP1
tKtAxWdu5GjDaq8xW6vlrNKEbD9jg2WU/myYffNeHxNVjivzypeQzBSmcE5LKFpM
objHkvXbBkkyX8Oh6ZF/3+NOhxOVZMH7CaZ90a+xPf7ENXJBAkEA+Zx/rwrx81+D
s79thCAUU4EnMVROGFPr/V7mt9AFXjAXHRuvg49Aon23bt48jdroakV5vk0ltKO8
Z5xnRWlGNQJBAOGCSOL+zFJd6zTM+CkbgGtml+DIunr7Avfu2mZyaMv4mLo+GTRN
ctZEKhknYfydewhORLKg51WdLThoe9VG6tECQCUyIvzMnr1rkE6qygEUXrMK3fJF
49VQPCxnhe4CO6qLdAZMZ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+lnumc2YQ==
-----END RSA PRIVATE KEY-----

-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCWVkOkNXkWd2QoWudAD8rrzioby2g8k1pruaclCMo2mSns++TG
S3lWgLqS2mPynNONVbqoOedLKKnGY9HYzLCOCuoq5loOYCtIcVyBVGhx4eQrZgmU
AwRqQChztrj09+omveQQF2+QW9DJZrYSsKB836iTt5oAEA3ER6fwcV3fLQIDAQAB
AoGAQKNife4H3CMFS1yaZS7lHrIL5Nt2O+VXUU4HkaLP3t3H6v77BE9bIlhC3/VK
V/wZ8YDUupP64JPhO02b4k1P83oG5SAFTb0zZwmHI1+wVnNomq0DoRPw2710a2P4
Ya09nLbuRlm6SzsAkuwmyv+fPDas8b/m15kWSP/HTKMbAPkCQQDEUaTQUg/8sPH+
jDraHpjGtVRqjkysAZhNgMH+NGWLbk3nujZj4oMfid+4Xg4/Sr5EADqr0HrSlMnV
hhlwBCxjAkEAxAodCSmu9khiuRzMzXAc/gvqdtq+fF6Zx+rTUF5HYeD7l1u3SXes
whDku23Pp41+A4axWHXHTsDlKv49m9YzLwJBAJWIEl9SgjbYmBpouVL2RyvjZf8b
kC7bsCQoVJrJUicXx2MfpNoD6BgXrj4B9hwjVDT7b7T6wPKwveNTlwCJTT0CQGNJ
W/tQCTTi5F0soO1LYuOn1U/s6ZzDrM77t5p0pWKFN+copGOpIO4I8E+dBhauKqxO
g3Nx80eHmolzynAqxDECQEmyZq1mFEpB6buwNhRmj/kyOGmA88IbovZhBT+Un/6o
heaaMVl7Hst7nhihxQ081c3mFuwnH/mjixKKkpVens8=
-----END RSA PRIVATE KEY-----

-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQCm7mMUqExF5Nq2qzQ8gLVfq5AqKQlKm47Df4H/XqH9msGNCoIx
tnH4Tbe38tzA9TDRxxYQ6oCmTRUlKqstxb0j8NK7KV63vrxhLUlCIinMk9qcTL/T
tLm7/rBSZ7M6OjupzjjQ8Idu7Jd7HVITvFDYbD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Nh
3JPMdw9hZ2fCD5HxzJEdLE8cLs81iRyfdAbnzCo5YIeA68kGkJz4PRUMr0t1AkBC
hfcvZPmiv9bHUTbwKIRbjU78qFkDP7iBpfjOITBcyJUSODWhadhXh15IxrJwJV2O
0aU0Tmfx0PkQEb+CWDfJAkEAzJPemI3yVYqXG2POBolG5gK2VkHEw7QCQOmMK60Z
+3H7U9F/AvoM3zpRujHBdRxdZlPSrCvbxhyavYhNXDqnjg==
-----END RSA PRIVATE KEY-----
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Why the hell N is not equal to p * q ?

Confirmed with the key provided. However, I could not duplicate when generating my own key with openssl genrsa 1024.

$ ./test-rsa.exe

***** P *****
12716592588957205057720227362856602359162165918588008158928004904780617816265357
754968000322907349867949577092305558696339499022301055839537975977118688137

***** Q *****
10059832080410675679274931119486090017360564066559599189867309386706046720813347
145402059918876964980032136051476889631968099168811652389989474036177869729

***** N *****
12793124891325327128971650020563999246628465128785735817714525182531281036799314
77808216241640625923377085054862755880280251221289282252666015910733318035804933
41719724935049328478344297205955905466581637169109448199715137939448946445804542
355907923908845024638480376219852266194827768486624319018352514599977

*** Calc N ***
12792678607990434048632475001531065755874366303681660462967024860494524785586521
24218714399928148249644594040544628864191685952244681103581135994857552437185369
35880977456835036272916419632947427885989469252242704342884476060911074859116149
906854382812141105735147419775039435625802885168567201322191763704873

$ cat test-rsa.c

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

#include <openssl/bn.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>

const char s_key[] = "-----BEGIN RSA PRIVATE KEY-----
"
"MIICXAIBAAKBgQC2Lh4HLcCR76Wv3oXl6sZ7pv8l9b/66H+I6Bb86tz8RhWCmTCG
"
"xPVgtZ+w9WutU/rqBHHZOpotX4QDksRD8dRFh6a3HwkIFQdTcDoiD39yNP8F02Gd
"
"dAl8p/URC5jNCLMSfUK38wMocmoI1I5vqdMNrzUnOup18rl4089Z+faMKQIDAQAB
"
"AoGBAJrL4z5iWiengxqV8yETBeU8WcJft+n0dapXzHDNAUo8Izr+AIYEBp1Ot4se
"
"f4igu6zuae80JJ45c2u14p/5dWzN9/URmhTP8xLGjGCjltOJLLyhBPP+ZsLjqu6l
"
"57MNV6jDqDLdRC66w4NMRCN3FACxcldIC5L6B9OA7UvO1ugBAkEA8s1o6BvufTeD
"
"ktyOMfW0ZeLGk/6EXp8nf7BIzYhAAPtSt8DegnfYFx2XDgKnYSB15dTDIDMe/KaA
"
"GcuUb/ZjiQJBAMATb1fFdl7PGccVPgeTaupJayHPySc9PSsACV6VIAnpcU/3NZzd
"
"MkdIL/JsOsD+1M9uQJqvRZO4qQdjcR5Om6ECQEJGgYlB/pJdcePHomTOvcRF55CE
"
"G9u8M8rt8qFvvJDICWcxFUulrO16XT4syUWA1825it2iNqYeSL9By63YIokCQBnm
"
"RPw71xM/r8UleyDAYwlGbxi3EPOmkUnsDldfmltby/ixZ9xIA1CTTkvNBjsh4YY6
"
"4qE5AxPBMaGaahVhGiECQHyQvXUNE2IUco+ZwmmLnb2ey1s5tBg7i0lEZG47G7GV
"
"o+yKdJHpToJD8eRJA/2D+pMhQIZm8X/XDIoi1AQ/TRY=
"
"-----END RSA PRIVATE KEY-----
";

int main(int argc, char* argv[])
{
    UNUSED(argc), UNUSED(argv);

    int rc;
    FILE* fd = NULL;
    EVP_PKEY* pkey = NULL;
    RSA* rsa = NULL;
    BIGNUM* n = NULL;
    BN_CTX* ctx = NULL;

    fd = fopen("./key.pem", "w+");
    if(fd == NULL) exit(1);

    rc = fwrite(s_key, 1, sizeof(s_key), fd);
    if(rc != sizeof(s_key)) exit(2);

    rc = fseek(fd, 0, SEEK_SET);
    if(rc != 0) exit(3);

    pkey = PEM_read_PrivateKey(fd, NULL, NULL, NULL);
    if(pkey == NULL) exit(4);

    rsa = EVP_PKEY_get1_RSA(pkey);
    if(rsa == NULL) exit(5);

#if 0
    fprintf(stdout, "
***** RSA *****
");
    RSA_print_fp(stdout, rsa, 0);
#endif

    fprintf(stdout, "
***** P *****
");
    fprintf(stdout, "%s
", BN_bn2dec(rsa->p));

    fprintf(stdout, "
***** Q *****
");
    fprintf(stdout, "%s
", BN_bn2dec(rsa->q));

    fprintf(stdout, "
***** N *****
");
    fprintf(stdout, "%s
", BN_bn2dec(rsa->n));

    n = BN_new();
    if(!n) exit(6);

    ctx = BN_CTX_new();
    if(ctx == NULL) exit(7);

    rc = BN_mul(n, rsa->p, rsa->q, ctx);
    if(rc != 1) exit(8);

    fprintf(stdout, "
*** Calc N ***
");
    fprintf(stdout, "%s
", BN_bn2dec(n));

    BN_CTX_free(ctx);
    BN_free(n);
    RSA_free(rsa);
    EVP_PKEY_free(pkey);
    fclose(fd);

    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...