Java Encryption - Decryption Code

package myLib;


import java.io.UnsupportedEncodingException;

import java.security.GeneralSecurityException;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

import java.security.SecureRandom;

import javax.crypto.Cipher;

import javax.crypto.NoSuchPaddingException;

import javax.crypto.spec.IvParameterSpec;

import javax.crypto.spec.SecretKeySpec;

import javax.xml.bind.DatatypeConverter;

import org.apache.xml.security.exceptions.Base64DecodingException;


/**

 *

 * @author Mohit Kumar Gupta

 */

public class CryptoAES256 {


   public static void main(String[] args) {

        try {

            String passP = "Bar12345Barjhggfh12345"; // 128 bit key

            String encryptedString = encrypt(passP, "Hello Mohit");

            System.out.println("EncryptedString= " + encryptedString);

            String decryptedString = decrypt(passP, encryptedString);

            System.out.println("DecryptedString= " + decryptedString);

//        System.out.println(decrypt(key, initVector,

//                encrypt(key, initVector, "Hello World")));


            String passPhase = "Bar1234";

            String value = "Hello Mohit 1 2 3 4 5 6";

            String enc = encrypt(passPhase, value);

            String dec = decrypt(passPhase, enc);

            System.out.println(enc);

            System.out.println(dec);

        } catch (Exception e) {

            e.printStackTrace();

        }

    }


    private static SecretKeySpec getKeySpec(String passphrase) throws NoSuchAlgorithmException, UnsupportedEncodingException {

        MessageDigest digest = MessageDigest.getInstance("SHA-256");

        return new SecretKeySpec(digest.digest(passphrase.getBytes("UTF-8")), "AES");

    }


    private static Cipher getCipher() throws NoSuchPaddingException, NoSuchAlgorithmException {

        return Cipher.getInstance("AES/CBC/PKCS5PADDING");

    }


    public static String encrypt(String passphrase, String value) throws GeneralSecurityException, UnsupportedEncodingException {

        byte[] initVector = new byte[16];

        SecureRandom.getInstanceStrong().nextBytes(initVector);

        Cipher cipher = getCipher();

        cipher.init(Cipher.ENCRYPT_MODE, getKeySpec(passphrase), new IvParameterSpec(initVector));

        byte[] encrypted = cipher.doFinal(value.getBytes());

        return DatatypeConverter.printBase64Binary(initVector)

                + DatatypeConverter.printBase64Binary(encrypted);

    }


    public static String decrypt(String passphrase, String encrypted) throws GeneralSecurityException, UnsupportedEncodingException, Base64DecodingException {

        byte[] initVector = DatatypeConverter.parseBase64Binary(encrypted.substring(0, 24));

        Cipher cipher = getCipher();

        cipher.init(Cipher.DECRYPT_MODE, getKeySpec(passphrase), new IvParameterSpec(initVector));

        byte[] original = cipher.doFinal(DatatypeConverter.parseBase64Binary(encrypted.substring(24)));

        return new String(original);

    }

}


No comments:

Post a Comment

If you have any doubts, please let me know.

Various types of salts effect on health (discussion with gemenie)

Is  rock salt or bitnoon helps in digestion or just a airuvedic nuska This is for informational purposes only. For medical advice or diagnos...