Created
February 2, 2021 17:30
-
-
Save aspose-com-gists/5bccba043cd3a3f2f27b92453a3e37d5 to your computer and use it in GitHub Desktop.
Encrypt Decrypt Excel Files using Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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