我尝试使用mcrypt并加密和解密json数据跨域。实际上,当我在同一个域中使用加密但在ajax中不起作用时,这种方法很有用。
Here is function all:
function all($contentType = 'page') {
$secret_key = "12345678911234567892123456789312";
$this->load->database();
$query = $this->db->query(
"SELECT * FROM category"
);
$buffer = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $secret_key, json_encode($result), MCRYPT_MODE_ECB);
$this->output->set_output(base64_encode($buffer));
Here is file of view:
$(function(){
try {
$.ajax({
type: "GET",
url: "http://localhost:8888/DIY/index.php/user/all/json",
crossDomain: true,
contentType: "application/x-www-form-urlencoded",
async: false,
dataType: 'text',
processData: false,
cache: false,
success: function (response) {
var a = mcrypt.Decrypt($.base64('decode', response ));
console.log( JSON.parse(a) );
},
error: function (ErrorResponse) {
console.log('error');
}
});
}
catch (error) {
}
});
The output in Chrome is:
[{"id":"1","EMAIL":"sda@q.com","PHONE":"sdsng","USERNAME":"12das","FIRST_NAME":null,"LAST_NAME":null,"PASSWORD":"","TYPE":null},{"id":"2","EMAIL":"adas","PHONE":"dsada","USERNAME":"asdasd","FIRST_NAME":null,"LAST_NAME":null,"PASSWORD":"","TYPE":null}]
如果我在视图文件中添加JSON.parse(a),它将不会显示任何内容。但是,在Chrome中获得console.log(a)后,我直接将输出数据复制为参数,并在Chrome控制台中使用它
JSON.parse('[{"id":"1","EMAIL":"139520519@qq.com","PHONE":"yang_zhang","USERNAME":"122545929","FIRST_NAME":null,"LAST_NAME":null,"PASSWORD":"","TYPE":null},{"id":"2","EMAIL":"adas","PHONE":"dsada","USERNAME":"asdasd","FIRST_NAME":null,"LAST_NAME":null,"PASSWORD":"","TYPE":null}]')
[Object, Object]
输出是我正在寻找的!任何人都可以帮我解决这个问题吗?
答案 0 :(得分:0)
现在它已经解决了。我将mcrypt的模式从ecb更改为cfb,并将IV传递给不同的域。然后使用传递的IV解密数据将获得可以使用JSON.parse()获取预期的json对象的字符串