Saturday, 25 February 2017

Explain different encryption and decryption techniques

Hi guys, here I am going to explain different encryption and decryption algorithm.
In encryption we completely make the readable content into unreadable so that only desirable person can understand the exact data and for outer world that unreadable (encoded) data will be meaningless.

Data encryption is not a new thing it’s happening even in very past time. Enigma is best and most popular example of encryption and decryption. It was more popular during 2nd world war, Enigma was invented by the German engineer Arthur Scherbius at the end of World War I.
Refer below URL to get a flavor

Encryption is a process to encode the meaning full data into unreadable content so that only desire person can decode and read it. Encryption is a security method in which information is encoded in such a way that only authorized user can read it. It uses encryption algorithm to generate cipher text that can only be read if decrypted.



Type’s encryption techniques-

There are two types of encryptions schemes as listed below:

Symmetric methods
Symmetric encryption is also known as private-key cryptography, and is called so because the key used to encrypt and decrypt the message must remain secure, because anyone with access to it can decrypt the data. Using this method, a sender encrypts the data with one key, sends the data (the ciphertext) and then the receiver uses the key to decrypt the data.

Asymmetric methods
Asymmetric encryption, or public-key cryptography, is different than the previous method because it uses two keys for encryption or decryption (it has the potential to be more secure as such). With this method, a public key is freely available to everyone and is used to encrypt messages, and a different, private key is used by the recipient to decrypt messages.

Hashing
Hashing creates a unique, fixed-length signature for a message or data set. Each “hash” is unique to a specific message, so minor changes to that message would be easy to track. Once data is encrypted using hashing, it cannot be reversed or deciphered.



Java encryption examples-

Code snippet-

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AESTest {

       private static SecretKeySpec secretKey;
       private static byte[] key;

       public static void setKey(String myKey) {
              MessageDigest sha = null;
              try {
                     key = myKey.getBytes("UTF-8");
                     sha = MessageDigest.getInstance("SHA-1");
                     key = sha.digest(key);
                     key = Arrays.copyOf(key, 16);
                     secretKey = new SecretKeySpec(key, "AES");
              } catch (NoSuchAlgorithmException e) {
                     e.printStackTrace();
              } catch (UnsupportedEncodingException e) {
                     e.printStackTrace();
              }
       }

       public static String encrypt(String strToEncrypt, String secret) {
              try {
                     setKey(secret);
                     Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                     cipher.init(Cipher.ENCRYPT_MODE, secretKey);
                     return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
              } catch (Exception e) {
                     System.out.println("Error while encrypting: " + e.toString());
              }
              return null;
       }

       public static String decrypt(String strToDecrypt, String secret) {
              try {
                     setKey(secret);
                     Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
                     cipher.init(Cipher.DECRYPT_MODE, secretKey);
                     return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
              } catch (Exception e) {
                     System.out.println("Error while decrypting: " + e.toString());
              }
              return null;
       }
}

public class EncryptionDecryptionAES {
       static Cipher cipher;

       public static void main(String[] args) throws Exception {
              KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
              keyGenerator.init(128);
              SecretKey secretKey = keyGenerator.generateKey();
              cipher = Cipher.getInstance("AES");

              String plainText = "AES Symmetric Encryption Decryption";
              System.out.println("Plain Text Before Encryption: " + plainText);

              String encryptedText = encrypt(plainText, secretKey);
              System.out.println("Encrypted Text After Encryption: " + encryptedText);

              String decryptedText = decrypt(encryptedText, secretKey);
              System.out.println("Decrypted Text After Decryption: " + decryptedText);
       }

       public static String encrypt(String plainText, SecretKey secretKey)
                     throws Exception {
              byte[] plainTextByte = plainText.getBytes();
              cipher.init(Cipher.ENCRYPT_MODE, secretKey);
              byte[] encryptedByte = cipher.doFinal(plainTextByte);
              Base64.Encoder encoder = Base64.getEncoder();
              String encryptedText = encoder.encodeToString(encryptedByte);
              return encryptedText;
       }

       public static String decrypt(String encryptedText, SecretKey secretKey)
                     throws Exception {
              Base64.Decoder decoder = Base64.getDecoder();
              byte[] encryptedTextByte = decoder.decode(encryptedText);
              cipher.init(Cipher.DECRYPT_MODE, secretKey);
              byte[] decryptedByte = cipher.doFinal(encryptedTextByte);
              String decryptedText = new String(decryptedByte);
              return decryptedText;
       }

}

No comments:

Post a Comment