Web Crypto:Ionic 应用程序后台模式下 AES-GCM 解密失败?

问题描述 投票:0回答:1

我目前面临着 Web Crypto API 的挑战。在尝试执行解密时,我注意到当离子应用程序位于前台时,它会按预期运行。然而,当应用程序移至后台时,解密过程似乎停止了,给人的印象是应用程序已冻结。

有趣的是,当我将应用程序返回前台时,我会立即收到所有待处理的通知。这种行为令人费解,我正在寻求帮助来理解和解决这个问题。

任何帮助将不胜感激。谢谢你。

this.firebaseX.onMessageReceived()
            .subscribe(async data => {
                this.ngxLogger.info('PUSH: onMessageRecieved:'+JSON.stringify(data));
                this.initializeDebug("OnMessageRecived");

              
                let decryptedData = await Promise.resolve(this.decryptionService.decryptCipherData(data.data, data.ets));
                this.decryptedData = decryptedData;

                this.ngxLogger.info('Decrypted Alert ' + this.decryptedData);
      
            }, errRes => {
                console.log(' Error in receiveing message from Firebasex ' + JSON.stringify(errRes));
            });

   async decryptCipherData(data, ets) {


            var secretKey  = localStorage.getItem('secretKey1');
            var iv = localStorage.getItem('ivParameterSpec1');
           
            return this.decrypt(data, secretKey, iv);

       
    }

    async decrypt(data, secretKey, iv) {
        try {
            // Convert iv and ciphertext to Uint8Array
            var ivUint8Array = this.b64ToUint8Array(iv);
            var ciphertextUint8Array = this.b64ToUint8Array(data);

            // Import the CryptoKey
            this.ngxLogger.info('Importing the key!');
            console.log('Importing the key!')
            var key = await Promise.resolve(this.importKey(secretKey)).catch(e);

            // Decrypt using crypto.subtle.decrypt
            var decrypted = await crypto.subtle.decrypt(
                { name: 'AES-GCM', iv: ivUint8Array },
                key,
                ciphertextUint8Array
              );
          

            // Decode the decrypted data
            this.ngxLogger.info('Decode the decrypted data!');
            console.log('Decode the decrypted data!')
            var dec = new TextDecoder();
            var decryptedText = dec.decode(new Uint8Array(decrypted));

            console.log('Decrypted Text:', decryptedText);
            console.log('Decrypted Text:', JSON.stringify(decryptedText));
            return decryptedText;
        } catch (error) {
            console.error('Decryption failed:', error);
            console.log(error)
            return null;
        }
    }

    async importKey(secretKey) {
        try {
            this.ngxLogger.info('Parse the Base64-encoded key');
            console.log('Parse the Base64-encoded key')
            var keyBuffer = this.b64ToUint8Array(secretKey);

            // Import the key using crypto.subtle.importKey
            this.ngxLogger.info(' Import the key using crypto.subtle.importKey');
            console.log(' Import the key using crypto.subtle.importKey')
            var key = await crypto.subtle.importKey(
                "raw",
                keyBuffer,
                { name: "AES-GCM" },
                true,
                ["decrypt", "encrypt"]
              );

            this.ngxLogger.info('Returning the key');
            console.log('Returning the key')
            return key;
        } catch (ex) {
            this.ngxLogger.info('Error');
            console.log('Error')
            this.ngxLogger.info('ImportKey error:', ex.name, ', Message:', ex.message);
            console.error('ImportKey error:', ex.name, ', Message:', ex.message);
            console.log(ex)
            return null;
        }
    }

    // Helper function
    b64ToUint8Array(base64string) {
        return Uint8Array.from(atob(base64string), c => c.charCodeAt(0));
    }

当应用程序移至后台时,解密代码无法执行,导致解密无法按预期进行。

javascript ionic-framework webcrypto-api ionic6
1个回答
0
投票

我也面临着同样的问题。您找到解决方案了吗?

© www.soinside.com 2019 - 2024. All rights reserved.