JavaTutorial #12 :How to encrypt and descrypt txt file with AES in java programing

in #utopian-io6 years ago

Repository

https://github.com/dmlloyd/openjdk

What Will I Learn?

  • You will learn about AES
  • You will learn how to encrypt and descrypt file with AES
  • You will learn how to implement this cryptography in java programing

Requirements

-NetBeans IDE Software

  • Basic Knowledge about Java Programing and Netbeans

Difficulty

  • Basic

Tutorial Contents

The Advanced Encryption Standard (AES) is a standard symmetric key-encryption adopted by the United States government. It consists of 3 block ciphers, namely AES-128, AES-192 and AES-256, which are adopted from larger collections that were originally published as Rijndael. Each cipher has a 128-bit size, with a key size of 128, 192 and 256 bits, respectively. AES has been widely analyzed and is now used throughout the world, as is the case with its predecessor, Data Encryption Standard (DES).

Here we will try to implement the AES method to encrypt and decrypt txt files. You need to know that in the encryption process there are 2 components, Plaintext and Chipper. The plaintext is the original text before it is encrypted and after the description. While Chipper is text that has been encrypted and cannot be read anymore. This encryption and description process uses certain keywords called secretkey.

unnamed.jpg

Now, Let's try to implement it in Java Programing

Preparation
  • Create a new txt file that contain some text

  • Open Netbeans IDE then create new project

  • Choose java Aplication then click next

  • Set project name and location and click finish

Start Coding
  • Open java class from your project

  • Add this code throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException after (String[] args). This code used to handle some error that will be happen later in our program

  • Left Click on warning lamp simbol beside the code

  • Click all add import until the error simbol doesn't exist anymore

  • Load the txt file that you have created before.Put the code bellow in public main
    byte[] array = Files.readAllBytes(new File("D:\\before.txt").toPath());
    all texts in before.txt are converted to array

  • After loading the file, Now we encrypt it using aes method. To encrypt some text with aes we need a secretkey, like code bellow :

                byte [] secKey=new byte[]{'1','2','3','4','m','m','a','d','r','i','z','k','a','a','j','0'};
                SecretKeySpec aesKey=new SecretKeySpec(secKey,"AES");
                Cipher cipher= Cipher.getInstance("AES/ECB/PKCS5Padding");
                cipher.init(Cipher.ENCRYPT_MODE, aesKey);               
                byte[] encrypted = cipher.doFinal(array);

byte [] secKey : secret key
SecretKeySpec aesKey=new SecretKeySpec(secKey,"AES"); : combine secretkey with AES
Cipher cipher= Cipher.getInstance("AES/ECB/PKCS5Padding"); : creating a chipper
cipher.init(Cipher.ENCRYPT_MODE, aesKey); : encryption process
byte[] encrypted = cipher.doFinal(array); :accommodate data that has been encrypted into variable

  • If you get some error notificaton, left click and import all package that suggest by aplication

  • display chipper text in new txt file

OutputStream out=new FileOutputStream("D:\\chiper.txt");           
out.write(encrypted);

OutputStream out=new FileOutputStream("D:\\chiper.txt"); : create new txt file
out.write(encrypted); : write data tha has been encrypted in chiper file

  • Now descrypt the file, before descryption you need to read the chipper first.
byte[] cipherFile = Files.readAllBytes(new File("D:\\paste.pdf").toPath()); 
  • descrypt the chipper file.
cipher.init(Cipher.DECRYPT_MODE, aesKey);
  • Accommodate tha chipper file in a variable
byte[] dekripsi = cipher.doFinal(cipherFile);
  • create new txt file
OutputStream outputDekripsi=new FileOutputStream("D:\\dekriptedfile.txt");
  • write all data that has been encrypted in dekriptedfile.txt
outputDekripsi.write(dekripsi);
out.flush();
Running the project
  • save all file and try to run

  • now open the location of before.txt , you can see now there is new file. It's chipper.txt and descriptedfile.txt

  • Open chipper.txt and descriptedfile.txt

  • success

Curriculum

Proof of Work Done

https://gist.github.com/team2dev/07ca19fd8deef935842926f170fbef0f

Sort:  
Loading...

Hi @team2dev!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @team2dev!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.18
TRX 0.15
JST 0.029
BTC 62938.05
ETH 2552.06
USDT 1.00
SBD 2.63