Java Encryption and Decryption

Hi Folks,

Most of you would be very much interested to know how to use java to do crypto work.
In this post, I would demonstrate how to use JCE(Java Cryptography Extension) Classes and methods to very simply Encrypt and Decrypt a PlainText.

This article is intended for an audience who is familiar with cryptography concepts and is looking for an implementation of such an algorithm in Java. If you are looking to learn Cryptography concepts, this post may not be helpful.

JCE provides us with a bunch of useful classes to accomplish this. I would demonstrate how to do so using Cipher class. Cipher class is handy to handle such task. You can get an instance of a Cipher Class by calling its static method

ENCRYPTION

Cipher cipher=Cipher.getDefaultInstance("DES");

This accepts a string indicating the encrypting algorithm. For now, we will use DES(Data Encryption Standard)

In order to Encrypt, we need a key. A key can be easily generated by using the KeyGenerator Class provided.

KeyGenerator keyGen=KeyGenerator.getInstance("DES");
Key key=keyGen.generateKey();

Once we have a key, Encryption is simple.

We first need an array of bytes for encryption.

byte[] encryptedStream="HelloWorld".getBytes();

Then, we need to set the Encryption mode of Cipher object we have for Encryption.
The Cipher object can be used for decryption also by setting the mode.
It can be done as follows.
As you may further observe, the init() method requires the key also.

cipher.init(Cipher.ENCRYPT_MODE, key);

Once, we have done the init(), we can Encrypt using the doFinal() method of Cipher Object.
This returns a byte array.

encryptedStream = cipher.doFinal(inputStream);

DECRYPTION

Decryption is also very similar.
Make sure, you use the same key for decryption(If you call KeyGenerator.getInstance("DES") once more, you would get a different key and you would not be able to decrypt the ciphertext back to the same input )

We just need to change the mode of the Cipher object to Decrypt.
It is done very similarly.

cipher.init(Cipher.DECRYPT_MODE, key);

decryptedStream=cipher.doFinal(inputStream);

Have FUN!!!!!!!

Comments

Unknown said…
DES is very basic level encryption. can you post a example on how to encrypt and decrypt for MD5
ruchira said…
Hi Abdul,

MD5 is not for encryption. It's widely used for hash functions. Hash functions cannot be decrypted back. The way hash functions are used is that you compare the two hashes to check whether the results are the same.

Anyway, if you want to use MD5, I believe JCE supports such features.

When getting a reference for the Cipher object, you would need to pass the parameters altered.

Try using;

Cipher cipher=Cipher.getDefaultInstance("MD5");

I haven't used this, so have a good look at the JCE documentation. I'm sure you can find your answer there.
Image Guru said…
Hello Ruchira,

I am getting problem Cannot access "java.security.Key" when I use the fiollowing statement

Key mykey1= keyGen.generateKey();

I have added the "crypto-145.zip" file. Please let me know if there is any other file which I have to add into my project to remove the above steted error.

Thank you
Andrea said…
I agree with Abdul that it provides basic level of encryption. I wanted to know about other means which offer advanced level of security. Please help me out by throwing light in this area.
electronic signature software

Popular posts from this blog

Encrypt and Decrypt Images Using Java

Build your own Network sniffer

ASP Response.Write newline