首页 > 解决方案 > '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

我是加密新手,任何帮助都会很棒。

非常感谢您提前。

标签: node.jstypescriptcryptojs

解决方案


TypeScript 告诉你返回类型textParts.shift()是可能的undefinedundefined您需要在调用之前检查您的函数该值不是Buffer.from(),或者使用非空断言

let iv = Buffer.from(textParts.shift()!, 'base64');

推荐阅读