小编Get*_*ter的帖子

Nodejs `crypto.publicEncrypt` 不会采用 `ssh-keygen rsa` 生成的公钥

我曾经ssh-keygen rsa生成 RSA 密钥对。生成的公钥如下所示:

ssh-rsa AAAAB3NzaC1yc2EAAA...
Run Code Online (Sandbox Code Playgroud)

当我尝试crypto在 Node.js 中使用来加密纯字符串时,

const fs = require('fs');
const { publicEncrypt } = require('crypto');

const publicKey = fs.readFileSync('$path/to/publicKey').toString();

const encryptedToken = publicEncrypt(publicKey, Buffer.from('some plain string'));
Run Code Online (Sandbox Code Playgroud)

它会给出以下错误:

Error: error:0909006C:PEM routines:get_name:no start line
  at node:internal/crypto/cipher:78:12
  ...
 library: 'PEM routines',
 function: 'get_name',
 reason: 'no start line',
 code: 'ERR_OSSL_PEM_NO_START_LINE'
Run Code Online (Sandbox Code Playgroud)

我对密码学非常陌生,只知道公钥/私钥加密的一般概念,因此非常感谢任何建议。

编辑:

我知道它crypto附带了生成密钥对的方法,所以我想问题更多是关于为什么公钥ssh-rsa在这里不起作用。

rsa node.js cryptojs

2
推荐指数
1
解决办法
3036
查看次数

标签 统计

cryptojs ×1

node.js ×1

rsa ×1