nov*_*ita 3 node.js cryptojs typescript
我已经在节点中实现了一种可以正常工作的加密方法,但由于 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
};
Run Code Online (Sandbox Code Playgroud)
这是我的解密方法,行不通
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();
}
Run Code Online (Sandbox Code Playgroud)
当我尝试解密时,我收到的消息是:
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'.
Run Code Online (Sandbox Code Playgroud)
我试图做到这一点 --> let iv = Buffer.from(textParts[0],'base64'); 但这就是我得到的破译结果
Decipheriv {
_decoder: null,
_options: undefined,
[Symbol(kHandle)]: {}
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过这个,但它也不起作用
let iv = Buffer.from(textParts.shift(),'base64') as string
Run Code Online (Sandbox Code Playgroud)
我是加密货币新手,任何帮助都会很棒。
预先非常感谢您。
TypeScript 告诉您 的返回类型textParts.shift()可能是undefined。您需要在undefined调用之前检查函数中的值是否不存在Buffer.from(),或者使用非空断言:
let iv = Buffer.from(textParts.shift()!, 'base64');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5407 次 |
| 最近记录: |