Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
Aes encryption in javascript

i am using crypto-js for encryption a text with key and IV (new byte[16]) for some reason it's giving different output from what it gives online or from my python code

my python code:

import base64
import binascii
import hashlib

from Crypto.Cipher import AES


def r_pad(payload, block_size=16):
    length = block_size - (len(payload) % block_size)
    return payload + chr(length) * length


key = "my-private-key"
body = "my-text-to-encrypt"
iv = binascii.unhexlify(16 * "00")
length = len(body)
encoded_key = key.encode('ascii')
data_from_encryption = r_pad(body).encode('utf-8')
encrypted_data = AES.new(encoded_key, AES.MODE_CBC, iv).encrypt(data_from_encryption)
encrypted_data_to_base64_str = base64.b64encode(encrypted_data).decode('utf-8')
print("Encrypted data: ", encrypted_data_to_base64_str)

my javascript code

import { Buffer } from "buffer";
import CryptoJS from "crypto-js";

const IV = Buffer.alloc(16, 0, "hex");
const key = "my-private-key";
const text = "my-text-to-encrypt"
const ciphertext = CryptoJS.AES.encrypt(text, key, {
    iv: IV,
    mode: CryptoJS.mode.CBC,
}).toString();

console.log(ciphertext)

i don't know what i am missing here which is causing problem. if i try to decrypt my encrypted javascript's code cyptherText from the code is used in python to decrypt it shows error

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7 in position 1: invalid continuation byte

and there is a lot of difference in the output of the both the encryption code:

python outputs: VfCJUymqvQWJWm9Vl93A6Q==

javascript outputs: U2FsdGVkX1+/owRNxuxz16Lq7OeNxYmeBiDQZDHHEAQ=

can anybody help me to fix my javascript code?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!