1

I`m trying to encrypt data via RSA using public key (128-bytes == 1024-bits) received from auth server.

Here is a code in Node.js:

const NodeRSA = require('node-rsa');

const openData = Buffer.from('example');
const rsaPublicKey = Buffer.from('04 D4 8B 30 F6 1C 89 8B 36 0B 32 BB 64 25 ED C0 76 1D 23 76 A9 49 D4 E7 24 99 24 C4 2E D7 D8 90 96 AF EE 53 3F 65 CE 3F 42 34 AB 56 47 7B 9A DD D5 7C 97 21 6F 37 2D 5A 7A E0 72 08 38 7A 18 85 AA FF C8 14 96 84 BB 65 33 68 11 E5 C4 9F CE 9F 19 1A C7 29 A5 13 80 4B D4 7E 8C 63 81 A1 FE 99 7D 11 35 46 08 93 BF D2 23 28 47 04 B4 B6 2B EF 5D 30 CF 33 CB D5 0E 28 A6 87 63 62 22 1E 46 74'.split(' ').join(''), 'hex');

const key = new NodeRSA();
key.importKey({
    n: rsaPublicKey,
    e: 65537
}, 'components-public');

const encryptedData = key.encrypt(openData);

But I`ve got error:

/Users/xok/node_modules/node-rsa/src/NodeRSA.js:283
            throw Error('Error during encryption. Original error: ' + e);
            ^

Error: Error during encryption. Original error: Error: error:0306E06C:bignum routines:BN_mod_inverse:no inverse
    at NodeRSA.module.exports.NodeRSA.$$encryptKey (/Users/xok/node_modules/node-rsa/src/NodeRSA.js:283:19)
    at NodeRSA.module.exports.NodeRSA.encrypt (/Users/xok/node_modules/node-rsa/src/NodeRSA.js:238:21)

If I use another key received from the same auth server, then all is OK:

const rsaPublicKey = Buffer.from('41 F5 0E FC 66 16 4D 28 89 E8 50 C9 8A CD C7 64 A7 5B D8 D0 98 4E 29 9F 52 FC 24 6C EA A5 5B 23 CD 37 B5 1E 9F F9 61 C5 FD C7 95 35 51 13 A0 4A 94 7E FA 23 92 0E DA 4E AD B8 98 86 6F EC 7D D4 C3 DA BF 98 01 A0 3F 8C 7A EC EE CB 53 2F 26 4C 66 2D D6 48 48 25 02 09 85 35 9F 6F F8 5F F7 1B BD 0A E0 02 61 B8 81 6A EE B2 F3 B0 BA EF 18 25 48 B6 1B 73 CB 32 33 E7 13 A7 3B D1 D7 D8 95 A9'.split(' ').join(''), 'hex');

Question: Could you say what I`m doing wrong, please?

xOk
  • 19
  • 2
  • 1
    I checked [here](http://crypt-online.ru/en/crypts/rsa/) and encryption succeeded. So, can it be a problem of "node-rsa" framework? – xOk Nov 01 '20 at 09:10
  • I think, I parse public key received from server incorrectly... I`m checking this moment... – xOk Nov 01 '20 at 10:35
  • I`ve got it... I parsed public key from byte-stream incorrectly.. Now I parse correctly, so there is no problems left. – xOk Nov 01 '20 at 16:16

0 Answers0