How to decrypt AES with SJCL

2.3k views Asked by At

I want to decrypt AES by given cipher and key with the Stanford Javascript Crypto Library (SJCL), but i can't pass the key:

var key = 'key';
var cipher = 'abjslö';
var aes = new sjcl.cipher.aes(key);

var plaintext = aes.decrypt(cipher);
alert(plaintext);

This dosen't work. Referred to the documentation, the key has to be "an array of 4, 6 or 8 words".

How could this be done?

2

There are 2 answers

1
Maarten Bodewes On

The key has to be an AES key, which is 128, 192 or 256 bits. The SJCL library however operates on 32 bit machine "words". Check out the source of the open source library or one of the tests to find out what to pass. Note that a password is not a key, you need a password based key derivation function such as PBKDF2 to convert a password into a key.

0
HARSH BHANOT On

Encryption

  1. Create an array of random words which will serve as our IV(initialisation vector).

  2. Then you need to create a bit array using a random key(size depends upon level of encryption and the type)

  3. Then you create a cypher using the key array.

  4. And finally encode your data using the cypher and the IV. (you can also add meta data to check the authenticity)

  5. Now just concat the IV and the encrypted bit array and finally convert it to a base64 string and pass.

Please note that you cannot decrypt a AES-GCM without IV.

Decryption

  1. While decrypting slice the IV from the encryted string.

  2. Now using the key create a cypher and use that to decrypt the string.

You can find the complete code here.