Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4271054
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:23:23+00:00 2026-05-21T07:23:23+00:00

This is how I have created my AES cipher , but I still keep

  • 0

This is how I have created my AES cipher , but I still keep getting this error of BadPaddingException in doFinal() block of decryption

// Get the key generator            
   KeyGenerator kg = KeyGenerator.getInstance("AES");
   kg.init(128);
   SecretKey sk = kg.generateKey();
   byte[] iv = new byte[]{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
   AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);

   ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
   dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

   ecipher.init(Cipher.ENCRYPT_MODE, sk,paramSpec);
   dcipher.init(Cipher.DECRYPT_MODE, sk,paramSpec);

Decrypt method code

public String decr(String str) {
        try {
            byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
            byte[] utf8 = dcipher.doFinal(dec);
            return new String(utf8, "UTF8");
        } catch (Exception ex) {
            return null;
        }
    }

Encrypt Method

public String encr(String str) {
        try {
            byte[] utf8 = str.getBytes("UTF8");
            byte[] enc = ecipher.doFinal(utf8);
            return new sun.misc.BASE64Encoder().encode(enc);
        } catch (Exception ex) {
            return null;
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T07:23:24+00:00Added an answer on May 21, 2026 at 7:23 am

    Your code works quite well for me:

    package com.sandbox;
    
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.security.spec.AlgorithmParameterSpec;
    
    import javax.crypto.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.KeyGenerator;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.IvParameterSpec;
    
    public class EncryptionTest
    {
        private final Cipher ecipher;
        private final Cipher dcipher;
    
        public EncryptionTest() throws Exception
        {
            KeyGenerator kg = KeyGenerator.getInstance("AES");
            kg.init(128);
            SecretKey sk = kg.generateKey();
            byte[] iv = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
            AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
    
            ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    
            ecipher.init(Cipher.ENCRYPT_MODE, sk, paramSpec);
            dcipher.init(Cipher.DECRYPT_MODE, sk, paramSpec);
        }
    
        public String encrypt(String str) throws IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException
        {
            byte[] utf8 = str.getBytes("UTF8");
            byte[] enc = ecipher.doFinal(utf8);
            return new sun.misc.BASE64Encoder().encode(enc);
        }
    
        public String decrypt(String str) throws IOException, IllegalBlockSizeException, BadPaddingException
        {
            byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
            byte[] utf8 = dcipher.doFinal(dec);
            return new String(utf8, "UTF8");
        }
    
        public static void main(String[] args)
        {
            try
            {
                EncryptionTest sandbox = new EncryptionTest();
                System.out.println(sandbox.decrypt(sandbox.encrypt("Hello world")));
            }
            catch (Exception e)
            {
              e.printStackTrace();
            }
        }
    }
    

    This prints out Hello World

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My application has multiple SQLite tables(around 20 with different fields). This I have created
I have created this simple program to learn shared_ptr using namespace std; #define Yes
I have created this report using BIRT and phpjavabridge <?php header(Content-type: application/pdf); header(Content-Disposition: inline;
I have created this Fiddle with this code: This is my problem I want
I have created this function to check abecedarian with while loop (A word is
i have created a java project in eclipse,in this project i have an html
To explain my question I have created this http://jsfiddle.net/Jams/XNVB2/ here I want when div1
I have followed this guide: http://maven.apache.org/guides/plugin/guide-java-plugin-development.html I have created a maven-plugin project hello-maven-plugin with
I have created my database by this sql query: (CREATE DATABASE + DBName +
I have created an xml file like this: <?xml version=1.0 encoding=utf-8?> <ListView xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=match_parent

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.