gpt4 book ai didi

php - mcrypt_encrypt 的 javascript 替代品

转载 作者:搜寻专家 更新时间:2023-10-31 21:42:13 24 4
gpt4 key购买 nike

我想知道是否有一个 javascript 库可以为 php 中的 mcrypt_encrypt 提供功能。

我正在编写一个函数来使用 javascript 访问我的 api。我总是对我的参数进行加密和编码。这是我想要的 js 版本的方法。

public function sendRequest($request_params)
{
//encrypt the request parameters
$enc_request = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->_app_key, json_encode($request_params), MCRYPT_MODE_ECB));

//create the params array, which will
//be the POST parameters
$params = array();
$params['enc_request'] = $enc_request;
$params['app_id'] = $this->_app_id;

//initialize and setup the curl handler
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->_api_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//execute the request
$result = curl_exec($ch);

//json_decode the result
$result = @json_decode($result);
//if everything went great, return the data
return $result;
}

这是我提出的上述请求的 jquery 版本,但它总是返回无效请求。表示 API 无法解密请求

        var queryAPI = function (request_object,callback)
{
var app_key = 'sdffkjhdsjfhsdjkfhsdkj';
var app_secret = 'hfszdhfkjzxjkcxzkjb';
var app_url = 'http://www.veepiz.com/api/jsonp.php';
var enc_request = $.toJSON(request_object);
var ciphertext =encode64(Crypto.AES.encrypt(enc_request, app_secret, { mode: new Crypto.mode.ECB }));
$.post(app_url,{'app_id':app_key,'enc_request':ciphertext},
function (data)
{
console.log(data);
},'jsonp');

}

下面是我如何运行上面的函数

                        var request={'controller':'user','action':'login','emailaddress':email,'password':pass};
queryAPI(request,function (d){console.log(d);});

在服务器端的api上,这里是php解密请求的方式

$params = json_decode(trim(mcrypt_decrypt( MCRYPT_RIJNDAEL_256, $app_secret, base64_decode( urldecode( $enc_request )), MCRYPT_MODE_ECB )));
//check if the request is valid by checking if it's an array and looking for the controller and action
if( $params == false || isset($params->controller) == false || isset($params->action) == false ) {
$result['success'] = 0;
$result['errormsg'] = "Request is not valid! ";
//echo the result of the API call
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/javascript');
$result=json_encode($result);
# JSON if no callback
if( ! isset($_GET['callback']))
exit( $result );

# JSONP if valid callback
if(is_valid_callback($_GET['callback']))
exit( "{$_GET['callback']}($result)" );

# Otherwise, bad request
header('Status: 400 Bad Request', true, 400);
}

最佳答案

你可以试试 crypto-js ,或后代之一 ezcryptocryptojs .我认为 AES 与 Rijndael 相同。

关于php - mcrypt_encrypt 的 javascript 替代品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9532505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com