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"); } }
Author Archives: compsciple
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.
The Decorator Design Pattern
The Decorator design pattern is a structural design pattern that allows you to add functionalities to an object at runtime.
Why Decorator ?
- If you need to attach additional responsibilities to an object dynamically.
- When sub-classing becomes impractical.
- You have a large number of possible functionalities and you need independent ways to add them to object.
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.
Working with COM Ports using JAVA
In this article, I’ll show you how to interact with the COM Ports of a machine from Java, many a times you need to perform some I/O on the ports, may be reading coordinates from a GPS device, values from a Barcode scanner or may be your friend, an electronics guy wants you to help him in his project.
The Simple Factory Design Pattern
The Simple Factory design pattern is a creational pattern, which saves you from coupling problems in contexts where objects are created based on some condition at runtime rather than specifying the exact class.
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.
The Ins and Outs of C++ Virtual Functions
Looking at the title of this article, you may wonder as to what’s so intriguing about virtual functions of C++ ?. This is indeed a long article. I couldn’t help reducing its size. you may or may not be already aware of what virtual functions are, anyways i’ll start from the beginning (although some knowledge of C++ and inheritance is required).
The Singleton Design Pattern
The Singleton design pattern is the easiest creational pattern, the peculiarity of this design pattern is that it restricts the instantiation of a class to one object.
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.