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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:53:14+00:00 2026-05-27T21:53:14+00:00

I am trying to write a simple app in order to encrypt, and decrypt

  • 0

I am trying to write a simple app in order to encrypt, and decrypt a chunk of text, using JCA and JCE from Java.

So far the encryption part works, but it gives me the following exception when decrypting :

javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher

This is the part where I initialize the two Ciphers, encCipher, and decCipher.

PBEKeySpec pbeKeySpec;
PBEParameterSpec paramSpec;
SecretKeyFactory keyFac;
byte[] salt = {(byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
               (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99};
int count = 20;
paramSpec = new PBEParameterSpec(salt, count);

try {
    pbeKeySpec = new PBEKeySpec("my_password".toCharArray(), salt, count);
    SecretKey secretKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(pbeKeySpec);

    encCipher = Cipher.getInstance(secretKey.getAlgorithm());
    encCipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
    decCipher = Cipher.getInstance(secretKey.getAlgorithm());
    decCipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);

} catch (Exception ex) {
    ex.printStackTrace();
 }

I don’t have experience with using the Java Cryptography Architecture, I would like to know how could I fix the error.

The exception happens at the line with decCipher.doFinal from loadClients.

private void saveClients() {
   String plainText = "";
   String encText = "";
   Set<String> clients = my_clients.keySet();
        try {
            PrintWriter out = new PrintWriter(new File(output_file));
            for (String client : clients) {
                long client_time = my_clients.get(client);
                plainText = client + " " + client_time;

                encText = new String(encCipher.doFinal(plainText.getBytes()));
                out.println(encText);
            }
            out.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void loadClients() {
        BufferedReader in;
        String line;
        try {
            in = new BufferedReader(new FileReader(output_file));
            while ((line = in.readLine()) != null) {
                byte[] decBytes = decCipher.doFinal(line.getBytes());
                String decText = new String(decBytes);
                String[] client_data = decText.split("[ ]");
                my_clients.put(client_data[0], Long.parseLong(client_data[1]));
            }
            in.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
     }
  • 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-27T21:53:14+00:00Added an answer on May 27, 2026 at 9:53 pm

    The problem comes from the fact that you transform bytes into Strings. Suppose that your default platform encoding is ASCII. This means that half of the bytes contained in your encrypted text (bytes 128 and above) don’t represent valid characters. Moreover, you write one line for each encrypted text. So, if your encrypted byte array happens to contain a newline char, you will in fact write two lines to the writer, and try to decrypt each line one by one, which will lead to an exception.

    Either use bytes to store and transfer your encryted data, or transform them into Strings using a non-lossy algorithm, like Base64 (which will make sure everything will be transformed into printable characters). Apache commons-codec has a Base64 implementation.

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

Sidebar

Related Questions

I'm trying to write a silly little app using Java and AWT. It simply
i'm trying to write a simple app with node.js to pull some json from
I'm new to cakephp and trying to write a simple app with it, however
I'm trying to write a simple WPF app that has two ellipses, joined by
I'm trying to write a simple AppleScript to do some text manipulation on the
I am trying to write an app in python to control a motor using
I'm trying to write a simple app that lets a user grant my code
I'm trying to write a simple app, and follow the ApiDemos sample. My app
I am currently trying to write a very simple app that sends an object
I'm trying to write a really simple GUI app for inserting some records into

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.