-2

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()
}
Topaco
  • 40,594
  • 4
  • 35
  • 62
  • please see cryptoJS: https://cryptojs.gitbook.io/docs/ it supports tripledes and aes – GrafiCode Sep 13 '22 at 12:38
  • 1
    Please post your most recent JavaScript code and describe the problem. – Topaco Sep 13 '22 at 12:43
  • The C# code uses AES and not TripleDES. The `TripleDESCryptoServiceProvider` instance is not used at all for encryption. – Topaco Sep 13 '22 at 13:29
  • Also the mode in the CryptoJS code is not correct (you have to use the CBC mode with a zero IV to be functionally identical to the C# code) – Topaco Sep 13 '22 at 13:43
  • And finally, in the CryptoJS code, the key must be Base64 decoded (the MD5 key derivation must be removed) and the text must be Utf-8 encoded. – Topaco Sep 13 '22 at 13:46
  • With the given directions from @Topaco here is the new JS function. but still did not return same value. `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() }` – Nuri Erginer Sep 13 '22 at 16:46
  • Regarding the key, replace the Utf8 encoder with the Base64 encoder. Regarding the IV you have to use \0 instead of 0 (the former corresponds to binary 0x00, the latter to 0x30). – Topaco Sep 13 '22 at 16:52
  • You are great @Topaco I answer the question with your directions. Others may use it in the future. Thanks. – Nuri Erginer Sep 13 '22 at 17:13

1 Answers1

1

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()
}