Secure Word Document Example
This program demonstrates securing a Word file with encryption.
Problem Statement
Write a Java program that encrypts a Word .docx file with a password using Apache POI.
Source Code
| 1 | import java.io.*; |
| 2 | import org.apache.poi.poifs.crypt.*; |
| 3 | import org.apache.poi.poifs.filesystem.*; |
| 4 | import org.apache.poi.xwpf.usermodel.*; |
| 5 | public class SecureWordExample { |
| 6 | public static void main(String[] args) { |
| 7 | System.out.println("Eduinq Secure Word Example"); |
| 8 | try(XWPFDocument doc=new XWPFDocument()) { |
| 9 | doc.createParagraph().createRun().setText("Eduinq Secure Word Document"); |
| 10 | POIFSFileSystem fs=new POIFSFileSystem(); |
| 11 | EncryptionInfo info=new EncryptionInfo(EncryptionMode.standard); |
| 12 | Encryptor enc=info.getEncryptor(); |
| 13 | enc.confirmPassword("eduinq123"); |
| 14 | try(OutputStream os=enc.getDataStream(fs)) { |
| 15 | doc.write(os); |
| 16 | } |
| 17 | try(FileOutputStream fos=new FileOutputStream("secure.docx")) { |
| 18 | fs.writeFilesystem(fos); |
| 19 | System.out.println("Eduinq Word Secured Successfully"); |
| 20 | } |
| 21 | } catch(Exception e) { |
| 22 | System.out.println("Eduinq Exception: "+e.getMessage()); |
| 23 | } |
| 24 | } |
| 25 | } |
Program Output
Eduinq Secure Word Example Eduinq Word Secured Successfully
Explanation
Encryption uses Encryptor with password protection. - Secures .docx file against unauthorized access. - Exception handling ensures safe operations. - In enterprise systems, Word security is used for confidential documents. Conclusion: Shows securing Word files with encryption.