Thursday, 1 June 2017

Encryption And Decryption Using AES Algorithm

1.AESEncryption.java


package com.tutorial4u;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;

/**
 * This example program shows how AES encryption and decryption can be done in
 * Java. Please note that secret key and encrypted text is unreadable binary and
 * hence in the following program we display it in hexadecimal format of the
 * underlying bytes.
 *
 * @author ashish
 */
public class AESEncryption {
       /**
        * gets the AES encryption key. In your actual programs, this should be
        * safely stored.
        *
        * @return
        * @throws Exception
        */
       public static SecretKey getSecretEncryptionKey() throws Exception {

             Date today = new Date();
             SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMyyyy");

             // Key is last 8 digits of JFS CIS_NUMBER (i.e. U65923KA2006PTC040028) +
             // DDMMYYYY

             String stringKey = "TC040028" + dateFormat.format(today);

             byte[] keyBytes = stringKey.getBytes("UTF-8");

             SecretKey secretKey = new SecretKeySpec(keyBytes, 0, keyBytes.length, "AES");

             return secretKey;
       }

       /**
        * Encrypts plainText in AES using the secret key
        *
        * @param plainText
        * @return
        * @throws Exception
        */
       public static byte[] encryptText(String plainText) throws Exception {
             // AES defaults to AES/ECB/PKCS5Padding in Java 7
             Cipher aesCipher = Cipher.getInstance("AES");
             aesCipher.init(Cipher.ENCRYPT_MODE, getSecretEncryptionKey());

             byte[] byteCipherText = aesCipher.doFinal(plainText.getBytes());
             return byteCipherText;
       }

       /**
        * Decrypts encrypted byte array using the key used for encryption.
        *
        * @param byteCipherText
        * @return
        * @throws Exception
        */
       public static String decryptText(byte[] byteCipherText) throws Exception {
             // AES defaults to AES/ECB/PKCS5Padding in Java 7
             Cipher aesCipher = Cipher.getInstance("AES");
             aesCipher.init(Cipher.DECRYPT_MODE, getSecretEncryptionKey());
             byte[] bytePlainText = aesCipher.doFinal(byteCipherText);
             return new String(bytePlainText);
       }

       /**
        * Convert a binary byte array into readable hex form
        *
        * @param hash
        * @return
        */
       public static String bytesToHex(byte[] hash) {
             return DatatypeConverter.printHexBinary(hash);
       }

       /**
        * Convert a readable hex to String
        *
        * @param hash
        * @return
        */
       public static byte[] hexToBytes(String hash) {
             return DatatypeConverter.parseHexBinary(hash);
       }

}



2.AESTest.java



package com.tutorial4u;

public class AESTest {

       public AESTest() {
             try {
                    String originalText = "password@#";
                    String encryptedText = AESEncryption.bytesToHex(AESEncryption.encryptText(originalText));
                    String decryptedText = AESEncryption.decryptText(AESEncryption.hexToBytes(encryptedText));

                    System.out.println("Original Text : " + originalText);
                    System.out.println("Encrypted Text (Hex Form) : " + encryptedText);
                    System.out.println("Decrypted Text : " + decryptedText);
             } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
             }
       }
       public static void main(String[] args) {
             new AESTest();
       }

}

No comments:

Post a Comment

No of Occurrence in Array

package com.tutorial4u; import java.util.HashMap; /************************************************************  No of Occurrence in Array...