Crypto JS code output does not match C# code output


This Content is from Stack Overflow. Question asked by Ray

I have some C# code that uses AES 256 to encrypt data, using a custom key and iv. I’m trying to recreate the code using CryptoJS library. They give different outputs, and I can’t figure out what I’m doing wrong.

The input for both is string ‘675675’.

C# code gives yzFi7xGovxl0qcLsMoXZ9Q== while JS code gives zJtuaFJcJdeSfZxneb6bTYDPjoH3QAz87HTaaLyn5Zc=

Here’s the C# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace Whatever.Class
    public class Program 
        public static void Main(string[] args)
        string moo = Encrypt_AES256.Encrypt256("675675", "5TGB&BJD!(JM(IK<5TGB&YHN7UJM(IK<", "!QAYDF7N#EDC4RFV");
    public class Encrypt_AES256
        public static string Encrypt256(string text, string AesKey256, string AesIV256)
            // AesCryptoServiceProvider
            AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
            aes.BlockSize = 128;
            aes.KeySize = 256;
            aes.IV = Encoding.UTF8.GetBytes(AesIV256);
            aes.Key = Encoding.UTF8.GetBytes(AesKey256);
            aes.Mode = CipherMode.CBC;
            aes.Padding = PaddingMode.PKCS7;

            byte[] src = Encoding.Unicode.GetBytes(text);

            using (ICryptoTransform encrypt = aes.CreateEncryptor())
                byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length);

                // Convert byte array to Base64 strings
                return Convert.ToBase64String(dest);

Here’s the JS code I attempted to write, using CryptoJS lib:

const encryptWithAES = message => {
    const key = CryptoJS.lib.WordArray.create('5TGB&BJD!(JM(IK<5TGB&YHN7UJM(IK<');
    const iv = CryptoJS.lib.WordArray.create('!QAYDF7N#EDC4RFV');
    const wordArray = CryptoJS.lib.WordArray.create(message);

    const cipher = CryptoJS.AES.encrypt(wordArray, key, {
      padding: CryptoJS.pad.Pkcs7,
      mode: CryptoJS.mode.CBC,

    return  cipher.toString();


What am I doing wrong in the JS code?


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?