Esempio n. 1
0
  private void encrypt(String[] args) throws Exception {
    if (args.length < 1) {
      usage();
    } else {
      AccessPermission ap = new AccessPermission();

      String infile = null;
      String outfile = null;
      String certFile = null;
      String userPassword = "";
      String ownerPassword = "";

      int keyLength = 40;

      PDDocument document = null;

      try {
        for (int i = 0; i < args.length; i++) {
          String key = args[i];
          if (key.equals("-O")) {
            ownerPassword = args[++i];
          } else if (key.equals("-U")) {
            userPassword = args[++i];
          } else if (key.equals("-canAssemble")) {
            ap.setCanAssembleDocument(args[++i].equalsIgnoreCase("true"));
          } else if (key.equals("-canExtractContent")) {
            ap.setCanExtractContent(args[++i].equalsIgnoreCase("true"));
          } else if (key.equals("-canExtractForAccessibility")) {
            ap.setCanExtractForAccessibility(args[++i].equalsIgnoreCase("true"));
          } else if (key.equals("-canFillInForm")) {
            ap.setCanFillInForm(args[++i].equalsIgnoreCase("true"));
          } else if (key.equals("-canModify")) {
            ap.setCanModify(args[++i].equalsIgnoreCase("true"));
          } else if (key.equals("-canModifyAnnotations")) {
            ap.setCanModifyAnnotations(args[++i].equalsIgnoreCase("true"));
          } else if (key.equals("-canPrint")) {
            ap.setCanPrint(args[++i].equalsIgnoreCase("true"));
          } else if (key.equals("-canPrintDegraded")) {
            ap.setCanPrintDegraded(args[++i].equalsIgnoreCase("true"));
          } else if (key.equals("-certFile")) {
            certFile = args[++i];
          } else if (key.equals("-keyLength")) {
            try {
              keyLength = Integer.parseInt(args[++i]);
            } catch (NumberFormatException e) {
              throw new NumberFormatException(
                  "Error: -keyLength is not an integer '" + args[i] + "'");
            }
          } else if (infile == null) {
            infile = key;
          } else if (outfile == null) {
            outfile = key;
          } else {
            usage();
          }
        }
        if (infile == null) {
          usage();
        }
        if (outfile == null) {
          outfile = infile;
        }
        document = PDDocument.load(infile);

        if (!document.isEncrypted()) {
          if (certFile != null) {
            PublicKeyProtectionPolicy ppp = new PublicKeyProtectionPolicy();
            PublicKeyRecipient recip = new PublicKeyRecipient();
            recip.setPermission(ap);

            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            InputStream inStream = new FileInputStream(certFile);
            X509Certificate certificate = (X509Certificate) cf.generateCertificate(inStream);
            inStream.close();

            recip.setX509(certificate);

            ppp.addRecipient(recip);

            ppp.setEncryptionKeyLength(keyLength);

            document.protect(ppp);
          } else {
            StandardProtectionPolicy spp =
                new StandardProtectionPolicy(ownerPassword, userPassword, ap);
            spp.setEncryptionKeyLength(keyLength);
            document.protect(spp);
          }
          document.save(outfile);
        } else {
          System.err.println("Error: Document is already encrypted.");
        }
      } finally {
        if (document != null) {
          document.close();
        }
      }
    }
  }