Issues encountered with AES decryption when utilizing CryptoJS

Currently, I am facing an issue with encrypting and decrypting data using AES encryption. In my code, I am encrypting the data in javascript using crypto.js and trying to decrypt it in Java.

Encryption and decryption work correctly when done in either javascript or java individually. However, when I attempt to encrypt in javascript and then decrypt in java, I encounter the following error:

javax.crypto.BadPaddingException: Given final block not properly padded at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:966)

Here is a snippet of my JSCode for encryption:

var keyHex = CryptoJS.enc.Utf8.parse('584771624934175587013168');
var iv    = CryptoJS.enc.Hex.parse('000000000000000000000000');
var encrypted = CryptoJS.AES.encrypt('1111', keyHex, {
    iv:iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
});
console.log('encryptByAES key: ',encrypted.toString());

And this is a snippet of my java code for decryption:

String key = "584771624934175587013168";
String plainText = "1111";

// Methods for encryption and decryption using AES

How can I resolve this decryption issue? Any assistance would be greatly appreciated!

Answer №1

When working with block encryption like AES, it is crucial to pad your input before encrypting it. There are several standard methods for accomplishing this.

In the JavaScript code, PKCS-7 padding is specified:

    padding: CryptoJS.pad.Pkcs7

However, in the Java code, PKCS-5 padding is being used:

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

It's essential to ensure that the same padding algorithm is utilized on both ends.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a spinning or swinging motion using scriptaculous shake

I want to create a unique hover effect where the item doesn't shake from side to side, but instead smoothly slides like a clock pendulum moving from 180-90 degrees. I'm not certain about the exact numbers right now. Is this achievable with Script ...

What is the best way to trigger events upward in a Backbone View hierarchy?

Running a backbone app with a structured view system, here's a simplified version of how it looks: NewsListView = Backbone.View.extend({ el: $('li#newspane'), initialize: function() { _.bindAll(this); }, render: f ...

Showing a notification that is persistent and not dismissible

Hello everyone! I've been struggling with a problem on the RPS project for quite some time now. The issue arises when I try to display a message for a tie in the game, but I can't seem to find a solution to make it disappear when the round is not ...

The selected value from a dropdown list may occasionally come back as text

I am facing an issue with a dropdown list on my form that has Integer Values set to display text. The problem arises when I run the code to show the value and associated text, as the text is being displayed as the value itself. Is there any workaround avai ...

Transferring files and information using the Fetch API

I am currently working on a React application and I have defined the state of my application as shown below: const [book, setBook] = useState({ title: '', cover: {} numberPages: 0, resume: '', date: date, }); The & ...

Execute a PHP function using JavaScript/jQuery with AJAX requests

Hello everyone, I am reaching out for assistance with AJAX as I am still quite new to it. Despite the abundance of examples available, I find it challenging to grasp the concept. Specifically, I need help with my validation of a contact form using JavaScri ...

Compress PDF documents directly from a given web address

I'm currently attempting to create a zip file containing multiple PDF files by using the archiver npm module. While I have successfully managed to zip files from local memory to my machine, I am facing difficulties when trying to use URLs in the fs.cr ...

Vue.js - Error: Module not found: Cannot locate module 'lottie-vuejs'

I've been attempting to integrate lottie-vuejs into my project. After running the npm install command and following the steps outlined here, I encountered an issue. Unfortunately, I received the following error message: Module not found: Error: Can ...

Creating a JSON object with dynamic values and multiple nested arrays can be achieved by following specific steps

Seeking a way to generate dynamic JSON by utilizing values retrieved from data-attributes. Interested in incorporating a multi-level array within the object as well. For instance: HTML: <div data-color="red" data-type="honda"> </div> JavaSc ...

Error: HTML code being displayed instead of form value in Ajax request output

For the past couple of days, I've been struggling with an Ajax issue that I just can't seem to figure out. Despite looking for a solution online, nothing seems to work. Below is the code snippet that's causing me trouble: <!DOCTYPE html& ...

Using Jquery to hide or show objects within a <div> when clicked

My goal is to create a webpage that displays three different contents based on which button is clicked. Below is the code for reference: I want the page to show three specific sections: A search bar only when the 'search' button is clicked, T ...

What is the best way to extract comma-separated values, disregarding any commas within square brackets?

The CSV Values in the sample file are separated as shown below: sample.csv 1,2,[3,4],5 To read the CSV file, use the following CSV Reader code snippet: final CsvToBean<Numbers> csvReader = new CsvToBeanBuilder(fileReader) .withType ...

Scroll the second div to align with the first div

I am in need of a scrolling set of divs that functions like a slide show. I have been searching for a solution but haven't found one yet. Is there a way to make the commented-out div scrollable by clicking somewhere on the page or an arrow image? I wa ...

The @JsonProperty annotation does not recognize case sensitivity when converting provided title case to lowercase

1 Parent Data Transfer Object (DTO) @JsonSerialize(include=JsonSerialize.Inclusion.NON_EMPTY) public class OfferResponse { @JsonProperty("Id") private String id; @JsonProperty("Status") private String status; @JsonProperty("Name") ...

Ensuring the Validity of a Text Using JFace Dialogs

Exploring a dialogue box I constructed with dual input fields using the Code below. public class CCIDDialog extends TitleAreaDialog { private Text ccidText; private Text descriptionText; private String CCID; private String description; public CCI ...

Error occurs when pressing a button causes the application to crash while trying to invalidate the canvas view in Android Studio using Java

I'm experiencing some issues with my app crashing after clicking on a button that calls invalidate() to refresh the view. Deleting canvasView.invalidate() prevents the app from crashing. Is it permissible to call invalidate() from another class? I&apo ...

Polymer event / callback for appending new child nodes

My current project involves creating a custom element, let's call it <parent-element>, that performs specific actions based on the presence of its childNodes. When I define the element like this: <parent-element> <div> </div&g ...

What are the steps for launching a node.js application within vert.x?

I am completely new to vert.x and I am currently exploring the possibility of migrating an existing nodejs application to vert.x. At this point, I have followed the steps outlined in to install vert.x using npm. While I was able to run a simple hello-worl ...

Executing RegisterStartupScript repeatedly in C#

Greetings to all, Here is a snippet from my code: start code protected void Button1_Click(object sender, EventArgs e) { ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "temp", "<script>loadAdditionalInfoDialog(info1)< ...

"We encountered an error with the external script while trying to load file content using jQuery's ajax function. It

//C.php <script type="text/javascript"> $(document).ready(function(e) { $('div').load("D.php"); }); </script> <div></div> //D.php <script type="text/javascript" src="D.js"></script> //D.js console.log(45 ...