AES Encryption and Decryption with Java

Yogesh D

Here is what I am doing which can look a bit clumsy but any help is appreciated regarding the problem. I'm getting a BadPaddingException. Read almost all related topics but didn't find the appropriate solution. I am new to encryption decryption programming and need to implement it in one of my Java application.

Thank You.. this is how the code looks....

public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    // TODO Auto-generated method stub
            String FileName="encryptedtext.txt";
            String FileName2="decryptedtext.txt";
            String pad="0"; 

            KeyGenerator KeyGen=KeyGenerator.getInstance("AES");
            KeyGen.init(128);

            SecretKey SecKey=KeyGen.generateKey();

            Cipher AesCipher=Cipher.getInstance("AES");
            AesCipher.init(Cipher.ENCRYPT_MODE,SecKey);

            byte[] byteText="My name is yogesh".getBytes();
            byte[] byteCipherText=AesCipher.doFinal(byteText);
            String cipherText = null;

            try {
                FileWriter fw=new FileWriter(FileName);
                BufferedWriter bw=new BufferedWriter(fw);
                bw.write(byteCipherText.toString());
                bw.close();
            }catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                FileReader fr=new FileReader(FileName);
                BufferedReader br=new BufferedReader(fr);
                cipherText=br.readLine();
                br.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            AesCipher.init(Cipher.DECRYPT_MODE,SecKey);
            while(((cipherText.getBytes().length)%16)!=0)
            {
                cipherText=cipherText+pad;


            }

            byte[] bytePlainText=AesCipher.doFinal(cipherText.getBytes());
            FileWriter fw1;
            try {
                fw1 = new FileWriter(FileName2);
                BufferedWriter bw1=new BufferedWriter(fw1);
                bw1.write(bytePlainText.toString());
                bw1.close();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }






}
bgamlath

Here, what you have to understand is that cipher text may contain non-printable characters. So, when you use readLine(), it will likely not give you all of the bytes in the file.

Also, byteCipherText.toString() does not give you what you thought you would get. In java, the toString() method does not give the string representation of the contents of the array.

There is no need to add padding to encrypted text. It is already padded.

import java.nio.file.Files;
import java.nio.file.Paths;
import javax.crypto.*;

public class Main {

    public static void main(String[] args) throws Exception {
        String fileName = "encryptedtext.txt";
        String fileName2 = "decryptedtext.txt";

        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(128);

        SecretKey secKey = keyGen.generateKey();

        Cipher aesCipher = Cipher.getInstance("AES");


        byte[] byteText = "Your Plain Text Here".getBytes();

        aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
        byte[] byteCipherText = aesCipher.doFinal(byteText);
        Files.write(Paths.get(fileName), byteCipherText);


        byte[] cipherText = Files.readAllBytes(Paths.get(fileName));

        aesCipher.init(Cipher.DECRYPT_MODE, secKey);
        byte[] bytePlainText = aesCipher.doFinal(cipherText);
        Files.write(Paths.get(fileName2), bytePlainText);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AES javascript encryption and Java decryption

From Dev

Java AES encryption and decryption with static secret

From Dev

AES 128 encryption in Java Decryption in PHP

From Dev

AES encryption using Java and decryption using Javascript

From Dev

Java simple helloworld AES encryption decryption

From Dev

AES Encryption/Decryption in Node JS similar to Java

From Dev

encryption using AES in android, and decryption in Java

From Dev

AES CTR encryption and decryption

From Dev

Slow AES GCM encryption and decryption with Java 8u20

From Dev

AES-256 Password Based Encryption/Decryption in Java

From Dev

Java AES encryption/decryption procedure and usage of Initialization Vector

From Dev

react-native AES Encryption matching Java Decryption algorithm

From Dev

AES-256-CTR Encryption in node JS and decryption in Java

From Dev

Java AES encryption/decryption always return the same content

From Dev

.NET AES Encryption and Android Decryption

From Dev

AES encryption/decryption iOs and .Net

From Dev

AES Encryption Decryption Algorithm in Android

From Dev

AES Encryption / decryption for hindi character

From Dev

AES encryption with CryptoJS and decryption with CodeIgniter

From Dev

AES Encryption - Encryption/Decryption Adding Extra Bytes

From Dev

Java encryption/decryption to Ruby

From Dev

AES 256 Encryption/Decryption without IV

From Dev

PHP equivalent to Rijndael AES encryption/decryption in .Net?

From Dev

AES Encryption in C# and decryption in CryptoJS

From Dev

AES encryption/decryption in javascript using CryptoJS

From Dev

Simple AES encryption decryption with openssl library in C

From Dev

C# Calculating Percentage of AES Encryption/Decryption

From Dev

AES encryption/decryption from Android to server

From Dev

AES encryption/decryption in javascript using CryptoJS

Related Related

  1. 1

    AES javascript encryption and Java decryption

  2. 2

    Java AES encryption and decryption with static secret

  3. 3

    AES 128 encryption in Java Decryption in PHP

  4. 4

    AES encryption using Java and decryption using Javascript

  5. 5

    Java simple helloworld AES encryption decryption

  6. 6

    AES Encryption/Decryption in Node JS similar to Java

  7. 7

    encryption using AES in android, and decryption in Java

  8. 8

    AES CTR encryption and decryption

  9. 9

    Slow AES GCM encryption and decryption with Java 8u20

  10. 10

    AES-256 Password Based Encryption/Decryption in Java

  11. 11

    Java AES encryption/decryption procedure and usage of Initialization Vector

  12. 12

    react-native AES Encryption matching Java Decryption algorithm

  13. 13

    AES-256-CTR Encryption in node JS and decryption in Java

  14. 14

    Java AES encryption/decryption always return the same content

  15. 15

    .NET AES Encryption and Android Decryption

  16. 16

    AES encryption/decryption iOs and .Net

  17. 17

    AES Encryption Decryption Algorithm in Android

  18. 18

    AES Encryption / decryption for hindi character

  19. 19

    AES encryption with CryptoJS and decryption with CodeIgniter

  20. 20

    AES Encryption - Encryption/Decryption Adding Extra Bytes

  21. 21

    Java encryption/decryption to Ruby

  22. 22

    AES 256 Encryption/Decryption without IV

  23. 23

    PHP equivalent to Rijndael AES encryption/decryption in .Net?

  24. 24

    AES Encryption in C# and decryption in CryptoJS

  25. 25

    AES encryption/decryption in javascript using CryptoJS

  26. 26

    Simple AES encryption decryption with openssl library in C

  27. 27

    C# Calculating Percentage of AES Encryption/Decryption

  28. 28

    AES encryption/decryption from Android to server

  29. 29

    AES encryption/decryption in javascript using CryptoJS

HotTag

Archive