I need help on JavaScript version of the given C# function

102 Views Asked by At

you can reach the C# version of the code here : http://tpcg.io/_XBMLYD you can also test it with given plaintext and the key. Key length is 44

Any help will be appreciated in order to convert it to JavaScript.

Here is my JavaScript code by using Cryptojs but not produce the same result.

Regards, Nuri

var text = 'plainText';
var key = "GSTEGSTEjdfheyhdHSHSHSHDHHDHmdjjdn12ndndn5r=";

key = CryptoJS.enc.Utf16LE.parse(key);
//key = CryptoJS.dec.Base64.parse(key);

key = CryptoJS.MD5(key)
key.words.push(key.words[0], key.words[1]);

//var options = {mode: CryptoJS.mode.ECB};//CBC
var options = {mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7};

var textWordArray = CryptoJS.enc.Utf16LE.parse(text);

var encrypted = CryptoJS.TripleDES.encrypt(textWordArray, key,options);
var base64String = encrypted.toString()

console.Log(base64String);

C# function

using System.IO;
using System;
using System.Security.Cryptography;
using System.Text;

class Program
{
    static void Main()
    {
        string sonuc = OpenSSLEncryptApi("plainText","GSTEGSTEjdfheyhdHSHSHSHDHHDHmdjjdn12ndndn5r=");
        Console.WriteLine(sonuc);
    }   
    public static string OpenSSLEncryptApi(string plainText, string apikey)
    {
        TripleDESCryptoServiceProvider keys = new TripleDESCryptoServiceProvider();
        keys.GenerateIV();
        keys.GenerateKey();
        string key = apikey;
        byte[] iv = new byte[16];
        byte[] array;
        using (Aes aes = Aes.Create())
        {
            aes.Key = Convert.FromBase64String(key);
            aes.IV = iv;
            ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter streamWriter = new StreamWriter((Stream)cryptoStream))
                    {
                        streamWriter.Write(plainText);
                    }
                    array = memoryStream.ToArray();
                }
            }
        }
        return Convert.ToBase64String(array);
    }
}

Edit - Code changes according to the comments:

function aesEncrypt (data) {
   const key = 'GSTEGSTEjdfheyhdHSHSHSHDHHDHmdjjdn12ndndn5r=';
   const iv = '0000000000000000';
   
   const cipher = CryptoJS.AES.encrypt(data, CryptoJS.enc.Utf8.parse(key), {
       iv: CryptoJS.enc.Utf8.parse(iv), // parse the IV 
       padding: CryptoJS.pad.Pkcs7,
       mode: CryptoJS.mode.CBC
   })
   
   // e.g. B6AeMHPHkEe7/KHsZ6TW/Q==
   return cipher.toString()
}
1

There are 1 best solutions below

0
Nuri Erginer On

With the great help of @Topaco We get the answer. Please find below the JS version of the function that outputs the same result.

function aesEncrypt (data) {
   const key = 'GSTEGSTEjdfheyhdHSHSHSHDHHDHmdjjdn12ndndn5r=';
   const iv = '\0';
   
   const cipher = CryptoJS.AES.encrypt(data, CryptoJS.enc.Base64.parse(key), {
       iv: CryptoJS.enc.Utf8.parse(iv), // parse the IV 
       padding: CryptoJS.pad.Pkcs7,
       mode: CryptoJS.mode.CBC
   })
   
 
   return cipher.toString()
}