我使用NodeJS Crypto模块在后端使用 RSA 进行加密和解密,并在前端使用JSencrypt进行前端 RSA 加密和解密
但问题是,每当我使用公钥在前端加密时,我的后端都会抛出此错误(PS:我在 NuxtJS 中使用它,因此使用导入函数。)
const { JSEncrypt } = await import('jsencrypt')
const rsa = new JSEncrypt({ default_key_size: 1024 })
rsa.setPublicKey(store.state.publicKey)
const xKey = rsa.encrypt(store.state.ticket)
Run Code Online (Sandbox Code Playgroud)
然后每当我尝试在后端使用这段代码进行解码时,它就会抛出这个
Error: error:04099079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error
Run Code Online (Sandbox Code Playgroud)
这是我使用 privateKey 进行 RSA 解码的后端代码
Error: error:04099079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error
Run Code Online (Sandbox Code Playgroud) 我正在使用 dotenv 来声明 JWT_SECRET 环境变量,它显示了标题中提到的错误。
.env
NODE_ENV="development"
JWT_SECRET="mySecretString"
Run Code Online (Sandbox Code Playgroud)
环境.d.ts
import { Secret } from 'jwt-promisify'
declare global {
namespace NodeJS {
interface ProcessEnv {
JWT_SECRET: Secret,
NODE_ENV: 'development' | 'production',
PORT?: number | string
}
}
}
export {}
Run Code Online (Sandbox Code Playgroud)
我在我的路由文件中使用即时签名令牌 JWT_SECRET
路由文件
NODE_ENV="development"
JWT_SECRET="mySecretString"
Run Code Online (Sandbox Code Playgroud)
这里的智能感知正在工作,但是当我运行应用程序或编译它时出现错误。
错误
error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'Secret'.
Type 'undefined' is not assignable to type 'Secret'.
32 const token = await jwt.sign({ id: newUser.id }, process.env.JWT_SECRET) …
Run Code Online (Sandbox Code Playgroud) node.js ×2
dotenv ×1
encryption ×1
express ×1
jsencrypt ×1
jwt ×1
node-crypto ×1
rsa ×1
typescript ×1