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.

Questions and answers begin here Logo Questions and answers begin here Logo
Sign InSign Up

Questions and answers begin here

Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Blog
  • Contact Us

Given final block not properly padded.

Home/ Questions/Q 365
Next
Answered
Given final block not properly padded.
oldsidefalcon
oldsidefalcon Teacher

I’m a college student majoring in computer science and I have been working on a project that involves encryption and decryption of data. I’ve written a program to decrypt a file encrypted with AES in CBC mode, but I keep getting an error message saying “Given final block not properly padded”.
Here is the relevant part of my code:

try {
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
IvParameterSpec ivSpec = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
byte[] decrypted = cipher.doFinal(encrypted);
return new String(decrypted);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
return null;
}

I looked up the error message online and found some explanations, but I still don’t understand what’s going wrong with my code. Can someone please help me?

decryptionencryptionjavapaddingprogramming
  • 359
  • 0 Followers
  • 1
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. maky_barua Teacher
    2018-05-21T21:50:26+00:00Added an answer about 5 years ago

    One possible solution to the issue you are facing is to check whether your encrypted data is padded correctly or not. The error message you are receiving is complaining about the padding being incorrect. One reason for this is that the length of the input data might not be a multiple of the block size. The encryption algorithm you are using might need the input to be padded with an extra block to make the length a multiple of the block size. You may want to explore some of the built-in padding schemes supported by your encryption algorithm in order to solve this problem.

    Another possible solution is to check whether you are initializing the encryption algorithm properly. The block cipher modes of operation require initialization vectors (IVs) to be set properly before encryption or decryption. An incorrect IV might cause the “given final block not properly padded” error. Make sure that you are setting the IV properly before encrypting or decrypting data.

    These are just some possible solutions to your problem. To better understand the problem and find a solution it is important that you provide more details on your implementation, encryption algorithm, and the data you are working with. Detailed error messages and stack traces would also be helpful in diagnosing the problem.

    • 100
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Best Answer
    renan_felix_azevedo Begginer
    2018-05-07T09:43:46+00:00Added an answer about 5 years ago

    Hi there,
    I understand that you’re experiencing issues with the error message “given final block not properly padded” while coding. This error usually occurs when you’re decrypting data that has not been properly padded, or when there is a mismatch in the padding between the sender and the receiver of the data.
    There are a few things you can try to fix this error:
    1. Ensure that the data you’re decrypting has been properly padded. Padding is essential when encrypting and decrypting data because it ensures that the data is of a consistent length. However, if the data has not been properly padded, this can cause issues with the decryption process. You should ensure that both the sender and receiver of the data are using the same padding method.
    2. Verify that you’re using the correct encryption and decryption algorithms. Different encryption algorithms use different padding methods, so it’s important to ensure that you’re using the correct algorithms. If you’re not sure which encryption algorithm to use, do some research on the subject or seek advice from an expert in the field.
    3. Check that you’re using the correct key to decrypt the data. If you’re using an incorrect or outdated key, this can cause issues with the decryption process. Ensure that the encryption key you’re using is current and correct.

    4. Ensure that there are no issues with the network or hardware. A faulty network connection or hardware issue can also cause issues with the decryption process, so it’s essential to ensure that all hardware and network components are functioning properly.

    I hope these tips help you fix the “given final block not properly padded” error that you’re experiencing. However, if you’re still having trouble, don’t hesitate to seek advice from an expert or a professional in the field. Good luck!

    • 11
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

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.