0

I am re-writing a Java application with NodeJS. I need to encrypt a message using these parameters but I cannot get it work.

{
    "oaepHashAlgo": "SHA256",
    "pubKeyIndex": "0004",
    "Sid": "0001nEDMjbDVqwgpjLTOKNr5_2z63SykIz2PNsKqDqEzlORG051o8drUp_8OwgXusTGzCSTlrmJMqkolMHTzb00FjEKWdds",
    "random": "586AADD845626E05A154DD2517F0C9B0",
    "pubKey": "82ADCD6B111C6659B51D11E833F3B4C572AA0D6A018DC3BD3653E21F9A9B76D5ED96E16CB594AABD2AE68075D8892567C2F469941466C36AFAD9AECC420BB9EEE86D21EA8F5082C241F50A50557FEEDBE77DB7061BE8FF1AE2F1D027DA27DC0D4F9FB4ED2C792A41E1C760302B74C23B2854A1DE1E7157E6DC4ED47CBCD9DF0D8B41D6C36CEBD5941FDCEC704007F4DF0F8ECD945A2500E59CB01E1279A28A625C2522AB4DFB118D0632C4BCC3E8DE78E6D201D0D128F7E94F2B939C95FA71E644B1A02479EC83A21F168A6E52222D4DD74F7284B999EFC99210C76A25B61B950A13863107133B44D2365B1E811B7B307A3497C7371A963751D01DEB7F3CCA3F,00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010001"
}

From these parameters I guess I will need to do RSA-Oaep encryption. As you can see the first half of pubKey is likely to be an HEX code. so I turn it to buffer but still no clue what format it is.

const buff = parseHexString('82ADCD6B111C6659B51D11E833F3B4C5...'))
// => <Buffer 82 AD CD 6B 11 1C 66 59 B5 1D 11 E8 33 F3 B4 C5...

crypto.publicEncrypt({
  key: buff,
  oaepHash: oaepHashAlgo,
}, Buffer.from("MyDataHere"))

// => Error: error:0909006C:PEM routines:get_name:no start line 

As expected, I got error for not passing a valid pem file.

So, What is format of this public key? and how do I use it?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Hereblur
  • 2,084
  • 1
  • 19
  • 22
  • 3
    The public key does not seem to have the correct format. `publicEncrypt` requires the PKCS#1 or X.509/SPKI format, PEM or DER encoded. Your key data seems to be modulus and exponent (hex encoded). You need a library (e.g. Node-RSA) or a tool (e.g. [here](https://superdry.apphb.com/tools/online-rsa-key-converter)) for the conversion. – Topaco Dec 10 '20 at 18:36
  • Yes, the public key was in mudulus/exponent format. Now I can import the key using node-rsa. Thank you. – Hereblur Dec 10 '20 at 19:15
  • 1
    @Topaco Please do post such things as answers instead. That way the question gets answered, which is useful to others, both for me (looking for interesting unanswered questions) and of course people running into the same problem. It also allows me to vote up :) Of course, you can also do this after making a suggestion if you're not sure. – Maarten Bodewes Dec 10 '20 at 23:22

0 Answers0