AES NODE + Arduino

Hi,

I’ve managed to get it to encrypt and decrypt in arduino, but in node, I can only encrypt and decrypt if I don’t add the IV, someone can help me please!!.

Arduino Encrypt → Arduino Decrypt OK
Arduino Encrypt → Node Decrypt OK
Node Encrypt → Node Decrypt KO (fail IV)

I can’t encode the IV , for decryption works :’(

Node Encrypt → Node Decrypt With IV (fail) :

var CryptoJS = require("crypto-js");
var AESKey = '2B7E151628AED2A6ABF7158809CF4F3C';


encryptMessage("helloAll")


function encryptMessage(messagePlain) {
    console.log("-----------------------")
    console.log("---- EncryptMessage ---")
    console.log("-----------------------")

    try {
        var key = CryptoJS.enc.Hex.parse(AESKey);
        var ivPlain = "987654321098765"  // generate IV
       // var ivBase64 = new Buffer(ivPlain).toString('base64')    // ENCODE BASE 64 IV
        var ivHex = CryptoJS.enc.Hex.parse(ivPlain);
        var txtBase64 = new Buffer(messagePlain).toString('base64')    // ENCODE BASE 64 TEXT
        var encrypted1 = CryptoJS.AES.encrypt(txtBase64, key , { iv: ivHex });      
        encrypted1.toString(CryptoJS.enc.base64)

        console.log("encrypted1: "+ encrypted1);
        console.log("iv1: "+ ivHex);

        decryptMessage(encrypted1, ivHex)


    }catch(error) {
        console.log("error ENCRYPT: "+error)
    } 
}

function decryptMessage(msg, ivSEend) { 
    console.log("-----------------------")
    console.log("---- DecryptMessage ---")
    console.log("-----------------------")

    var plain_iv =  new Buffer( ivSEend , 'base64').toString('hex'); // DECODE BASE 64 IV -> transform HEX
    var iv = CryptoJS.enc.Hex.parse( plain_iv );
    var key = CryptoJS.enc.Hex.parse( AESKey );
    
    // Decrypt
    var bytes  = CryptoJS.AES.decrypt( msg, key , { iv: iv} )
    console.log("bytes: "+bytes)
    
    try {
        var plaintext = bytes.toString(CryptoJS.enc.Base64)
        var decoded_b64msg =  new Buffer(plaintext , 'base64').toString('ascii');  // DECODE BASE 64 IV
        var decoded_msg =     new Buffer( decoded_b64msg , 'base64').toString('ascii');            
        console.log("Decrypt: ", decoded_msg); 
    }catch(error) {
        console.log("error DECRYPT: "+error)
    }   
}

Arduino encrypt → Node decrypt With IV:

var esp8266_msg = '/1aZRwVaw3jv+ct8HS4pCV5lThvTG70M90ARiyAsIDYMkfJE3w8F3bgxaOKVA0rX4m1Mq50VVN0u9gRw9F2gKE4r2OcY8oECv8bKT80F9pY=';
var esp8266_iv  = 'cAFviaDMHejlteGn9/4eQQ==';

decryptMessage(esp8266_msg, esp8266_iv)

function decryptMessage(msg, ivSEend) {  
    console.log("-----------------------")
    console.log("---- DecryptMessage ---")
    console.log("-----------------------")

    var plain_iv =  new Buffer( ivSEend , 'base64').toString('hex'); // DECODE BASE 64 IV -> transform HEX
    var iv = CryptoJS.enc.Hex.parse( plain_iv );
    var key = CryptoJS.enc.Hex.parse( AESKey );
    
    // Decrypt
    var bytes  = CryptoJS.AES.decrypt( msg, key , { iv: iv} )
    
    try {
        var plaintext = bytes.toString(CryptoJS.enc.Base64)
        var decoded_b64msg =  new Buffer(plaintext , 'base64').toString('ascii');  // DECODE BASE 64 IV
        var decoded_msg =     new Buffer( decoded_b64msg , 'base64').toString('ascii');            
        console.log("Decrypt: ", decoded_msg); 
    }catch(error) {
        console.log("error DECRYPT: "+error)
    }   
}

Arduino encrypt → Arduino decrypt With IV:

void encriptar(String message) {  // OK
   
    // IV
    String ivRandom = "1234567890123456";
    byte ivByteArray[ivRandom.length()];
    ivRandom.getBytes(ivByteArray, ivRandom.length());


    char b64dataIV[200];
    base64.b64_encode( b64dataIV, (char *)ivByteArray, N_BLOCK);
    String iv = String(b64dataIV);
    Serial.println ("IV MIO: " + iv);

    
    // encript message  
    int b64len = base64.b64_encode(b64data, (char *)message.c_str(),message.length()); 
    aes.do_aes_encrypt((byte *)b64data, b64len , cipher, key, 128, ivByteArray);

    
    // DATA encripted
    base64.b64_encode(b64data, (char *)cipher, aes.get_size() );
    String data = String(b64data);

    Serial.println ("Data: " + data);


    decrypt(b64data, b64dataIV, aes.get_size());
    
}

void decrypt(String b64data, String IV_base64, int lsize) {  // OK
Serial.println("------ NEW DECRYPT ------");
  
  char data_decoded[300];
  char iv_decoded[300];
  byte out[300];
  char temp[300];
  b64data.toCharArray(temp, 300);
  base64.b64_decode(data_decoded, temp, b64data.length());
  IV_base64.toCharArray(temp, 300);
  base64.b64_decode(iv_decoded, temp, IV_base64.length());
  aes.do_aes_decrypt((byte *)data_decoded, 50, out, key, 128, (byte *)iv_decoded);
  char message[1000];
  base64.b64_decode(message, (char *)out, aes.get_size());
  for (int i = 0; i < aes.get_size(); i++)
  {
    char curChar = (char)message[i];
    if (curChar != '}')
      temp[i] = curChar;
    else
    {
      temp[i] = curChar;
      temp[i+1] = '\0';
      break;
    }
  }
  String result = String((char *)temp);
  Serial.println(result);
  Serial.println("=======");
}

Thank you very much.