Why does pycrypto return the same encryption result when using two different public keys?

I've generated two key pairs with openssl:

openssl genpkey -algorithm RSA -out ndkey.pem -pkeyopt rsa_keygen_bits:1023 -pkeyopt rsa_keygen_pubexp:3

I using pycrypto to encrypt cipher with primitive RSA for two public key:

key1 = importKey(open('pkey.pem'))
pk1 = key1.publickey()
enc_data1 = pk1.encrypt(text, 1)
# print enc_data1[0]
f1 = open('encrypted.1', 'w')
f1.write(enc_data1[0])
f1.close()

key2 = importKey(open('pkey2.pem'))
pk2 = key2.publickey()
enc_data2 = pk2.encrypt(text, 1)
# print enc_data2[0]
f2 = open('encrypted.2', 'w')
f2.write(enc_data2[0])
f2.close()

print key1.n
print key2.n
print '({}, {}, {})'.format(key1.n, key1.e, bytes_to_long(enc_data1[0]))
print '({}, {}, {})'.format(key2.n, key2.e, bytes_to_long(enc_data2[0]))
print enc_data1 == enc_data2

Result:

22446587435783322535029926755522036093675835913290096371282649374261751886779792158268506193653741624694526861855388271128220343487285072455172348728517611812702642248438921952478917230370355877411774793976036676098428192127612807541759106243552886208613574681618562399678299666931976649794119197607602009750603778388548285240483011272063617594153157862753765694060756437470380792465477448634937898772420338613993398024699219918370034214455085868046146614573532274141958156157669859296150827240202401991892690879375285068011539679340140386717637063432283626051255917883375261840829071425328178052796156585434620172339
27173517653161146721399399483321106051348494833126656892132038343853315394541987081201985517940371665296451046896427324444405991983593681459775927250781326318134023191438693906225250446425515737352497760790346987933424933378740267054765665975297294346138266451296229352298302247469480080897832820929109398112519858525971582557120594702836884498254069262704265059341307982026489056555170273623524529550647132999158193858003667551632101384236498921133041585007229051277169868413556175970329043233718304735481129875181130029006556378684002155199475307210851127922382666215752506594372446312743297408737452007324664998721
(22446587435783322535029926755522036093675835913290096371282649374261751886779792158268506193653741624694526861855388271128220343487285072455172348728517611812702642248438921952478917230370355877411774793976036676098428192127612807541759106243552886208613574681618562399678299666931976649794119197607602009750603778388548285240483011272063617594153157862753765694060756437470380792465477448634937898772420338613993398024699219918370034214455085868046146614573532274141958156157669859296150827240202401991892690879375285068011539679340140386717637063432283626051255917883375261840829071425328178052796156585434620172339, 3, 8919260869834175894157597887369949260094875703418884180570952000)
(27173517653161146721399399483321106051348494833126656892132038343853315394541987081201985517940371665296451046896427324444405991983593681459775927250781326318134023191438693906225250446425515737352497760790346987933424933378740267054765665975297294346138266451296229352298302247469480080897832820929109398112519858525971582557120594702836884498254069262704265059341307982026489056555170273623524529550647132999158193858003667551632101384236498921133041585007229051277169868413556175970329043233718304735481129875181130029006556378684002155199475307210851127922382666215752506594372446312743297408737452007324664998721, 3, 8919260869834175894157597887369949260094875703418884180570952000)
True

What is this mean?

Upvotes: 0

Views: 116

Answers (1)

dave_thompson_085
dave_thompson_085

Reputation: 38841

Your "encrypted" value is the cube of the integer which is equivalent to the ASCII codes (in bigendian order) for the text "plaintext". Because your plaintext value is small and you ignored the advice in every competent source including Wikipedia and the pycrypto documentation and used 'textbook' RSA (no padding) with e=3 to make it even worse, the computation msg^e mod n did not actually wrap and use the mod n part and thus provides absolutely zero security.

Upvotes: 1

Related Questions