Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can we open password protected excel file with npoi? #170

Closed
karanjshah opened this issue Jan 11, 2019 · 24 comments
Closed

How can we open password protected excel file with npoi? #170

karanjshah opened this issue Jan 11, 2019 · 24 comments

Comments

@karanjshah
Copy link

If anyone can guide me that how can we access password protected file with NPOI in c# then it would really very grateful for me.

Thank You

@Bykiev
Copy link
Collaborator

Bykiev commented Jan 12, 2019

@karanjshah, hi

you can use this code from the [NPOI.TestCases] project (https://github.com/tonyqus/npoi/blob/master/testcases/main/POIFS/Crypt/TestDecryptor.cs):

POIFSFileSystem fs = new POIFSFileSystem(new FileStream("protect.xlsx", FileMode.Open));
EncryptionInfo info = new EncryptionInfo(fs);
Decryptor d = Decryptor.GetInstance(info);
d.VerifyPassword("password");

But this code is not working, because NPOI.POIFS.Crypt namespace is missing (Crypt folder is excluded from the project) for some reason...

@tonyqus, can you confirm it was excluded by mistake?

@tonyqus tonyqus self-assigned this Feb 8, 2019
@afrikaan-official
Copy link

any update on this ?

@avoll
Copy link

avoll commented May 23, 2019

Would love to hear an update!

@jlgordo
Copy link

jlgordo commented Aug 2, 2019

Hello @tonyqus , do you have an estimated date to have this issue solved?

@niikoo
Copy link

niikoo commented May 26, 2020

+1

@daveechols
Copy link

daveechols commented May 27, 2020

I would like to see an update/timeline on this also. I am trying to open an Excel 2016 password protected file but I get errors.

this is the error I get....

System.IO.IOException
HResult=0x80131620
Message=Not found type NPOI.POIFS.Crypt.Agile.AgileEncryptionInfoBuilder
Source=NPOI
StackTrace:
at NPOI.POIFS.Crypt.EncryptionInfo..ctor(ILittleEndianInput dis, Boolean isCryptoAPI)
at NPOI.POIFS.Crypt.EncryptionInfo..ctor(POIFSFileSystem fs)
at Test_NPOI_Password.Program.Main(String[] args) in C:_Projects\Research\General\Test_NPOI_Password\Program.cs:line 20

This exception was originally thrown at this call stack:
[External Code]

I

This is the code I used...

  POIFSFileSystem fs = new POIFSFileSystem(new FileStream("Book1.xlsx", FileMode.Open));
  EncryptionInfo info = new EncryptionInfo(fs);
  Decryptor d = Decryptor.GetInstance(info);
  d.VerifyPassword("admin123");

Book1.xlsx

@rajnishdubey3
Copy link

Hello,

Is there any fix for this. I would like to open and read a Password protected Excel file on the server-side.

I used the below code.

`

Public Sub open_2()

    Dim fp As String = "C:\Desktop\Test\Filewithpassword\File with pass_2.xlsx"
    Try
        Dim nfs As NPOI.POIFS.FileSystem.POIFSFileSystem = New NPOI.POIFS.FileSystem.POIFSFileSystem(New FileStream(fp, FileMode.Open, FileAccess.ReadWrite))
        Dim password As String = "12345"
        Dim info As EncryptionInfo = New EncryptionInfo(nfs)
        Dim dc As Decryptor = Decryptor.GetInstance(info)
        Dim b As Boolean = dc.VerifyPassword(password)

        If b Then
            Console.WriteLine("Authenticated.")
        Else
            Console.WriteLine("Wrong password.")
        End If

        Dim excelData As Stream = dc.GetDataStream(nfs)

    Catch ex As Exception

    End Try
End Sub

`

@cbloechle
Copy link

+1 Would like to have this feature in our toolbelt.

@tonyqus tonyqus added this to the NPOI vNext milestone Jun 26, 2021
@tonyqus
Copy link
Member

tonyqus commented Jun 26, 2021

@Bykiev NPOI.POIFS.Crypt is included but I notice that there are a few methods in this namespace, which are not implemented

@AkanshaKumari
Copy link

is there any update on this because I have been facing this issue since very long and I need a fix urgently. Else i will have to use other library.

@tonyqus

This comment was marked as off-topic.

@dp250195
Copy link

dp250195 commented Jun 18, 2022

I noticed that, some files, even if they are password-protected, can still be open with Excel without an issue. These files have only their structure protected, but contents can still be read.

image

Is it possible to open such files in read-only mode, without specifying a password and without triggering an exception?

@tim-rue
Copy link

tim-rue commented Jul 27, 2022

I found this PR just after creating a workaround myself. I tested it and I can confirm #883 works like a charm!

@tonyqus
Copy link
Member

tonyqus commented Jul 31, 2022

I'm testing code of #883. Looks it's not fully working. @tim-ruecker can you give me a sample code that is working on your side?

Exception I got:
NPOI.EncryptedDocumentException:“Export Restrictions in place - please install JCE Unlimited Strength Jurisdiction Policy files”

@tim-rue
Copy link

tim-rue commented Jul 31, 2022

@tonyqus This exception is commented out in #883:

/*if (Cipher.GetMaxAllowedKeyLength(cipherAlgorithm.jceId) < keySizeInBytes * 8) {
throw new EncryptedDocumentException("Export Restrictions in place - please install JCE Unlimited Strength Jurisdiction Policy files");
}*/

Are you sure you checked out the right branch?

This is how I open the workbook:

POIFSFileSystem fs = new POIFSFileSystem(new FileStream(@"path\to\excel.xlsm", FileMode.Open));
EncryptionInfo info = new EncryptionInfo(fs);
Decryptor d = Decryptor.GetInstance(info);

var validPassword = d.VerifyPassword("password");
if(!validPassword)
   return; //todo throw exception

var st = d.GetDataStream(fs);
var xssWorkbook = new XSSFWorkbook(st);

@tonyqus
Copy link
Member

tonyqus commented Jul 31, 2022

My bad. I used the wrong branch. Looks it works very well.

@tim-rue
Copy link

tim-rue commented Aug 1, 2022

@tonyqus Great. Any chance we can have this added to 2.6.0?

@tonyqus tonyqus closed this as completed in 66a8e15 Aug 1, 2022
@dontbyte
Copy link

dontbyte commented Aug 2, 2022

Great, huge big thanks to all of you! @KnyazSh @tonyqus @tim-ruecker

@tonyqus
Copy link
Member

tonyqus commented Aug 3, 2022

@tim-ruecker #883 will be shipped with 2.6.0. I've merged the PR.

@JulienLeal
Copy link

JulienLeal commented Dec 10, 2022

How can I encrypt the xlsx? With this way I only can decrypt the file, right? @tonyqus

@tonyqus
Copy link
Member

tonyqus commented Dec 12, 2022

@JulienLeal It's not supported.

@tonyqus tonyqus modified the milestones: NPOI 2.6.1, NPOI 2.6.0 Mar 19, 2023
@egtalbot
Copy link

egtalbot commented Apr 5, 2023

I have 2.6.0 and trying to open a sheet where the tabs are password protected against changes and this line gives me an error:
POIFSFileSystem fs = new POIFSFileSystem(new FileStream(stream))

Note, I am inside a using statement after opening a stream when I execute that line. Error indicates the supplied data is Office 2007+ XML. It's an xlsx file just like in the example from @tim-rue so not sure what's going wrong.

@tonyqus
Copy link
Member

tonyqus commented Apr 5, 2023

@egtalbot POIFSFileSystem is for xls only.

Please try the following code:
IWorkbook workbook = WorkbookFactory.Create("encryped.xlsx", "password");

@egtalbot
Copy link

egtalbot commented Apr 6, 2023

@egtalbot POIFSFileSystem is for xls only.

Please try the following code: IWorkbook workbook = WorkbookFactory.Create("encryped.xlsx", "password");

Thanks - I did try that and it failed to create. I tried so many things I can't even remember what the error was, but I'm moving back to ExcelDataReader.

I do appreciate the response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests