首页 \ 问答 \ JS:存储在数据库(SJCL)后无法解密(JS: Decryption not possible after storing in database (SJCL))

JS:存储在数据库(SJCL)后无法解密(JS: Decryption not possible after storing in database (SJCL))

我正在尝试使用Stanford Javascript加密库(SJCL)并希望加密并稍后解密字符串。

以下代码工作正常:

var pw = "password";
var message = "message";
var encrypted = sjcl.encrypt(pw, message);
alert(encrypted);

var decrypted = sjcl.decrypt(pw, encrypted)
alert(decrypted);

第一个警报显示加密数据,第二个警报显示“消息”。 但是我需要将var加密存储在SQL数据库中,因此我将它通过ajax发送到服务器,然后将其存储在表中。

我后来请求加密的消息(再次通过ajax)并将其存储在加密的变量中。 之后我想解密它:

var decrypted = sjcl.decrypt(pw, encrypted);
alert(decrypted);

但我没有收到包含字符串“messages”的警报,控制台只显示“未捕获的异常:CORRUPT:ccm:tag不匹配”。

我没有更改加密文本,两个示例之间的唯一区别是我从服务器加密了变量。

有什么想法有什么不对?

编辑:

将它存储在DB中的ajax代码:

var url = "action.php?action=r&n="+name+"&pw="+pw_hashed+"&encrypted="+encrypted;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function()
{
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
    {
        if(xmlhttp.responseText == "success")
        {
            alert("success");
        }
    }
}

以及接收数据的ajax代码:

var url = "action.php?action=l&n="+name+"&pw="+pw_hashed;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function()
{
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
    {
        if(xmlhttp.responseText == "success")
        {
            var encrypted = xmlhttp.responseText;;
        }
    }
}

我还将加密后的加密字符串与服务器上的字符串和客户端上的字符串(用于解密)进行了比较:所有都是相同的。


I'm experimenting with the Stanford Javascript Crypto Library (SJCL) and want to encrypt and later decrypt a string.

The following code works fine:

var pw = "password";
var message = "message";
var encrypted = sjcl.encrypt(pw, message);
alert(encrypted);

var decrypted = sjcl.decrypt(pw, encrypted)
alert(decrypted);

The first alert show the encrypted data and the second alert shows "message". However I need to store var encrypted in a SQL database so I send it through ajax to the server which stores it in a table.

I later request the encrypted message (again through ajax) and store it in the variable encrypted. After that I want to decrypt it:

var decrypted = sjcl.decrypt(pw, encrypted);
alert(decrypted);

But I don't get an alert which contains the string "messages", the console only displays "uncaught exception: CORRUPT: ccm: tag doesn't match".

I didn't change the encrypted text, the only difference between the two examples is that I got the variable encrypted from the server.

Any ideas what is wrong?

EDIT:

The ajax code to store it in the DB:

var url = "action.php?action=r&n="+name+"&pw="+pw_hashed+"&encrypted="+encrypted;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function()
{
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
    {
        if(xmlhttp.responseText == "success")
        {
            alert("success");
        }
    }
}

And the ajax code to receive the data:

var url = "action.php?action=l&n="+name+"&pw="+pw_hashed;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function()
{
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
    {
        if(xmlhttp.responseText == "success")
        {
            var encrypted = xmlhttp.responseText;;
        }
    }
}

I also compared the the encrypted string right after the encryption with the string on the server and with the one on the client side (for the decryption): all are the same.


原文:https://stackoverflow.com/questions/30425767
更新时间:2023-01-05 11:01

最满意答案

Try to implement Listener like this

LinearLayout test = (LinearLayout) findViewById(R.id.lineartest);
 TableLayout ll = (TableLayout) findViewById(R.id.tabletest);
 TableRow row= new TableRow(this);
 TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
 row.setLayoutParams(lp);

 for (int i = 0; i < 10; i++) {
    Chip c1 = new Chip(this);
    c1.setText("Hey I'm " + i); 
    c1.setOnClickListener(new Listener(c1));
    row.addView(c1);
 }
 ll.addView(row);
 }
 class Listener implements View.OnClickListener
    {
    Chip view;
    Listener(Chip view)
    {
    this.view = view;
    }

     @Override
        public void onClick(View v) {
          if (view.is_stateChanged()) {
                    view.set_selected(false);
            } else {
                view.set_selected(true);
            }
            view.set_stateChanged(!view.is_stateChanged());
        }
    }

Try to implement Listener like this

LinearLayout test = (LinearLayout) findViewById(R.id.lineartest);
 TableLayout ll = (TableLayout) findViewById(R.id.tabletest);
 TableRow row= new TableRow(this);
 TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
 row.setLayoutParams(lp);

 for (int i = 0; i < 10; i++) {
    Chip c1 = new Chip(this);
    c1.setText("Hey I'm " + i); 
    c1.setOnClickListener(new Listener(c1));
    row.addView(c1);
 }
 ll.addView(row);
 }
 class Listener implements View.OnClickListener
    {
    Chip view;
    Listener(Chip view)
    {
    this.view = view;
    }

     @Override
        public void onClick(View v) {
          if (view.is_stateChanged()) {
                    view.set_selected(false);
            } else {
                view.set_selected(true);
            }
            view.set_stateChanged(!view.is_stateChanged());
        }
    }

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。