https://github.com/dmlloyd/openjdk
-NetBeans IDE Software
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.
Now, Let's try to implement it in Java Programing
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
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
byte[] cipherFile = Files.readAllBytes(new File("D:\\paste.pdf").toPath());
cipher.init(Cipher.DECRYPT_MODE, aesKey);
byte[] dekripsi = cipher.doFinal(cipherFile);
OutputStream outputDekripsi=new FileOutputStream("D:\\dekriptedfile.txt");
dekriptedfile.txtoutputDekripsi.write(dekripsi);
out.flush();
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
https://gist.github.com/team2dev/07ca19fd8deef935842926f170fbef0f