Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Encrypting and decrypting content with Java

In this article we will learn how make a program which to helps you learn how you can perform encryption/decryption with java.

You may use this program to encrypt your passwords and then store those in database so that they are secure. First we call al the classes which we want to import.

Listing 1: Importing classes

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.Cipher;
import sun.misc.BASE64Encoder;

Here:

  1. KeyGenerator is used to Generate an AES key
  2. SecretKey is used to store and use the generated key
  3. Cipher is used for encryption and decryption purpose.
  4. BASE64Encoderis for encoding of data.

Now, We will make the class along with the main method:

Listing 2: Class declaration along with Main method

public class AesEncrDec 
{
public static void main(String args[])
{
	new AesEncrDec().encrypt();
} 

Here,

  1. Name of class is AesEncrDec
  2. We define the main method and called the encrypt method which will contain the main logic.

Now we will make the encrypt method containing the logic,

Listing 3: Making the keygenerator and making key

void encrypt()
{
	try
	{
	String plainData="hello",cipherText,decryptedText;
	KeyGenerator keyGen = KeyGenerator.getInstance("AES");
	keyGen.init(128);
	SecretKey secretKey = keyGen.generateKey();

Here:

  1. We defined a function called encrypt.
  2. We defined a variable called plainData which contain the data to be encrypted.
  3. We defined a Keygenerator and since we are using AES encryption so we pass an instance of AES to the keygenerator.
  4. Now we want a 128 bit key so we set the initialize the generator with 128
  5. We generate a key from the generator and store that in a SecretKey.

Now we will perform the encryption part:

Listing 4: Performing the encryption

Cipher aesCipher = Cipher.getInstance("AES");
	aesCipher.init(Cipher.ENCRYPT_MODE,secretKey);
	byte[] byteDataToEncrypt = plainData.getBytes();
	byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt); 
	cipherText = new BASE64Encoder().encode(byteCipherText);

Here:

  1. We pass an instance of AES to Cipher.
  2. We initialize the cipher telling it that it has to perform encryption and also give it the key to be used.
  3. We perform the encryption using the doFinal method on byte array of the input string.
  4. We encode the data to obtain the final encrypted data.

Now we will perform the decryption part:

Listing 5: Performing the decryption

	aesCipher.init(Cipher.DECRYPT_MODE,secretKey,aesCipher.getParameters());
	byte[] byteDecryptedText = aesCipher.doFinal(byteCipherText);
	decryptedText = new String(byteDecryptedText);
	System.out.println("\n Plain Data : "+plainData+" \n Cipher Data : "+cipherText+" \n Decrypted Data : "+decryptedText);
	}
	catch(Exception e)
	{
		
	}
}
}

Here:

  1. We initialize the cipher to use decryption mode and pass it the key which will be used for decryption
  2. We perform the decryption on the bytearray of the cipher text
  3. Now we convert it into string
  4. We display the final results.

Full Source Code:

Listing 6: Full Source code

package article13;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.Cipher;
import sun.misc.BASE64Encoder;

public class AesEncrDec 
{
public static void main(String args[])
{
	new AesEncrDec().encrypt();
}
void encrypt()
{
	try
	{
	String plainData="hello",cipherText,decryptedText;
	KeyGenerator keyGen = KeyGenerator.getInstance("AES");
	keyGen.init(128);
	SecretKey secretKey = keyGen.generateKey();
	Cipher aesCipher = Cipher.getInstance("AES");
	aesCipher.init(Cipher.ENCRYPT_MODE,secretKey);
	byte[] byteDataToEncrypt = plainData.getBytes();
	byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt); 
	cipherText = new BASE64Encoder().encode(byteCipherText);
	aesCipher.init(Cipher.DECRYPT_MODE,secretKey,aesCipher.getParameters());
	byte[] byteDecryptedText = aesCipher.doFinal(byteCipherText);
	decryptedText = new String(byteDecryptedText);
	System.out.println("\n Plain Data : "+plainData+" \n Cipher Data : "+cipherText+" \n Decrypted Data : "+decryptedText);
	}
	catch(Exception e)
	{
		
	}
}
}

Output:

Plain Data : hello Cipher Data : VD7EhOL4+n+h9mK+Gsj7/A== Decrypted Data : hello


My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login