Tuesday 20 January 2015

Crack pdf password using Brute Force technique


Hello every one, unfortunately I forget password for one of my pdf document so I want to get the password somehow. I used simple Brute-Force technique to get the password.

To explain, I am considering below assumptions.
1. Password is of length 8 (Since 8 is my lucky numberJ).
2. Password contains only numbers

This program took approximately 32 minutes to retrieve password. One thing I would like to tell you, below program used simple Brute-Force technique, so it may take days to break password for your pdf document (which contains alpha numeric characters and special symbols, Upper case and lowers case letters).

import org.apache.pdfbox.pdmodel.PDDocument;
import org.paukov.combinatorics.Factory;
import org.paukov.combinatorics.Generator;
import org.paukov.combinatorics.ICombinatoricsVector;
import java.util.*;

public class PDFUnlock {

    static int passwordLength = 8;

    public static String getPassword(PDDocument document){
        // Create the initial vector of  all digits
        ICombinatoricsVector<Integer> originalVector = Factory.createVector(new Integer[]{0,1,2,3,4,5,6,7,8,9});

        // Create the permutation generator by calling the appropriate method in the Factory class
        Generator<Integer> gen = Factory.createPermutationWithRepetitionGenerator(originalVector, 8);

        System.out.println("Total permutations : " + gen.getNumberOfGeneratedObjects());

        // Try for each permutation
        for (ICombinatoricsVector<Integer> perm : gen) {
            List<Integer> list = perm.getVector();
            StringBuilder s = new StringBuilder("");
            for(int i : list){
                s.append(i);
            }
            boolean flag;

            try{
               flag = true;
               document.decrypt(s.toString());
            }
            catch(Exception e){
                flag = false;
            }

            if(flag == true){
                return s.toString();
            }
        }

        return null;
    }
    
    public static void main(String args[]) throws Exception {
        PDDocument document = PDDocument.load("E:\\hari\\im_ptr.pdf");

        long time1 = System.currentTimeMillis();
        System.out.println(getPassword(document));
        long time2 = System.currentTimeMillis();

        System.out.println("Total time taken is " + (time2-time1) + " Milliseconds");
    }
}

I used CombinatoricsLib and Apache PDFBox libraries.

No comments:

Post a Comment