Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created February 2, 2021 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/5bccba043cd3a3f2f27b92453a3e37d5 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/5bccba043cd3a3f2f27b92453a3e37d5 to your computer and use it in GitHub Desktop.
Encrypt Decrypt Excel Files using Java
// Initialize loading options
LoadOptions loadOptions = new LoadOptions(LoadFormat.XLSX);
// Set original password
loadOptions.setPassword("1234");
// Instantiate a Workbook object with Excel file's path
Workbook workbook = new Workbook("encrypted-workbook.xlsx", loadOptions);
// Set password to null
workbook.getSettings().setPassword(null);
// Save the decrypted Excel file
workbook.save("decrypted-workbook.xlsx");
// Instantiate a Workbook object by excel file path
Workbook workbook = new Workbook("workbook.xlsx");
// Password protect the file
workbook.getSettings().setPassword("1234");
// Encrypt by specifying the encryption type
workbook.setEncryptionOptions(EncryptionType.XOR, 40);
// Specify Strong Encryption type (RC4,Microsoft Strong Cryptographic Provider)
workbook.setEncryptionOptions(EncryptionType.STRONG_CRYPTOGRAPHIC_PROVIDER, 128);
// Save the encrypted Excel file
workbook.save("encrypted-workbook.xlsx");
// Create a Stream object
FileInputStream fstream = new FileInputStream("encrypted-workbook.xlsx");
// Verify password
boolean isPasswordValid = FileFormatUtil.verifyPassword(fstream, "1234");
// Print results
System.out.println("Password is Valid: " + isPasswordValid);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment