Help us understand the problem. What is going on with this article?

node.js で公開鍵/秘密鍵によるデータの暗号化/復号

More than 3 years have passed since last update.
import keypair from 'keypair'
import crypto from 'crypto'

const encrypt = (raw, publicKey) => {
  const encrypted = crpyto.publicEncrypt(publicKey, new Buffer(raw))
  return encrypted.toString('base64')

const decrypt = (encrypted, privateKey)=> {
  const decrypted = privateDecrypt(privateKey, new Buffer(encrypted, 'base64'))
  return decrypted.toString('utf8')

const pair = keypair()
const data = 'foo bar baz'

const encrypted = encrypt(data, pair.public)
const decrypted = decrypt(encrypted, pair.private)

注意: 簡単のため npm の keypair モジュールを使って公開鍵、秘密鍵を生成しているが、メモリ操作などによって脆弱な鍵を生成させられる可能性が排除できないので、 実際には openssl などの信頼できる実装を使ってください。

CXプラットフォーム「KARTE」の開発・運営、EC特化型メディア「Shopping Tribe」の企画・運営、CX特化型メディア「XD(クロスディー)」の企画・運営
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away