Sunday, June 29, 2014
Today in this tutorial I am going to show you how to generate and use Digital Signature to sign any data or document in Java. Digital Signatures are a very important part of security. It ensures authenticity and non-repudiation of any data. In order to prove authenticity, the sender of the data signs the data with a digital signature and the receiver verifies the signature. Here we will only consider about signing the data, while verifying digital signature will be discussed in another later tutorial.
How to sign data with digital signature ?
1. Form the message to be signed.
2. Generate a public-private key pair
3. Calculate hash of the message and encrypt it with sender's private key
4. Send the dugutally signed message with the signature along with the public key.
In this tutorial, we will be signing the data stored in a file. The file path will be taken as an input. The resulting output will be two file : a .dsa file containing the digital signature, and a .pubkey file containing the public key in encoded form.
How to sign data with digital signature ?
1. Form the message to be signed.
2. Generate a public-private key pair
3. Calculate hash of the message and encrypt it with sender's private key
4. Send the dugutally signed message with the signature along with the public key.
In this tutorial, we will be signing the data stored in a file. The file path will be taken as an input. The resulting output will be two file : a .dsa file containing the digital signature, and a .pubkey file containing the public key in encoded form.
Labels:Security | 0
comments
Sunday, June 15, 2014
Today in this article I will discuss with an amazing feature of Java7. Here I will show you how to create a file/folder watcher/watch service. In other words I will actually write Java code which will be able to monitor a file/folder/directory for changes. This will require Java 7 NIO .2. This monitoring service is very useful in many applications like IDE. Also this can be used for hot deployment in servers. Monitoring or watching a folder for changes means that whenever there is a change in file system associated with the folder then an event is triggerd. The event is catched and associated change is extracted to know what change has occurred and based on that different actions can be taken.
Below are the steps you must follow to implement watch service
1. Get the path of the folder - The first step is to get the Path object associated with the folder. This is done using Paths.get() method.
2. Get file system of the path - Next step is to get the FileSystem object associated with the Path object. This is done using getFileSystem() method.
3. Get watch service - Next you have to get WatchService from FileSystem object using newWatchService() method.
Below are the steps you must follow to implement watch service
1. Get the path of the folder - The first step is to get the Path object associated with the folder. This is done using Paths.get() method.
2. Get file system of the path - Next step is to get the FileSystem object associated with the Path object. This is done using getFileSystem() method.
3. Get watch service - Next you have to get WatchService from FileSystem object using newWatchService() method.
Labels:Files | 0
comments
Monday, June 9, 2014
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem that is solved by recursion. It has 3 towers and the first tower contains n number of disks. A person is said to have completed or solve the problem if he is able to move all the disks to the third tower with certain constraints.
Similarly in the game that is developed here follows the rules - 1. Only one disk can be moved at a time. 2. Cannot place a larger disk on a smaller disk. The game her when starts will start with 4 initial disks. You will have to move all the disk to 3rd tower. You can use this game and ask people to complete and win the game of Tower of Hanoi. You also have a choice to select the number of disks you want to play with. So our main objective her is that we will actually not solve the Tower of Hanoi problem, rather use it as a game and ask players of the game to solve the problem.
Similarly in the game that is developed here follows the rules - 1. Only one disk can be moved at a time. 2. Cannot place a larger disk on a smaller disk. The game her when starts will start with 4 initial disks. You will have to move all the disk to 3rd tower. You can use this game and ask people to complete and win the game of Tower of Hanoi. You also have a choice to select the number of disks you want to play with. So our main objective her is that we will actually not solve the Tower of Hanoi problem, rather use it as a game and ask players of the game to solve the problem.
Labels:Swing | 5
comments
Sunday, June 8, 2014
Today in this article I will tell you how to create a generic binary search tree (BST) in Java. A normal binary tree is any tree where each node can have a maximum of 2 nodes. But binary search tree is a special kind of binary tree which when traveresed using inorder traversal algorithm lists the nodes in ascending order. In this article we will not deal with traversal, and only concentrate on creating and generating the BST.
The code is written to support generics. This means that only one code has to be written for binary search tree(BST). You can insert anything in the tree but the objects must be comparable. Comparable objects here means that classes of those objects must extend java.lang.Comparable interface. The comments are also added to the code in Javadoc style
The code is written to support generics. This means that only one code has to be written for binary search tree(BST). You can insert anything in the tree but the objects must be comparable. Comparable objects here means that classes of those objects must extend java.lang.Comparable interface. The comments are also added to the code in Javadoc style
Labels:Algorithms,Generics | 0
comments
Thursday, June 5, 2014
Today in this article I will show you how to create your own annotation. Here we will start with a very basic annotation and then slowly show you how to include arrays and also how to use default values. We will also show you how to yo use annotations. We have divided our tutorial on creating and using annotations in two parts. The first part meaning this one is a very basic and good one for beginners to start with.
In our first annotation we will just start with an empty annotation specifying only@target and @Retention.
In our first annotation we will just start with an empty annotation specifying only
import java.lang.annotation.*; @Target(value={ElementType.FIELD}) @Retention(value=RetentionPolicy.RUNTIME) public @interface Author { }In the next example you will see that when there is only one value for an annotation then we can omit the name as well as braces{} indicating arrays. The above sample is re-written as
import java.lang.annotation.*; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Author { }In our next example you will see how to mention the target type when it is more than one say FIELD and METHOD.
import java.lang.annotation.*; @Target({ElementType.FIELD, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface Author { }In our next example we will deal with annotation which has annotation type elements. It looks almost like methods. We will also show you how to use this annotation in a class.
import java.lang.annotation.*; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Author { String name(); } class MyClass { @Author(name="Nirupam") int i = 10; }In our next and last example of today, we will show you how to mention default values of annotation type elements. It is done using keyword default . If a default value is mentioned, then you can omit adding your own value. But it does not mean that you cannot add your own value, of course you can do it.
import java.lang.annotation.*; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Author { String name() default "Nirupam"; } class MyClass { @Author int i = 10; }In our next and last part we will deal with creating and using advanced annotation. Till then keep coding and keep reading our articles.
Labels:Annotation | 0
comments
Subscribe to:
Posts
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...