Data Security By Using Encryption And Decryption In Java
Posted By : Kiran Sharma | 29-Sep-2017
Encryption is a process of hiding the meaning of a part of the information and only authorized persons can read the actual data. This process is used to protect data that is being transferred from one location to another. This process is very much needed by a secure computing environment.
Decryption ->When we encrypt data then for the authorized person we need to decrypt that data. Encryption is also known as “cipher” and decryption process is “decipher”.
The methods which are providing security to our information, are given next level of security by “keys”. Now lets talk about keys , the one who is having key only that can encode and decode the information. Here in our code we have used the following API for encryption and decryption. The level of security of an encryption scheme is directly proportional to the its key size. Key sizes should be long enough due to which attacks become unfeasible.
Algorithms :
1.Symmetric encryption algorithms: This alogrithm uses the exactly same key for data encryption and data decryption. For this algorithm use AES or AESWrap block cipher 2.Asymmetric (public key) encryption algorithms: This algorithm uses two different keys for both the processes. For this algorithm use RSA.
Following parameters should be configured correctly to configure any encryption scheme securely.
1.Choosing the correct algorithm
2.Choosing the correct operation mode
3.Choosing the right padding scheme
4.Choosing the right keys and their sizes
CODE DEMO FOR IMPLEMENTING ENCRYPTION AND DECRYPTION
import java.security.Key;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.xml.bind.DatatypeConverter;
import play.Play;
import play.Play;
public class EncryptionDecryptionUtil
{
private static final String ALG = "AES";
private static final byte[] keyVal = new String( Play.application().configuration().getString("application.secret")).substring(0, 16).getBytes();
//To get encrypted value of a string
public static String encrypt(String valToEnc) throws Exception
{
Key ky = generateKey();
Cipher cph = Cipher.getInstance(ALG);
c.init(Cipher.ENCRYPT_MODE, ky);
byte[] encValue = cph.doFinal(valToEnc.getBytes());
String encryptedVal = DatatypeConverter.printBase64Binary(encValue);
return encryptedVal; }
//To get decrypted value of a string
public static String decrypt(String encryptedVal) throws Exception {
Key ky = generateKey();
Cipher c = Cipher.getInstance(ALG);
c.init(Cipher.DECRYPT_MODE, ky);
byte[] decordedVal = DatatypeConverter.parseBase64Binary(encryptedVal);
byte[] decValue = cph.doFinal(decordedVal);
String decryptedVal = new String(decValue);
return decryptedVal;
}
private static Key generateKey() throws Exception
{
Key ky = new SecretKeySpec(keyVal, ALG);
return ky;
}
}
}
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Kiran Sharma
Kiran has good knowledge of java with Servlets, JSPs, Spring, and hibernate frameworks. She is very honest towards her work. Her hobby is listening to music.