title: Decrypt AES in JavaScript
body: <p>I am encrypting a text with AES256 in swift language and outputting it as hex. I want to decrypt this code I received with JS, but I could not reach the result. I tried the CryptoJS library but still couldn't get the result I wanted. All I want is the js code that will give me the decoded version when I enter the IV, password and ciphertext.</p>
  <pre class="lang-js prettyprint-override"><code>const crypto = require(&quot;crypto&quot;);
  
  var key = &quot;&quot;;
  const iv =  &quot;&quot;;
  const token = &quot;&quot;;
  
  
  function decrypt(token, iv, key) {
      const decrypter = crypto.createDecipheriv(&quot;aes-256-cbc&quot;, key, iv);
      let decrypted = decrypter.update(token, &quot;hex&quot;, &quot;utf8&quot;);
      decrypted += decrypter.final(&quot;utf8&quot;);
      return  decrypted
  }
  
  console.log(decrypt(token, iv, key));
  </code></pre>
  <p>With the Node.js code above, I achieve what I want, but I want to do it with normal JS code, not using node. I don't want to mess with the server. I would be very happy if you help.</p>
  <p>EDIT:
  I am using CryptoSwift library in Swift language.</p>
  <pre><code>func encryption(uuid: String, token: String) -&gt; String {
      do {
          let aes = try AES(key: String(uuid.prefix(32)), iv: String(uuid.prefix(16)))
          
          let ciphertext = try aes.encrypt(Array(token.utf8))
          let encrypttext = ciphertext.toHexString()
          return encrypttext
      }
      catch {
          return &quot;error&quot;
      }
  }
  </code></pre>
  <p>I tried to do something with CryptoJS with the codes from the site below, but it didn't work like the codes in Node.js.</p>
  <ul>
  <li><a href="https://embed.plnkr.co/0VPU1zmmWC5wmTKPKnhg/" rel="nofollow noreferrer">https://embed.plnkr.co/0VPU1zmmWC5wmTKPKnhg/</a></li>
  </ul>
  <p>EDIT2:</p>
  <ul>
  <li>I've been trying different things but couldn't quite figure it out. I get an error when I add PBKDF2. I don't fully understand the problem.</li>
  </ul>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var password = "6268890F-9B58-484C-8CDC-34F9C6A9";
  var iv = "6268890F-9B58-48";
  var cipher = "79a247e48ac27ed33ca3f1919067fa64";
  
  /*
  var key = CryptoJS.PBKDF2(password, {
        keySize: 32
      });
  */
  
    var dec= CryptoJS.enc.Hex.parse(cipher);
    const decrypted = CryptoJS.AES.decrypt({
    ciphertext: dec 
  },
     password, {
       iv: iv,
        mode: CryptoJS.mode.CBC, 
        padding: CryptoJS.pad.Pkcs7
        });
  
        console.log(decrypted.toString(CryptoJS.enc.Utf8));</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"&gt;&lt;/script&gt;
  &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/pbkdf2.js"&gt;&lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>