Vinayak B
Vinayak B

Reputation: 4520

Unsupported array-like object error when decryption using aes-js library in CBC mode

I am using aes-js library to test AES encryption . When I try to decrypt a ciphertext, I am getting the following error

Error: unsupported array-like object at coerceArray (/data/data/com.suspendresume/files/nodejs-project/node_modules/aes-js/index.js:51:15) at new ModeOfOperationCBC (/data/data/com.suspendresume/files/nodejs-project/node_modules/aes-js/index.js:442:33) at MyEmitter.rn_bridge.channel.on (/data/data/com.suspendresume/files/nodejs-project/main.js:76:15) at emitOne (events.js:115:13) at MyEmitter.emit (events.js:210:7) at Immediate.setImmediate [as _onImmediate] (/data/data/com.suspendresume/files/nodejs-builtin_modules/rn-bridge/index.js:14:13) at runCallback (timers.js:781:20) at tryOnImmediate (timers.js:743:5) at processImmediate [as _immediateCallback] (timers.js:714:5)

The following is my code

 encryptedHex ='yRe2x6Gf2uVzfesp1I7ISkkAjTo2xoH2SPSqXzdWKHg+HhosYblfTFUJVoPVgpyf'
  iv= 'ec8902010adc3d63';
  key='aa54c24fae5e52a5861c80f466a90922'
  key= aesjs.utils.hex.toBytes(key)
 // When ready to decrypt the hex string, convert it back to bytes
 var encryptedBytes = aesjs.utils.hex.toBytes(encryptedHex);

 // The cipher-block chaining mode of operation maintains internal
 // state, so to decrypt a new instance must be instantiated.

 var aesCbc = new aesjs.ModeOfOperation.cbc(key, iv);
 var decryptedBytes = aesCbc.decrypt(encryptedBytes);

 // Convert our bytes back into text
 var decryptedText = aesjs.utils.utf8.fromBytes(decryptedBytes);
 console.log(decryptedText);

Please help me to solve this problem

Upvotes: 2

Views: 1807

Answers (1)

Anders Carstensen
Anders Carstensen

Reputation: 4134

There are a few problems with your code:

  1. The Initialization Vector must also be passed as an array.
  2. The Initialization Vector must be of length 16 bytes (that is 32 in hex). Yours is only 8 bytes.
  3. The variable called encryptedHex doesn't contain hex but it contains base64. Hex only allows numbers and the letters A-F, but the string contains other letters and even +.

I have solved these issues like this:

  1. Convert it to bytes using aesjs.utils.hex.toBytes
  2. Since I don't have the original 16 bytes IV, I can't decrypt your text. Instead, I have created a new encrypted text, IV and Key to demonstrate that the code works.
  3. I convert the base64 string into an Uint8Array using this snippet.

Here's the result. It should print Hello in the console:

encryptedB64 = 'kcGz8P/m0lRRRxcT3tJiSw==';

iv = '6162636465666768696a6b6c6d6e6f70';
iv = aesjs.utils.hex.toBytes(iv);

key = '31323334353637383930313233343536';
key = aesjs.utils.hex.toBytes(key);

// When ready to decrypt the base64 string, convert it back to bytes
var encryptedBytes = Uint8Array.from(atob(encryptedB64), c => c.charCodeAt(0));

// The cipher-block chaining mode of operation maintains internal
// state, so to decrypt a new instance must be instantiated.

var aesCbc = new aesjs.ModeOfOperation.cbc(key, iv);
var decryptedBytes = aesCbc.decrypt(encryptedBytes);

// Convert our bytes back into text
var decryptedText = aesjs.utils.utf8.fromBytes(decryptedBytes);
console.log(decryptedText.trimEnd('\0'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/aes-js/3.1.2/index.min.js"></script>

Upvotes: 4

Related Questions