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

  • Home
  • SEARCH
  • 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 6686611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:11:30+00:00 2026-05-26T05:11:30+00:00

Possible Duplicate: Java AES Encrypt Entire String Im having problems with the conversions back

  • 0

Possible Duplicate:
Java AES Encrypt Entire String

Im having problems with the conversions back and forth between strings and byte arrays.

Basically I’ve made a small program to encrypt and decrypt messages using AES.

After encrypting the message this happens:

        byte[] result = cipher.doFinal(message.getBytes());
    String stringResult = new String(result);

Which converts the encrypted message to a string.
Now my decryptor changes the string back to a byte using:

byte[] result = stringResult.getBytes();

but when it decrypts the message (depending on the message) it may not be able to. There appears to be a padding problem and the error that I get is:

Exception in thread “main” javax.crypto.BadPaddingException: Given final block not properly padded

Any ideas why this occurs?

One example when this occurs for sure is when the encryption key is “1231231231231231” and the message encrypted is “read”.

  • 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-26T05:11:31+00:00Added an answer on May 26, 2026 at 5:11 am

    You’re using the platform default encoding – once to convert message to bytes, and once to then convert the arbitrary binary output of encryption back into a string. Both steps are problematic.

    Firstly, it’s best to use a fixed encoding which is known to cover the whole of Unicode when converting the string to bytes. UTF-8 is usually a good bet.

    Then there’s the matter of representing arbitrary binary data as text. This isn’t text data represented in an encoding – it’s binary data. Interpreting it as if it were text data will almost certainly lose information. You need something more robust, capable of round-tripping arbitrary binary data. Base64 is usually the way to go.

    There’s a public domain base64 encoder which works pretty well as far as I know. So your encryption code becomes:

    byte[] result = cipher.doFinal(message.getBytes("UTF-8"));
    String stringResult = Base64.encodeBytes(result);
    

    The decrypting code would then be:

    byte[] encrypted = Base64.decode(encryptedBase64);
    byte[] decrypted = /* do the decryption here */
    String message = new String(decrypted, "UTF-8");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Java AES Encrypt Entire String I've run into a small problem. For
Possible Duplicate: Java: convert List<String> to a join()d string Having this: List<String> elementNames =
Possible Duplicate: java String concatenation How to Improve performance of this chunk of code
Possible Duplicate: Java Generics To be more specific, whats the role of the <String>
Possible Duplicate: Java: What is the difference between implementing Comparable and Comparator? Difference between
Possible Duplicate: Java string comparison? I have encounter the following problem, I have an
Possible Duplicate: Java - escape string to prevent SQL injection Hi. I constantly am
Possible Duplicate: Java - escape string to prevent SQL injection I know php has
Possible Duplicate: Java : switching between dll depends on system architecture (32/64) In my
Possible Duplicate: Java: Parse a mathematical expression given as a string and return a

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.