Wednesday, April 06, 2011

Encrypt and decrypt files in your CMS

Every so often I read this question in forums: Does this repository API (for example a the Jackrabbit JCR implementation) support document encryption? And then the answer: This is not a concern of the repository but of the client.

The question though is if this is really so difficult task and actually at least in Java thanks to the Jasypt library the answer is: It is not difficult at all.

Below is a fully working program that will allow to encrypt a document and store it in a given path (or in the same folder path with different name by default). Few lines of code from Jasypt library are actually enough to accomplish this goal.

Note that this code uses the BasicBinaryEncryptor but you can also use the StrongBinaryEncryptor which is not any different.

package com.nestorurquiza.pdf.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.jasypt.util.binary.BasicBinaryEncryptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CryptFile {
    
    private static final Logger log = LoggerFactory.getLogger(CryptFile.class.getName());
    
    private CryptFile(){};
    
    public static void main( String[] args ) throws Exception
    {
        if(args.length < 3) {
            usage();
        }
        String action = args[0];
        boolean encrypt = action.equalsIgnoreCase("encrypt");
        boolean decrypt = action.equals("decrypt");
        
        if( !encrypt && !decrypt  ) {
            usage();
        }
        
        String password = args[1];
        String inputFilePath = args[2];
        String ext = inputFilePath.substring( inputFilePath.length() - 4 );
        String outputFilePath = inputFilePath.substring( 0, inputFilePath.length() -4 ) + "_" + action + ext;
        if(args.length >3) {
            outputFilePath = args[3];
        }
        
        File inputFile = new File(inputFilePath);

        byte[] bytes = new byte[(int) inputFile.length()];
        try {
            FileInputStream fileInputStream = new FileInputStream(inputFile);
            fileInputStream.read(bytes);
            BasicBinaryEncryptor binaryEncryptor = new BasicBinaryEncryptor();
            binaryEncryptor.setPassword(password);
            File outputFile = new File(outputFilePath);
            FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
            if(encrypt) {
                fileOutputStream.write(binaryEncryptor.encrypt(bytes));
            } else {
                fileOutputStream.write(binaryEncryptor.decrypt(bytes));
            }
            fileOutputStream.close();
            fileInputStream.close();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        

    }
    

    private static void usage()
    {
        System.err.println( "Usage: com.nestorurquiza.pdf.tools.EncryptFile <action(encrypt/decrypt)> <password> <input file> [output file]\n");
        System.exit( 1 );
    }
}

No comments:

Followers