DreamsCoder.com

We Code your Dreams


home > AES Encryption and Decryption using Java




AES Encryption and Decryption using Java










import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AES {

    public void operation() {
      try {
         String text = "Hello DreamsCoder reader";
         
        String key = "x4690alo09549a12"; // 128 bit key
         
         // Create key and cipher
         Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
         Cipher cipher = Cipher.getInstance("AES");

         // encrypt the text
         cipher.init(Cipher.ENCRYPT_MODE, aesKey);
         byte[] encrypted = 
cipher.doFinal(text.getBytes());
         System.out.println("Encrypted Text	");
         System.err.print(new String(encrypted));
  
         // decrypt the text
         cipher.init(Cipher.DECRYPT_MODE, aesKey);
         String decrypted = new
String(cipher.doFinal(encrypted));
         System.out.println("Decrypted Text	");
         System.err.print(decrypted);
      }catch(Exception e) {
         e.printStackTrace();
      }
    }

    public static void main(String[] args) {
       AES app = new AES();
       app.operation();
    }
}

Label - java





by - Chinmay Mahajan





Are you looking for something else?


Suggested Programs




Difference between ArrayList and LinkedList in Java with example


Dynamically Updating a List in JAVA


Upload and Preview Image using JFrame in Java


Connect Database and Retrieve values using JSP


Chat application using Socket programming in JAVA