斯坦福Javascript加密库。 (SJCL)在不发送参数的情况下加密和解密

时间:2016-06-02 20:27:05

标签: javascript encryption cryptography sjcl

SJCL Demo中,有一个复选框“发送参数和经过身份验证的数据以及消息”。将消息减少到非常短的字符串。

我找不到包含有效参数的列表,用于加密(和解密)功能:

var encryptedMessage = sjcl.encrypt(key,message, ??? );

var decryptedMessage = sjcl.decrypt(key,encryptedMessage);

4 个答案:

答案 0 :(得分:1)

如果输入密码,文本并单击加密,结果将显示在密文框中,例如:
密码:"传递",消息:"文字",认证数据:" XXX"

结果:

{
"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
}

site的使用示例:

sjcl.encrypt("password", "data") 
sjcl.decrypt("password", "encrypted-data")

尽管有可用的说明,但平台上通常不支持模式CCMOCB2

答案 1 :(得分:0)

感谢您的帮助!!! 对于我数据库中的最小消息开销,此解决方案适用于我:

  //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"

答案 2 :(得分:0)

使用此:

<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>

答案 3 :(得分:-1)

谢谢,斯蒂芬。分离选项可能会更清晰,因此我们可以遍历要删除的选项。另外,salt需要是base64,我们可以使用JavaScript的btoa()函数轻松生成它。

  //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"