Advertisement
kiwiwings

POI Docx De-/Encryption

Aug 1st, 2014
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.10 KB | None | 0 0
  1. package org.apache.poi.xslf.usermodel;
  2.  
  3. import static org.junit.Assert.assertTrue;
  4.  
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.util.Arrays;
  10.  
  11. import org.apache.poi.POIDataSamples;
  12. import org.apache.poi.poifs.common.POIFSConstants;
  13. import org.apache.poi.poifs.crypt.Decryptor;
  14. import org.apache.poi.poifs.crypt.EncryptionInfo;
  15. import org.apache.poi.poifs.crypt.EncryptionMode;
  16. import org.apache.poi.poifs.crypt.Encryptor;
  17. import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
  18. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  19. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  20. import org.junit.Test;
  21.  
  22. public class TestMisc {
  23.     @Test
  24.     public void protectedRewrite() throws Exception {
  25.         File docs[] = {
  26.             POIDataSamples.getDocumentInstance().getFile("52288.docx"),
  27.             POIDataSamples.getPOIFSInstance().getFile("protected_agile.docx"),
  28.         };
  29.        
  30.         for (File doc : docs) {
  31.             if (isEncrypted(doc)) {
  32.                 NPOIFSFileSystem fs = new NPOIFSFileSystem(doc, true);
  33.                
  34.                 // decrypt the protected file - in this case it was encrypted with the default password
  35.                 EncryptionInfo encInfo = new EncryptionInfo(fs);
  36.                 Decryptor d = encInfo.getDecryptor();
  37.                 boolean b = d.verifyPassword(Decryptor.DEFAULT_PASSWORD);
  38.                 assertTrue(b);
  39.                
  40.                 // do some strange things with it
  41.                 XWPFDocument docx = new XWPFDocument(d.getDataStream(fs));
  42.                 docx.getParagraphArray(0).insertNewRun(0).setText("Ajeet was here! All your base are belong to us!");
  43.                 docx.getParagraphArray(0).insertNewRun(1).addBreak();
  44.                 // in-place write of the original npoifs doesn't work, so using a new POIFS for now
  45.                 fs.close();
  46.                
  47.                 // and encrypt it again
  48.                 POIFSFileSystem fs2 = new POIFSFileSystem();
  49.                 encInfo = new EncryptionInfo(fs2, EncryptionMode.agile);
  50.                 Encryptor e = encInfo.getEncryptor();
  51.                 e.confirmPassword("AYBABTU");
  52.                 docx.write(e.getDataStream(fs2));
  53.                 FileOutputStream fos = new FileOutputStream(new File(doc.getName()));
  54.                 fs2.writeFilesystem(fos);
  55.                 fos.close();
  56.             } else {
  57.                 System.out.println(doc.getName()+" is without encryption");
  58.             }
  59.         }
  60.     }
  61.    
  62.     public static boolean isEncrypted(File file) throws IOException {
  63.         FileInputStream fis = new FileInputStream(file);
  64.         byte buf[] = new byte[4];
  65.         fis.read(buf);
  66.         fis.close();
  67.        
  68.         if (Arrays.equals(buf, POIFSConstants.OOXML_FILE_HEADER)) {
  69.             // plain ooxml
  70.             return false;
  71.         }
  72.        
  73.         NPOIFSFileSystem poifs = new NPOIFSFileSystem(file);
  74.         boolean hasEncInf = poifs.getRoot().hasEntry("EncryptionInfo");
  75.         poifs.close();
  76.        
  77.         return hasEncInf;
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement