zurmokeeper / officecrypto-tool

officecrypto-tool is a library for js that can be used to decrypt and encrypt office(excel/ppt/word) files.
https://www.npmjs.com/package/officecrypto-tool
MIT License
19 stars 4 forks source link

Cannot determine whether the file is password protected #28

Closed manik-ushur closed 4 months ago

manik-ushur commented 4 months ago

I have created a Password protected .xls (Not .xlsx) file using MS Excel on Macbook and trying to determine using officecrypto-tool whether the file is password protected. It is not able to determine correctly. I have used the following code:

const arrayBuffer = this.result;
                            const uint8Array = new Uint8Array(arrayBuffer);

                            try {
                                // Check if the file is encrypted
                                const encrypted = isEncrypted(uint8Array);
                                file.isProtectedFile = encrypted;
                            } catch (error) {
                                console.error('Error reading the file:', error);
                                file.isProtectedFile = false;
                            }

This is the file which I tried sample3.xls

zurmokeeper commented 4 months ago

@manik-ushur Can you provide your version of MS Excel?

manik-ushur commented 4 months ago

@manik-ushur Can you provide your version of MS Excel?

Thanks @zurmokeeper for your response. Here you go: Version 16.87 (24071426)

image
zurmokeeper commented 4 months ago

@manik-ushur

Thanks, I've found the problem.

Can you provide me with the password for sample.xls? Or can you create a new encrypted xls file with random contents for me? I don't have such a file.

I'd like to test if you can decrypt this file properly, as this also affects the decryption method.

manik-ushur commented 4 months ago

Password$456

zurmokeeper commented 4 months ago

@manik-ushur v0.0.14 has been released, just upgrade and install the latest version!

krishna-ushur commented 4 months ago

@zurmokeeper Thanks for the quick fix. 'isEncrypted' method works now, but there's an issue with the 'decrypt' method as well. It resolves as true even with the wrong password, and not entering the catch block. Below is the code:

              const uint8Array = new Uint8Array(arrayBuffer);
              try {
                    await officeCrypto.decrypt(uint8Array, { password: password });
                    resolve(true);
              } catch (error) {
                  if (error.message.includes('Password')) {
                      resolve(false);  // Password is incorrect
                  }
              }
zurmokeeper commented 4 months ago

@krishna-ushur Yes, there is indeed a problem here, and I've found it, but it's going to take some time to fix, so I went ahead and posted a new version dealing with the isEncrypted method.

manik-ushur commented 4 months ago

thanks @zurmokeeper We would want the decrypt feature as well. Shall we create another ticket?

zurmokeeper commented 4 months ago

@manik-ushur Yes, you can.

zurmokeeper commented 4 months ago

@manik-ushur @krishna-ushur v0.0.15 has been released to support BIFF5 decryption, just upgrade and install the latest version! Try this version.

manik-ushur commented 4 months ago

Thanks a lot @zurmokeeper. We will give it a try and update you.