问题
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
Create an array of random words which will serve as our IV(initialisation vector).
Then you need to create a bit array using a random key(size depends upon level of encryption and the type)
Then you create a cypher using the key array.
And finally encode your data using the cypher and the IV. (you can also add meta data to check the authenticity)
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
While decrypting slice the IV from the encryted string.
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