title: How can I encrypt a string with AES-128-CBC algorithm in Javascript?
body: <p>I have the following shell script which uses openssl to encrypt string:</p>
  
  <pre><code>API_KEY="qrBprgc/3dTjrrD@4t!9FcNjwT3_Ra"
  DATE="Mon, 19 Mar 2018 12:45:05 EET"
  
  aesivkey=$(echo -n "$DATE" | openssl dgst -sha256 -hmac "$API_KEY" -r)
  aes128cbciv=${aesivkey:0:32}
  aes128cbckey=${aesivkey:32:32}
  
  private_key="test"
  encrypted_private_key=`echo -e $private_key | openssl aes-128-cbc -base64 -nosalt -K $aes128cbckey -iv $aes128cbciv`
  </code></pre>
  
  <p>I am trying to make the same function in javascript(to use it in postman). At the moment I have the following code:</p>
  
  <pre><code>var dateString = "Mon, 19 Mar 2018 12:45:05 EET";
  var api_key="qrBprgc/3dTjrrD@4t!9FcNjwT3_Ra"
  
  //aesivkey=$(echo -n "$DATE" | openssl dgst -sha256 -hmac "$API_KEY" -r)
  var aesivkey = (CryptoJS.HmacSHA256(dateString, api_key)).toString();
  //aes128cbciv=${aesivkey:0:32}
  var aes128cbciv = aesivkey.substring(0, 32);
  //aes128cbckey=${aesivkey:32:32}
  var aes128cbckey = aesivkey.substring(aesivkey.length - 32);
  
  var private_key="test"
  
  //encrypted_private_key=`echo -e $private_key | openssl aes-128-cbc -base64 -nosalt -K $aes128cbckey -iv $aes128cbciv`
  var encrypted_private_key = CryptoJS.AES.encrypt(private_key, aes128cbckey,
  {
      keySize: 128 / 8,
      iv: aes128cbciv,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7
  });
  </code></pre>
  
  <p>Could someone please explain what I am doing wrong?</p>
  
  <p>Result in shell script: HSMD8RaXNbRrN4c1NzFXvQ==</p>
  
  <p>Result in javascript: U2FsdGVkX1+uapLKV00iSOtj8eVpjfY4onoqQmoPPF4=</p>