How to decrypt AES with SJCL

十年热恋 提交于 2020-03-24 11:41:12

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/20821085/how-to-decrypt-aes-with-sjcl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!