Reputation: 69
In the SJCL Demo there is the checkbox "Send the parameters and authenticated data along with the message." to reduce the message to a very short string.
I can´t find a list with valid parameters for the use in the encrypt (and decrypt) function:
var encryptedMessage = sjcl.encrypt(key,message, ??? );
var decryptedMessage = sjcl.decrypt(key,encryptedMessage);
Upvotes: 1
Views: 7297
Reputation: 195
Use this:
<script type="text/javascript" src="js/sjcl.js"></script>
<script type="text/javascript">
var cypheredMsg = sjcl.encrypt("secret", "Hi Amresh!");
var plainMsg = sjcl.decrypt("secret", cypheredMsg);
console.log(cypheredMsg);
console.log(plainMsg);
</script>
Upvotes: 0
Reputation: 112855
If you enter a password, text and click encrypt the result will be in the Ciphertext box, ex:
password:"pass", message:"text", Authenticated data:" xxx"
Result:
{
"iv":"tjp81jkAzUpW1bI9gLDDpg==", // iv Base64 encoded
"v":1, // version
"iter":1000, // iteration count
"ks":128, // key size in bits
"ts":64, // authentication strength
"mode":"ccm", // mode
"adata":"xxx", // authenticated data
"cipher":"aes", // cipher
"salt":"lx06UoJDNys=", // key derivation salt
"ct":"Gv7ptKdTtUz6AGtX" // ciphet text
}
Example usage from the site:
sjcl.encrypt("password", "data")
sjcl.decrypt("password", "encrypted-data")
The catch is that notwithstanding the availability stated the modes CCM
and OCB2
are not commonly supported across platforms.
Upvotes: 2
Reputation: 11
Thanks, Steffen. It might be a little cleaner to separate the options, so then we can iterate through the options to delete. Also, the salt needs to be base64, which we can easily generate with JavaScript's btoa() function.
//Encrypt
var salt = btoa( "myGeneratedSalt" );
var options = {mode:"ccm",iter:1000,ks:128,ts:64,v:1,cipher:"aes",adata:"",salt:salt}
var encryptedMessage = sjcl.encrypt("myPassword","myMessage",options);
var parsedMessage = JSON.parse(encryptedMessage);
var prop;
for (prop in options) {
delete parsedMessage[prop];
}
encryptedMessageWithoutParameters = JSON.stringify(parsedMessage);
//Decrypt
var parsedMessage = JSON.parse(encryptedMessageWithoutParameters);
jQuery.extend(parsedMessage,options);
messageWithParameters = JSON.stringify(parsedMessage);
var decryptedMessage = sjcl.decrypt("myPassword",messageWithParameters);
//Result > "myMessage"
Upvotes: -1
Reputation: 69
Thanks for your help!!! For a minimal message overhead in my database this solution works for me:
//Encrypt
var encryptedMessage = sjcl.encrypt("myPassword","myMessage",{mode:"ccm",iter:1000,ks:128,ts:64,v:1,cipher:"aes",adata:"",salt:"myGeneratedSalt"});
var parsedMessage = JSON.parse(encryptedMessage);
delete parsedMessage.mode;
delete parsedMessage.iter;
delete parsedMessage.ks;
delete parsedMessage.ts;
delete parsedMessage.v;
delete parsedMessage.cipher;
delete parsedMessage.salt;
delete parsedMessage.adata;
encryptedMessageWithoutParameters = JSON.stringify(parsedMessage);
//Decrypt
var parsedMessage = JSON.parse(encryptedMessageWithoutParameters);
jQuery.extend(parsedMessage,{mode:"ccm",iter:1000,ks:128,ts:64,v:1,cipher:"aes",adata:"",salt:"myGeneratedSalt"});
messageWithParameters = JSON.stringify(parsedMessage);
var decryptedMessage = sjcl.decrypt("myPassword",messageWithParameters);
//Result > "myMessage"
Upvotes: 1