Skip to content
Advertisement

Decrypt AES in JavaScript

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.

const crypto = require("crypto");

var key = "";
const iv =  "";
const token = "";


function decrypt(token, iv, key) {
    const decrypter = crypto.createDecipheriv("aes-256-cbc", key, iv);
    let decrypted = decrypter.update(token, "hex", "utf8");
    decrypted += decrypter.final("utf8");
    return  decrypted
}

console.log(decrypt(token, iv, key));

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.

EDIT: I am using CryptoSwift library in Swift language.

func encryption(uuid: String, token: String) -> 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 "error"
    }
}

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.

EDIT2:

  • 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.

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));
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/pbkdf2.js"></script>

Advertisement

Answer

CryptoJS uses WordArrays, so that key, IV and ciphertext have to be converted accordingly. For this purpose the appropriate encoders have to be applied. Furthermore decrypt() expects the ciphertext as CipherParams object.

This results in the following possible CryptoJS implementation:

var ciphertext = "79a247e48ac27ed33ca3f1919067fa64";
var key = "6268890F-9B58-484C-8CDC-34F9C6A9";
var iv = "6268890F-9B58-48";

var ciphertextWA = CryptoJS.enc.Hex.parse(ciphertext);
var keyWA = CryptoJS.enc.Utf8.parse(key);
var ivWA = CryptoJS.enc.Utf8.parse(iv);
var ciphertextCP = { ciphertext: ciphertextWA };

var decrypted = CryptoJS.AES.decrypt(
    ciphertextCP,
    keyWA, 
    { iv: ivWA }
);

console.log(decrypted.toString(CryptoJS.enc.Utf8)); // Apple
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>

which is functionally identical to the posted NodeJS code that also successfully decrypts the test data.


Regarding the question asked in the comment about the encodings:
In general, the decryption side must have knowledge of the encodings used for encryption. However, in this case the encodings can be derived from the posted NodeJS code:

Also, the data used is consistent with these conclusions.


Note that for security reasons a static IV may not be used. Instead, a random IV must be generated for each encryption.
Also, no password may be applied as key, even if it has the right length. If a password is to be used, a key derivation is necessary, e.g. with PBKDF2.
For test purposes, the data is of course enough.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement