Monday, July 2, 2018

Generate PKCS12 Format Key Store From Pem Format Cert Files Using OpenSSL

Use below Command in order to generate PKCS12 key store.

openssl pkcs12 -export -out bundle.pfx -inkey privkey.pem -in cert.pem -certfile chain.pem

bundle.pfx : output file name
inkey         : private key
certfile      : certificate chain

Sunday, November 5, 2017

Java Instrumentation Agent


A Java-agent is a special program which can be used to inspect or modify another Java program at run time. Java agents were introduced with the Java Instrumentation package in JDK 1.5.

Java agents are mostly used for Debugging, Profiling and Logging a program.

Let's see how Java-agents works by using a simple example
 
In this example we are creating a Java-gent which calculate the execution time of a method and print the time on terminal.

In order to understand this code, you should be familiar with following technologies

  • Java programming 
  • Maven build tool
  • How to create a jar file (Agent is built as a jar file)

In this example we are creating a calculator program. This calculator program is the class to be instrumented by the Java-agent class.


You can save this code in a file named Main.java and run this class from terminal by using following commands.
 javac Main.java                                                        
 java Main                                                           
It will show an output similar to this
Addition : 36
Subtraction : 24
Multiplication : 180
Division : 5.0
Let's create the Java-agent

You need to create 3 files for the Java-agent program.
1. JavaAgent.java
2. pom.xml (Maven)
3. MANIFEST.MF (jar manifest file)

JavaAgent.java
premain method act as the main method of the java agent. This method is executed by the JVM once the agent is attached. Here we are creating a class named SimpleClassTransformer which implements the ClassFileTransformer interface and it has a method named transform. Once the agent attached every class (Except some bootstrap time classes and some classes upon which JavaAgent depends) is passed through this method before the specific class is loaded by the Java class loader.
Read the comments added in the JavaAgent.java file for more information.

MANIFEST.MF 
This file contain the Pre-main class name. JVM find the class name to execute from this file. (There should be a new line at the end of each line of a mainfest file to work properly)

pom.xml
This file contains the dependencies required for the agent program. Here we are using Javassist which is a great library for instrumenting java classes. You can get more information on it through this link. (Javassist)

Instructions to run the program
  • Go to the folder containing src folder and the pom.xml file and build the project using mvn package command.
  • If the build is successful there will be a new jar file named JavaAgent-1.0-SNAPSHOT.jar in the target folder.
  • Now you need to add this jar file as the java-agent  of the first program(calculator) and execute it again.
  • Go to the folder containing calculator program which we have developed earlier. There should be two files(Main.java, Main.class)  in that folder since we have compiled it earlier. 
  • Now execute the following command.
java -cp "/absolute/path/to/javassist-3.18.0-GA.jar:./" -javaagent:"/path/to/JavaAgent-1.0-SNAPSHOT.jar" Main
  • Then you will see an output similar to this
Instrumentatin Agent Added ...
Method execution time in Millis : 0.013584
Addition : 36
Method execution time in Millis : 0.001592
Subtraction : 24
Method execution time in Millis : 0.001158
Multiplication : 180
Method execution time in Millis : 0.001265
Division : 5.0 
This example is shown using a Linux environment if you are on a different operating system then you need to change the terminal commands specific to the relevant operating system. Please share your questions, ideas, & suggestions by commenting below.

Saturday, April 4, 2015

Adding Base64 Encoded Images To Jasper Reports

Base64

Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. This method can be used to transfer binary data as textual data. Click here to read more on Base64

Adding To Jasper Reports

If you need to add the image through a URL(or a file location) to the report you can easily do it by going through image properties. But if you need to set the image through a Base64 encoded character string then you need to add it to the report as shown below.

Assume that the parameter for the Base64 encoded string is 'p_logoURL'. Then you can add this parameter to the image expression as shown below.

Image Expression
That's it. You can see the image in your report.

Sunday, February 22, 2015

How To Create A Windows Batch File



Batch files are used to store DOS commands. Once you run the batch file by double clicking on the file or via any other method such as using command prompt, all the DOS commands are executed sequentially. Normally batch files are mostly used to automate repetitive tasks.  You can save your time by automating repetitive tasks. This method is very useful for developers more than normal computer users.

Creating a batch file is not a complex task.  You need to know only the DOS commands that you are going to put into the batch file and the order of the commands.

Here you can see few of commonly used DOS commands.

  • TITLE      Window name when bat file executes.
  • PAUSE    :  Pause the execution of the bat file and wait until user press any key  to continue
  • CLS        :  Clear the DOS window
  • MKDIR    Create a directory
  • DIR         :  Move into a directory ( “DIR directory_path”) or view current directory (“DIR”)
  • START    :  Execute a file using default application
  • ECHO      :  Print something on the command prompt window
  • ECHO OFF    :    Prevent showing the batch file commands on the command prompt
You can find a list of DOS commands by typing HELP in command prompt 

Let’s create few batch files.

After understood basics you can create complex batch files by adding various commands. First you need to create a file with .bat extension or .cmd extension.

Open notepad --> click save --> select all files --> name it as test.bat or test.cmd --> save





Now you can insert below DOS commands into your file and test them.

1.  A program to create a directory
       MKDIR  Hello_World
       MKDIR “Hello World”

Insert above commands into your test.cmd/test.bat file and save. Then execute these commands by double clicking the on the file. Keep in mind to put double quotation marks if you use spaces in a name.
Above commands will create two folders called Hello_world and Hello World in the same directory where your batch file exists.


2.       Create a text file with a text string
MKDIR “text folder”
CD "text folder"
ECHO "This is the text string" > textFile.txt

This DOS command will create a folder named text folder and create a file named textFile.txt and write a text string into the file.

You can understand the basics of creating a batch file using above examples. Once you got the basics you can create very complex files to automate a process.
Click here to read more on this topic.


Sunday, February 1, 2015

Create Your Own HTML Tags



You might be programming with html and may be thinking how to create your own html tags  to use in html code. This is a very easy task. First you need to define a name for your tag. You should be careful not to use any html reserved words. Assume you have named your tag name as mytag. Use it in the html code as a normal tag.(write opening tag and closing tag)

<mytag>

</mytag>
 Now you need to provide attributes for the tag that you have defined. You can add these attributes using CSS.
<style type="text/css">
     mytag{
            background-color: lawngreen;
            padding : 10px;
            font-style : italic; 
          }
</style>
Now you can use your tag as a normal html tag and  use it in your html code.
Complete code :
<!DOCTYPE html>
<html>
    <head>
         <style type="text/css">
             mytag{
                 background-color: lawngreen;
                 padding: 10px;
                 font-style: italic;
                  }
         </style>
    </head>
    <body>
        <mytag>
            hello world
        </mytag>
    </body>
</html> 

Monday, July 28, 2014

.Net Framework Web Browser Class


Web Browser class is one of the most important class provided by .NET Framework for tasks related with web. It has lots of methods which can be used to implement web related tasks such as web scraping, creating a inbuilt browser in a software and many more.

Now I am going to let you know how to create program which will search a given word in yahoo search engine using this WebBrowser class. The language I am using to explain is C# here but you can use languages as visual C++, F#, VB as well.
You need to have some knowledge about C# and HTML before begin to do this task because almost all the things are represented in HTML in a web page thus you need to have an  understanding about HTML. Also if you do not know anything about C# then you are again in trouble. Therefore the prerequisites are C# (I am using Visual Studio 2013 IDE)  and HTML.

Lets get into work,

open visual studio and go to New => project => select windows forms Application under C#

Then you will see a windows form appear with title Form1. Now drag  a "button", "textbox" and a "web browser" from the tool box.


 Set the URL of the web browser as https://www.yahoo.com and text of the button(not the name) as "Go".
you have to make sure that the names of each component were not changed.
web browser name  : webBrowser1
button name            : button1
text box name         : textBox1

Now click on the text box and set TextChanged event as shown below:


just double clik on the TexChanged event(highlighted in yellow).

then a new file will be opened(Form1.cs)  and you will see a method called "textBox1_TextChanged".

 Now always when we  type something on the text box, this method is invoked. What we expect from this is when we type something on the text box it should be appeared on the search box of the yahoo search engine. Now we place a code inside textBox1_TextChanged method to do this task.

How it looks like after adding the code

The piece of code we used it shown above. Now your basic HTML and C# knowledge will help you to understand what happened and I will explain how we got the element ID.(p_13838465-p).

go to the shown link using your web browser. https://www.yahoo.com

now right click on the search box and click Inspect Element (I assume you know these things)

Finding ID of the search box

Then you will see the ID of the search box.

Our next step is setting the "go" button in the windows form work as "search web" button in the web page.

set button click event handler on the "go" button or just double click on the "go" button to set it automatically. set following code to the button1_Click method.

You can find the ID of the search button by the same method used to find the ID of search box.


This is all with coding and now you can test your program by running the code.

When you run the program you will see the web page is loaded into the web browser window.

Then type anything in the text box of the program, then you will see same text appears on the search box too.

When you click the "go" button it will search the web with the entered word.

I hope now you got an idea about some of usages of the web browser class. Like this you can do lots of advanced things with using this class.

Comment below If you have any question regarding this program.

Saturday, May 31, 2014

How to create c++ header files and make them work in ubuntu

You can easily create header files in c++ and work with them using an IDE(Integrated Development Environment) such as CodeBlocks, Eclipse, etc. If you are using an IDE, you just need to create the relevant files( such as header file, implementation file and main file) and compile them directly using the IDE. so when you give the compile command  the IDE care the rest of the tasks and give you the output.

But if you are going to do this task in a terminal(ubuntu) without using any IDE then you will have to do these all the steps done by the IDE manually. Therefore here is explained how the same thing can be done by using terminal.(only the steps are shown and not in much details)

step 1 :

You need to create the header file as you need
(a sample header file is shown below)

// rectangle.h  (begining of the file)


#ifndef _RECTANGLE_H
#define _RECTANBLE_H


double area(int length, int width);
// initialize any functions as you need
#endif


// end of the file

you need to save this as a header file.(rectangle.h)

step 2 :

Now you need to create the implementation file.
(a sample implementation file is shown below)

//rectangle.cpp (begining of the file)

#include <iostream>
#include "rectangle.h"

using namespace std;


double area(int length, int width){

    return length*width;

}


// end of the file

you need to save this file as a c++ file (rectangle.cpp)


step 3 :

Now you need to create the main file
(a sample main file is shown below)


// main.cpp  beginning of the file

 #include <iostream>
#include "rectangle.h"

int main(){

    std::cout<< area(4,4);

    return 0;

}


// end of the file

you need to save this file as a c++ file (main.cpp)

step 4 :

Now you can run these files on terminal.

put all the above 3 files into a same folder. then open terminal and go to the folder where these 3 files reside. (using "cd" command   eg : "cd Desktop\header_test_folder")

first you need to compile all c++ files.
type bellow command for each c++ file

g++ -c filename.cpp
 //other cpp files (if any)
g++ -c main.cpp

example : (for above files)

g++ -c rectangle.cpp
g++ -c main.cpp

now you need to link these files and create the executable file.
so you need to run below command for that.

g++ -o finalfilename main.o filename.o  // otherfiles.o (if any) 

example : (for above files)

g++ -o finalrectangle main.o rectangle.o

now you can see there is a new file in your directory(finalfilename)
you need to run that file

./finalfilename

for above example

./finalrectangle

now it will be running on the terminal.