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 7910583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:54:53+00:00 2026-06-03T12:54:53+00:00

I am using AES encryption and decryption using java. And I use Appache commons

  • 0

I am using AES encryption and decryption using java. And I use Appache commons library for conversion from string to byte and vice versa. But when I decrypt data then it is different from the input data that was encrypted using same key? why is so

Here is my code:

public static void main(String[] args) throws Exception {



        String key="this is key";

        String message="This is just an example";

           KeyGenerator kgen = KeyGenerator.getInstance("AES");

           kgen.init(128, new SecureRandom(Base64.decodeBase64(key)));

           // Generate the secret key specs.
           SecretKey skey = kgen.generateKey();

           byte[] raw = skey.getEncoded();

           SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

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

           cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

           byte[] encrypted= cipher.doFinal(Base64.decodeBase64(message));

           String encryptedString=Base64.encodeBase64String(encrypted);

           cipher.init(Cipher.DECRYPT_MODE, skeySpec);

           byte[] original =
             cipher.doFinal(Base64.decodeBase64(encryptedString));

           System.out.println(Base64.encodeBase64String(original));


    }

I get the output “Thisisjustanexamplc=” where it should have been “This is just an example”. what I need to change in my code. Thanks in advance

  • 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-06-03T12:54:56+00:00Added an answer on June 3, 2026 at 12:54 pm

    You are base-64–decoding your plain text message. You should use message.getBytes(StandardCharsets.UTF_8) (or some other encoding) to convert to bytes instead. Base-64 encode the result of the encryption operation, then base-64 decode it before decrypting. Use new String(original, StandardCharsets.UTF_8) to convert the result of the decryption operation back to text.

    In other words, use a character encoding to convert between text and bytes. Use base-64 encoding and decoding to encode binary data in a text form.

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

Sidebar

Related Questions

I'm implementing encryption / decryption using Java Cipher and AES. Everything is working well
I'm doing password based file encryption in Java; I'm using AES as the underlying
I want to do AES CBC encryption in Java. I'm using javax.crypto . After
i have a java program . this is a AES encryption - decryption program.
I am using AES algorithm for encryption and decryption of password when the PasswordFormat
I Currently perform a large amount of encryption/decryption of text in c# using AES.
I am using encryption in java and performing decryption in php. i am using
I am doing encryption and decryption using AES Algorithm with bouncy castle My encryption
can you please any one explain AES/CBC/PKCS5Padding encryption and decryption using bounce castle i
I'm trying to encrypt/decrypt a string using 128 bit AES encryption (ECB). What I

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.