Encrypting-Decrypting a file using Java Cryptography Extension

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

public class JCEEncryptDecrypt {

    public static void encrypt(String PlaintextFile, String Key, String CiphertextFile) {
        try {
            byte[] KeyData = Key.getBytes();
            SecretKeySpec KS = new SecretKeySpec(KeyData, "Blowfish");
            Cipher cipher = Cipher.getInstance("Blowfish");
            cipher.init(Cipher.ENCRYPT_MODE, KS);
            FileInputStream fis = new FileInputStream(PlaintextFile);
            CipherInputStream cis = new CipherInputStream(fis, cipher);
            FileOutputStream fos = new FileOutputStream(CiphertextFile);
            byte[] b = new byte[1024];
            int i = cis.read(b);
            while (i != -1) {
                fos.write(b, 0, i);
                i = cis.read(b);
            }
            fos.flush();
            fos.close();
            fis.close();
            cis.close();
            System.out.println("Encryption Successfull !!!");
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IOException ex) {
            System.out.println(ex);
        }
    }

    public static void decrypt(String CiphertextFile, String Key, String DecipheredFile) {
        try {
            byte[] KeyData = Key.getBytes();
            SecretKeySpec KS = new SecretKeySpec(KeyData, "Blowfish");
            Cipher cipher = Cipher.getInstance("Blowfish");
            cipher.init(Cipher.DECRYPT_MODE, KS);
            FileInputStream fis = new FileInputStream(CiphertextFile);
            FileOutputStream fos = new FileOutputStream(DecipheredFile);
            CipherOutputStream cos = new CipherOutputStream(fos, cipher);
            byte[] b = new byte[1024];
            int i = fis.read(b);
            while (i != -1) {
                cos.write(b, 0, i);
                i = fis.read(b);
            }
            fos.flush();
            fos.close();
            fis.close();
            cos.flush();
            cos.close();
            System.out.println("Decryption Successfull !!!");
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IOException ex) {
            System.out.println(ex);
        }
    }

    public static void main(String[] args) {
        encrypt("D:\\plaintext.txt", "testkey", "D:\\ciphertext.txt");
        decrypt("D:\\ciphertext.txt", "testkey", "D:\\originaltext.txt");
    }
}

JAXB without using an XML Schema

In this article I’ll show you how to make use of the Java Architecture for XML Binding – JAXB without using the XML Schema Definition file. JAXB is used for marshalling and unmarshalling Plain Old Java Objects to & from XML files. Although a Binding compiler can be used to generate JAXB compliant classes, with the help of annotations you can create those classes manually.

Continue reading

Capturing network packets using jNetPcap API

In this article, I’ll show you how to capture the network packets using Java. While studying networking you might have come across different kinds of packets, their header formats, their fields etc. But ever thought how it would be to grab one of them and study ? If yes then read on…this article will gratify your eagerness.

Here i will be using jNetPcap library. It is an open source java library, used to capture and decode network packets. It uses native implementations to provide optimum packet decoding performance.

Continue reading

ANT : Another Neat Tool

Assuming you are a Java Programmer, are you tired of compiling, packaging and generating docs for java sources manually ? I am sure, many of you might have thought, there should be some tool to automate all these trivial tasks. while searching for such a tool I came across an Ant, which led me to write this article.

Continue reading

A Gentle Introduction to Java Native Interface

In Real world, however grown-up and efficient a child may be, there comes a time when it has to return to its mother for assistance. the field of programming is no exception to it, after all programming is all mimicking real world entities. Even powerful programming language like Java has to depend on its ancestors (no doubt they are still very young) C, C++ etc.

We all  know any java program runs inside a managed environment called JVM which restricts the programs from accessing critical parts of system, also there is no low-level memory manipulation possible in java. so in such cases and many more, a Java program has to depend on other languages like C, C++ or others to accomplish what it alone cannot.

Continue reading