node.js - 'string | 类型的参数 undefined' 不可分配给 'ArrayBuffer | 类型的参数 共享数组缓冲区'
问题描述
我已经在节点中实现了一种可以正常工作的加密方法,但是由于 typeScript 问题,我无法解密。这是我的加密方法,效果很好,我正在遵循的例子
private encryptWifiPassword = (param1: string, password: string) => {
const ENCRYPTION_KEY = param1.padStart(32, '0'); // Must be 256 bits (32 characters)
const IV_LENGTH = 16; // For AES, this is always 16
const iv = crypto.randomBytes(IV_LENGTH);
const cipher = crypto.createCipheriv('aes-256-gcm', Buffer.from(ENCRYPTION_KEY), iv);
let encrypted = cipher.update('prefix'+ password);
encrypted = Buffer.concat([encrypted, cipher.final()]);
const pskEncripted = iv.toString('base64') + ':' + encrypted.toString('base64')
return pskEncripted
};
这是我的解密方法不起作用
private decryptWifiPassword(param1: string, password: string): string {
const ENCRYPTION_KEY = param1.padStart(32, '0')
let textParts = password.split(':');
let iv = Buffer.from(textParts.shift(),'base64'); //this is the part with the issue
let encryptedText = Buffer.from(textParts.join(':'), 'base64');
let decipher = crypto.createDecipheriv('aes-256-gcm', Buffer.from(ENCRYPTION_KEY), iv);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}
当我尝试解密时,我收到的消息是这样的:
No overload matches this call.
Overload 1 of 5, '(arrayBuffer: ArrayBuffer | SharedArrayBuffer, byteOffset?: number | undefined, length?: number | undefined): Buffer', gave the following error.
Argument of type 'string | undefined' is not assignable to parameter of type 'ArrayBuffer | SharedArrayBuffer'.
Type 'undefined' is not assignable to type 'ArrayBuffer | SharedArrayBuffer'.
Overload 2 of 5, '(obj: { valueOf(): string | object; } | { [Symbol.toPrimitive](hint: "string"): string; }, byteOffset?: number | undefined, length?: number | undefined): Buffer', gave the following error.
Argument of type 'string | undefined' is not assignable to parameter of type '{ valueOf(): string | object; } | { [Symbol.toPrimitive](hint: "string"): string; }'.
Type 'undefined' is not assignable to type '{ valueOf(): string | object; } | { [Symbol.toPrimitive](hint: "string"): string; }'.
Overload 3 of 5, '(str: string, encoding?: "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex" | undefined): Buffer', gave the following error.
Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
Type 'undefined' is not assignable to type 'string'.
我试图做到这一点-> let iv = Buffer.from(textParts[0],'base64'); 但这就是我得到的破译
Decipheriv {
_decoder: null,
_options: undefined,
[Symbol(kHandle)]: {}
}
我也试过这个,但它也不起作用
let iv = Buffer.from(textParts.shift(),'base64') as string
我是加密新手,任何帮助都会很棒。
非常感谢您提前。
解决方案
TypeScript 告诉你返回类型textParts.shift()
是可能的undefined
。undefined
您需要在调用之前检查您的函数该值不是Buffer.from()
,或者使用非空断言:
let iv = Buffer.from(textParts.shift()!, 'base64');
推荐阅读
- spring - 在AuthenticationProvider中成功认证后是否需要将原始凭据放入UsernamePasswordAuthenticationToken?
- objective-c - Xcode 错误 kCFNumberFormatterCurrencyStyle
- docker - 通过 setup.yml 将保存的对象导入 Kibana
- node.js - 如何在 php/apache 服务器上部署 nodejs 应用程序及其静态文件?
- javascript - 将列表中的项目循环到控制器中的 ViewBag 中
- node.js - jsonwebtoken: expiresIn 不会过期?
- python - 使用 Tkinter 从此脚本打开一个窗口
- tinymce - 在单个页面上使用多个 TinyMCE 编辑器
- java - 如何从头开始构建一个自包含的 java 程序的目录结构?
- python - 使用 pandas/Numpy 在日期上创建资源钟分布