Thursday, October 5, 2017

Data Security By Using Encryption And Decryption In Java

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<span style="font-size:16px;"><span style="font-family:arial,helvetica,sans-serif;">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(016).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;
        }
    }
}<span style="white-space: normal;">
</span></span></span>

No comments:

Post a Comment