alex.mironov
alex.mironov

Reputation: 2942

How to sign a message with public key RSA

I'm new to cryptography and trying to perform simple RSA sign of the message with the public key.

const PublicKey = "-----BEGIN PUBLIC KEY-----GIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCS7h56MiVTsV1SZ9gvWidH5XheZIpq1PUjKA//6m5N5HYkDtWQ9d+jx6niKDRUYf+aVc8StDKvIRJGT2ZZtIJ27FG9VpvZhR5yG38sh51MAPqZ/rpIzXg2Vj9dR2y3IUyWAafJ/VVGlecSYWREK1t6aMi7piHZpP/Rvn+1ImUTQIDAQAB-----END PUBLIC KEY-----"
const MessageKey = "4FSF5BE55B26FBABA402C23E1FF85E8F"

with a help of jsrsasign library I achieved this:

  const publicKey = RSA.KEYUTIL.getKey(PublicKey)
  const encrypted = RSA.KJUR.crypto.Cipher.encrypt(MessageKey, publicKey, "RSA")
  console.log(RSA.hextob64(encrypted))

But I'm afraid it's incorrect. Has someone performed encryption of message with public key in JS?

Upvotes: 1

Views: 1945

Answers (1)

c2h2
c2h2

Reputation: 12339

sign with private key, encrypt with public key

to gen a pub key (with openssl and id_rsa):

openssl rsa -in ~/.ssh/id_rsa -pubout > ~/.ssh/id_rsa.pub.pem

to encrypt:

cat plain.txt | openssl rsautl -encrypt -pubin -inkey ~/.ssh/id_rsa.pub.pem > cipher.txt

to decrypt:

cat cipher.txt | openssl rsautl -decrypt -inkey ~/.ssh/id_rsa

Upvotes: 1

Related Questions