Tuesday, 1 September 2015

JD: Java Decompiler

A decompiler is a program that performs the reverse operation to that of a compiler. Decompiers are not perfectly reconstruct the source code, but still they plays vital role in software reverse engineering. In this post, we are going to discuss about JD (Java Decompiler).

“Java Decompiler project” provide tools to decompile and analyze java source code. “Java decompiler project” provides core libraries, standalone User interface, plugins to IDES like Eclipse, Intellij. In this post we are going to see, how to use JD to decompile .class files.

Download Java decompiler GUI setup file from following location.

After downloading setup file, install and launch JD project.


Employee.java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Employee implements Serializable {
 int id;
 String firstName, lastName;
 byte b[] = { 1, 2, 3, };

 Employee(int id, String firstName, String lastName) {
  this.id = id;
  this.firstName = firstName;
  this.lastName = lastName;
 }

 public static void main(String args[]) throws Exception {

  /* Serialize object */
  FileOutputStream fos = new FileOutputStream("ser.out");
  ObjectOutputStream out = new ObjectOutputStream(fos);
  Employee emp = new Employee(1, "Krishna", "Arjun");
  out.writeObject(emp);

  /* Deserialize object */
  FileInputStream fis = new FileInputStream("ser.out");
  ObjectInputStream in = new ObjectInputStream(fis);
  Employee emp1 = (Employee) in.readObject();

  System.out.println(emp1.id + " " + emp1.firstName + " " + emp1.lastName);
 }
}

Compile above java class, it generates Employee.class file. Open Employee.class file JD, you will see the source code for Employee class.



Decompiler shows following code.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Employee implements Serializable {
 int id;
 String firstName, lastName;
 byte b[] = { 1, 2, 3, };

 Employee(int id, String firstName, String lastName) {
  this.id = id;
  this.firstName = firstName;
  this.lastName = lastName;
 }

 public static void main(String args[]) throws Exception {

  /* Serialize object */
  FileOutputStream fos = new FileOutputStream("ser.out");
  ObjectOutputStream out = new ObjectOutputStream(fos);
  Employee emp = new Employee(1, "Krishna", "Arjun");
  out.writeObject(emp);

  /* Deserialize object */
  FileInputStream fis = new FileInputStream("ser.out");
  ObjectInputStream in = new ObjectInputStream(fis);
  Employee emp1 = (Employee) in.readObject();

  System.out.println(emp1.id + " " + emp1.firstName + " " + emp1.lastName);
 }
}


In the same way, you can decompile jar files also.



mahout: Generating Recommendations for Boolean data sets

Sometimes you need to generate recommendations for input data, which has no preference values. What I mean is, data should be like following, without preferences.

User_id1 item_id1
User_id2 item_id2
User_id3 item_id3
User_id4 item_id4

Above kind of data is called Boolean data, since it has no preference value. To handle such kind of data, we need to select proper similarity algorithms and recommenders.

Choosing similarity algorithm
For this example, i am going to use TanimotoCoefficientSimilarity, is intended for "binary" data sets (preference value doesn't matter here).

Choose Recommender
I am going to use GenericBooleanPrefUserBasedRecommender here. (You can use GenericBooleanPrefItemBasedRecommender, for item based recommendations)
import java.io.File;
import java.util.List;

import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericBooleanPrefUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.TanimotoCoefficientSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;

public class RecommenderExample {
 private static String[] books = { "Meet Big Brother",
   "Explore the Universe", "Memoir as metafiction",
   "A child-soldier's story", "Wicked good fun",
   "The 60s kids classic", "A short-form master",
   "Go down the rabbit hole", "Unseated a president",
   "An Irish-American Memoir" };

 private static final int NEIGHBOR_HOOD_SIZE = 5;

 private static String inputFile = "/Users/harikrishna_gurram/customer.csv";

 /**
  * Get Recommender instance for given input file
  * 
  * @param fileName
  * @return
  * @throws Exception
  */
 public static Recommender getRecommender(String fileName) throws Exception {
  /* Create DataModel object from preferences data store in file */
  DataModel model = new FileDataModel(new File(fileName));

  /* Get Person correlation instance from given model */
  // UserSimilarity similarity = new PearsonCorrelationSimilarity(model);

  UserSimilarity similarity = new TanimotoCoefficientSimilarity(model);
  /*
   * Computes a neighborhood consisting of the nearest n users to a given
   * user.
   */
  UserNeighborhood neighborhood = new NearestNUserNeighborhood(
    NEIGHBOR_HOOD_SIZE, similarity, model);

  /* Get Recommender */
  Recommender recommender = new GenericBooleanPrefUserBasedRecommender(
    model, neighborhood, similarity);

  return recommender;
 }

 /**
  * Get noOfRecommendations for given customer
  * 
  * @param recommender
  * @param custId
  * @param noOfRecommendations
  * @return
  * @throws Exception
  */
 public static List<RecommendedItem> getRecommendations(
   Recommender recommender, int custId, int noOfRecommendations)
   throws Exception {
  return recommender.recommend(custId, noOfRecommendations);
 }

 public static void displayRecommendations(int custId,
   List<RecommendedItem> recommendations) {
  System.out.println("Recommendations for customer " + custId + " are:");
  System.out.println("*************************************************");

  for (RecommendedItem recommendation : recommendations) {
   int bookId = (int) recommendation.getItemID();
   System.out.println(bookId + " " + books[bookId - 1]);
  }

  System.out.println("*************************************************");
 }

 public static void main(String args[]) throws Exception {

  Recommender recommender = getRecommender(inputFile);
  List<RecommendedItem> recommendations;

  recommendations = getRecommendations(recommender, 1, 5);
  displayRecommendations(1, recommendations);

  recommendations = getRecommendations(recommender, 2, 5);
  displayRecommendations(2, recommendations);
 }
}


Output

Recommendations for customer 1 are:
*************************************************
4 A child-soldier's story
1 Meet Big Brother
8 Go down the rabbit hole
6 The 60s kids classic
3 Memoir as metafiction
*************************************************
Recommendations for customer 2 are:
*************************************************
9 Unseated a president
*************************************************



Prevoius                                                 Next                                                 Home