How to encrypt a file in Mendix

0
i tried to encrypt a file using java action but encountered with some errors I’m pasting the code here please check the code, suggest if any extra libraries are needed to import. if there are any other methods to encrypt a file please suggest. Suggessions are very much appreciated Thanks in advance.   // This file was generated by Mendix Studio Pro. // // WARNING: Only the following code will be retained when actions are regenerated: // - the import list // - the code between BEGIN USER CODE and END USER CODE // - the code between BEGIN EXTRA CODE and END EXTRA CODE // Other code you write will be lost the next time you deploy the project. // Special characters, e.g., é, ö, à, etc. are supported in comments. package myfirstmodule.actions; import java.io.File; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.encryption.AccessPermission; import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.SecretKeySpec; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.security.AlgorithmParameters; import java.security.GeneralSecurityException; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import java.util.Base64; import com.mendix.systemwideinterfaces.core.IContext; import com.mendix.webui.CustomJavaAction; import com.mendix.systemwideinterfaces.core.IMendixObject; public class securePDF extends CustomJavaAction<java.lang.Boolean> {     private IMendixObject __filedocument;     private system.proxies.FileDocument filedocument;     public securePDF(IContext context, IMendixObject filedocument)     {         super(context);         this.__filedocument = filedocument;     }     @java.lang.Override     public java.lang.Boolean executeAction() throws Exception     {         this.filedocument = __filedocument == null ? null : system.proxies.FileDocument.initialize(getContext(), __filedocument);         // BEGIN USER CODE                  IContext context = getContext();         ILogNode logger = core.getLogger("securePDF");          logger.trace("Retrieve generated document");         PDDocument inputDoc = PDDocument.load(core.getFileDocumentContent(context, filedocument.getMendixObject()));                  // Define the length of the encryption key.         // Possible values are 40 or 128 (256 will be available in PDFBox 2.0).         int keyLength = 32;         AccessPermission ap = new AccessPermission();         // Disable printing, everything else is allowed         ap.setCanPrint(false);         ap.setCanAssembleDocument(false);         ap.setCanExtractContent(false);         ap.setCanExtractForAccessibility(false);         ap.setCanModify(false);         // Owner password (to open the file with all permissions) is "12345"         // User password (to open the file but with restricted permissions, is empty here)          StandardProtectionPolicy spp = new StandardProtectionPolicy("12345", "12345", ap);         spp.setEncryptionKeyLength(keyLength);         spp.setPermissions(ap);         inputDoc.protect(spp);         ByteArrayOutputStream output = new ByteArrayOutputStream();         inputDoc.save(output);         InputStream newContent = new ByteArrayInputStream(output.toByteArray());                 logger.trace("Store result in original document");         Core.storeFileDocumentContent(context, filedocument.getMendixObject(), newContent);                           logger.trace("Close PDFs");         inputDoc.close();                  logger.trace("Secure PDF done");         return true;                  // END USER CODE     }     /**      * Returns a string representation of this action      */     @java.lang.Override     public java.lang.String toString()     {         return "securePDF";     }     // BEGIN EXTRA CODE     // END EXTRA CODE }  
asked
1 answers
0

The annotation states that possible key lengths are 40 or 128, but you set it to 32. Please check with 40 or 128.

// Define the length of the encryption key.
// Possible values are 40 or 128 (256 will be available in PDFBox 2.0).
int keyLength = 32;

 

answered