|
@@ -80,6 +80,10 @@ class JsonRpcClientBase {
|
|
|
|
|
|
String package = jsonEncode(req.toJson());
|
|
|
|
|
|
+ if (requestEncryptConfig.enable) {
|
|
|
+ package = _encryptRequestPackage(package);
|
|
|
+ }
|
|
|
+
|
|
|
var response = await _postRequest(package);
|
|
|
if (response == null) throw JsonRpcException(message: "Response Empty");
|
|
|
|
|
@@ -106,6 +110,15 @@ class JsonRpcClientBase {
|
|
|
throw JsonRpcException(
|
|
|
message: "Http error.Status coder: ${response.statusCode}.");
|
|
|
}
|
|
|
+ if (response.headers.containsKey("response_encrypt_mode")) {
|
|
|
+ final resEncryptMode = response.headers["response_encrypt_mode"]!;
|
|
|
+ // 解密响应包
|
|
|
+ if (resEncryptMode.isNotEmpty && resEncryptMode.first.isNotEmpty) {
|
|
|
+ final resPackage = _decryptResponsePackage(response.data);
|
|
|
+ final resJsonMap = jsonDecode(resPackage);
|
|
|
+ return resJsonMap;
|
|
|
+ }
|
|
|
+ }
|
|
|
return response.data;
|
|
|
} on dio.DioError catch (e) {
|
|
|
throw JsonRpcException(message: "Http error", data: e);
|
|
@@ -136,6 +149,44 @@ class JsonRpcClientBase {
|
|
|
result = await jsonRpcInterceptHost.onResponseResult(result);
|
|
|
return result;
|
|
|
}
|
|
|
+
|
|
|
+ String _encryptRequestPackage(String package) {
|
|
|
+ final mode = requestEncryptConfig.encryptMode.toLowerCase();
|
|
|
+ if (kIsWeb) {
|
|
|
+ return package;
|
|
|
+ }
|
|
|
+ if (mode == "sm2") {
|
|
|
+ // 使用国密2加密请求
|
|
|
+ return JsonRpcEncryptUtils.sm2Encrypt(
|
|
|
+ package,
|
|
|
+ requestEncryptConfig.encryptKey,
|
|
|
+ );
|
|
|
+ }
|
|
|
+ if (mode == "rsa") {
|
|
|
+ // 使用RSA加密请求
|
|
|
+ return JsonRpcEncryptUtils.rsaEncrypt(package);
|
|
|
+ }
|
|
|
+ return package;
|
|
|
+ }
|
|
|
+
|
|
|
+ String _decryptResponsePackage(String package) {
|
|
|
+ final mode = responseEncryptConfig.encryptMode.toLowerCase();
|
|
|
+ if (mode == "sm4") {
|
|
|
+ // 使用国密4解密
|
|
|
+ return JsonRpcEncryptUtils.sm4Decrypt(
|
|
|
+ package,
|
|
|
+ responseEncryptConfig.encryptKey,
|
|
|
+ );
|
|
|
+ }
|
|
|
+ if (mode == "des") {
|
|
|
+ // 使用DES解密
|
|
|
+
|
|
|
+ // 截取前8位
|
|
|
+ final key = responseEncryptConfig.encryptKey.substring(0, 8);
|
|
|
+ return JsonRpcEncryptUtils.desDecrypt(package, key);
|
|
|
+ }
|
|
|
+ return package;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
abstract class JsonRpcRequestModelBase {
|