Lazarus

Programming => Packages and Libraries => FPSpreadsheet => Topic started by: bonmario on January 17, 2022, 04:43:32 pm

Title: Read ods with password?
Post by: bonmario on January 17, 2022, 04:43:32 pm
Hi,
with this code, i can read a file ods without password:
Code: Pascal  [Select][+][-]
  1.   MyWorkbook:=TsWorkbook.Create;
  2.   try
  3.     MyWorkbook.ReadFromFile('PwdNo.ods', sfOpenDocument);
  4.  
  5.     MyWorksheet:=MyWorkbook.GetFirstWorksheet;
  6.     Memo1.Lines.Add('File without password: Col = ' +
  7.                     IntToStr(MyWorksheet.GetLastColIndex(False)) +
  8.                     ' - Row = ' +
  9.                     IntToStr(MyWorksheet.GetLastRowIndex(False)));
  10.   finally
  11.     FreeAndNil(MyWorkbook);
  12.   end;
  13.  

Now, the same file have been protected with password.
I've tried with the code avove, but it fails:
Code: Pascal  [Select][+][-]
  1.   MyWorkbook:=TsWorkbook.Create;
  2.   try
  3.     MyWorkbook.ReadFromFile('PwdYes_123456.ods', '123456'{, sfOpenDocument});
  4.  
  5.     MyWorksheet:=MyWorkbook.GetFirstWorksheet;
  6.     Memo1.Lines.Add('File with password: Col = ' +
  7.                     IntToStr(MyWorksheet.GetLastColIndex(False)) +
  8.                     ' - Row = ' +
  9.                     IntToStr(MyWorksheet.GetLastRowIndex(False)));
  10.   finally
  11.     FreeAndNil(MyWorkbook);
  12.   end;
  13.  

Someone can help me?

Thanks, Mario
Title: Re: Read ods with password?
Post by: wp on January 17, 2022, 05:47:45 pm
fpspreadsheet does not support encrypted files.
Title: Re: Read ods with password?
Post by: rvk on January 17, 2022, 06:05:59 pm
You would need to unzip the content of the ods yourself (not protected) and look in the META-INF/manifest.xml. It will show how the files are encrypted (probably aes256-cbc). The initialisation-vector (IV) is also in there. With that you could try to decrypt all the files mentioned in that manifest.xml  (a recent post here (https://forum.lazarus.freepascal.org/index.php/topic,57776.msg429973.html#msg429973) shows some code to decrypt AES-256-CBC with TDCP_rijndael). After that you can rezip the contents back to an unprotected ods-file and read it with fpspreadsheet. (Don't forget to remove the encryption-data parts in the manifest.xml)

Maybe you also need to do something (PBKDF2) with the password. See https://askubuntu.com/a/223183/926892 for more details.
Title: Re: Read ods with password?
Post by: bonmario on January 18, 2022, 08:36:51 am
Ok, thanks
TinyPortal © 2005-2018