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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:26:54+00:00 2026-06-17T06:26:54+00:00

I wrote two functions as follows which encrypt and decrypt the data. public static

  • 0

I wrote two functions as follows which encrypt and decrypt the data.

public static void encrypt() throws Exception {
    // Add the BouncyCastle Provider
    //Security.addProvider(new BouncyCastleProvider());


// Generate the key
byte[] keyBytes = "AAAAAAAAAAAAAAAA".getBytes();
SecretKeySpec   key = new SecretKeySpec(keyBytes, "AES");

// Generate the IV
byte[] ivBytes  = "AAAAAAAAAAAAAAAA".getBytes();
IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);

// Create the cipher object and initialize it
Cipher          cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);

// Read all bytes from a file into a bytes array
byte[] inputBytes = GCM.readFile("input");
byte[] cipherBytes = cipher.doFinal(inputBytes);

BufferedOutputStream  outputStream = new BufferedOutputStream(new FileOutputStream("output.enc"));
outputStream.write(cipherBytes);

outputStream.close();   
}

public static void decrypt() throws Exception {
    // Add the BouncyCastle Provider
     //Security.addProvider(new BouncyCastleProvider());

 // Generate the key
 byte[] keyBytes = "AAAAAAAAAAAAAAAA".getBytes();
 SecretKeySpec   key = new SecretKeySpec(keyBytes, "AES");

 // Generate the IV
 byte[] ivBytes  = "AAAAAAAAAAAAAAAA".getBytes();
 IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);

 // Create the cipher object and initialize it
 Cipher          cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
 cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);

 // Read all bytes from a file into a bytes array
 byte[] cipherBytes = GCM.readFile("ouput.enc");
 byte[] decBytes = cipher.doFinal(cipherBytes);

 BufferedOutputStream  outputStream = new BufferedOutputStream(new FileOutputStream("regen.plain"));
 outputStream.write(decBytes);
 outputStream.close();   
}

I realize that the code has the key set as "AAAAAAAAAAAAAAAA".getBytes() ; however please bear with me as this is just an example.

When I run the program I get the following stack trace :-

Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
    at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
    at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
    at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..)
    at javax.crypto.Cipher.doFinal(DashoA13*..)
    at GCM.decrypt(GCM.java:80)
    at GCM.main(GCM.java:90)

I’m having trouble figuring out why I’m encountering this error. Any hints on how I could solve the issue?

[EDIT]
It seems that when I write out data there are 16 bytes in all but only 15 bytes when I read it back in.

  • 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-17T06:26:55+00:00Added an answer on June 17, 2026 at 6:26 am

    On your update: Well, that’s easy then, fix the part that reads the file because the ciphertext needs to be N * blocksize, thus 16 bytes. I don’t see any other blatant errors.

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

Sidebar

Related Questions

I am a beginner in JavaScript but I wrote two functions that is meant
In the following snippet, you can see my two collatz functions I wrote in
I have a cakePHP application that is pulling data from two different databases, which
I just wrote a stored function to calculate the working days between two dates.
guys, I wrote a function to test if two inputs (a and b) have
I write two function, which callback will be invoke by the fn : var
I wrote two small applications (a client and a server) to test UDP communication
I wrote two code both are same Using NSURLConnection and Using AFNetworking. My NSURLConnection
In a written test I came across a question which reads as follows: We
My problem is as it follows: I have a piece of JavaScript code which

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.