Page 1 of 1

How to password protect a docx document?

PostPosted: Wed May 27, 2015 8:38 pm
by andreas
Hello,
A simple question regarding password protection of docx document. The protection and the document is locked perfectly but we cant seem to get the password to unlock the document again. :oops:

Here is what our final settings.xml is looking like.

Code: Select all
<w:trackRevisions/><w:documentProtection w:salt="765hSrDKb6lC49/43ZYQIw==" w:hash="GQX3oKKv71QhkN5AAR2evA==" w:cryptSpinCount="100000" w:cryptAlgorithmSid="14" w:cryptAlgorithmType="typeAny" w:cryptAlgorithmClass="hash" w:cryptProviderType="rsaAES" w:enforcement="true" w:edit="readOnly"/>


and the docx4j java code creating the protection element
Code: Select all
  // (1.1) lock the entire document and enable tracked changes (revisions) via docx settings.xml
    // Create settings part, and init content
    DocumentSettingsPart dsp;
    try {
      dsp = new DocumentSettingsPart();

      CTSettings settings = factory.createCTSettings();
      settings.setTrackRevisions(new BooleanDefaultTrue());

      CTDocProtect dp = factory.createCTDocProtect();
      dp.setEdit(STDocProtect.READ_ONLY);
      dp.setEnforcement(true);
     
      dp.setCryptProviderType(STCryptProv.RSA_AES);
      dp.setCryptAlgorithmClass(STAlgClass.HASH);
      dp.setCryptAlgorithmType(STAlgType.TYPE_ANY);
      dp.setCryptAlgorithmSid(BigInteger.valueOf(14));
      dp.setCryptSpinCount(BigInteger.valueOf(100000));
     
      String hash = "hH19JqFv5Z+txtWMTCKmi1yinjNnO-eiKvnPg19V5rvpjcHK9Lv3JCyCGmZtnrXj5jVVTVyHRSNR5OBTX8egNng=="; //password=xyz
      String salt = "qva6VWB5NDLWxfepI9WU4Q==";
     
      try {
        byte[] bytesOfMessage = hash.getBytes("UTF-8");
 
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] thedigesthash = md.digest(bytesOfMessage);
 
        byte[] bytesOfMessagesalt = salt.getBytes("UTF-8");
 
        MessageDigest mdsalt;
       
        mdsalt = MessageDigest.getInstance("MD5");
        byte[] thedigestsalt = mdsalt.digest(bytesOfMessagesalt);
       
        dp.setHash(thedigesthash);
        dp.setSalt(thedigestsalt);
       
      } catch (NoSuchAlgorithmException e) {
        LOG.error("Could not password protect document - NoSuchAlgorithmException" + e + " -- " );
        e.printStackTrace();
      } catch (UnsupportedEncodingException e) {
        LOG.error("Could not password protect document - UnsupportedEncodingException" + e + " -- " );
        e.printStackTrace();
      }

      dsp.setJaxbElement(settings);
      dsp.getContents().setDocumentProtection(dp);
      mdp.addTargetPart(dsp);
    } catch (InvalidFormatException e1) {
      LOG.error(" could not generate document settings, InvalidFormatException something is very wrong.");
      e1.printStackTrace();
    } catch (Docx4JException e1) {
      LOG.error(" could not generate document settings, something is wrong.");
      e1.printStackTrace();
    }

Something is not right...
Any ideas on how to set the password in plain text?

Looking for a method like this ;)
Code: Select all
dp.setPlainTextPassword("xyz");

Re: How to password protect a docx document?

PostPosted: Wed Jul 15, 2015 2:29 pm
by jason
Sorry for the delay in responding.

Courtesy of some code from Apache, this works in forthcoming docx4j 3.3.0; see https://github.com/plutext/docx4j/tree/VERSION_3_3_0

Specifically https://github.com/plutext/docx4j/blob/ ... ected.java
and https://github.com/plutext/docx4j/blob/ ... sPart.java

So you could checkout that branch, or if you wanted, copy the new DocumentSettingsPart.java and the code in package https://github.com/plutext/docx4j/tree/ ... apache/poi into your code base.