gpt4 book ai didi

android - 是否可以在改造库中添加网络拦截器来加密和解密数据有效负载

转载 作者:行者123 更新时间:2023-11-29 15:36:32 26 4
gpt4 key购买 nike

我的应用程序通过 https 与服务器通信,但为​​了提供额外的安全性,我被告知要加密数据负载,因为它包含敏感数据。由于有很多 Web 服务方法,我正在寻找一种解决方案,可以在公共(public)位置加密请求并解密响应,而不是在我使用的所有类上执行加密/解密并节省大量编码。

经过一番研究,我发现网络拦截器可以用来重写请求/响应。

这是一个好的做法吗?如果没有请解释一下

您能否展示一些重写请求/响应正文的示例。

最佳答案

 private static final Interceptor PARAMS_INTERCEPTOR = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
if (original.body() instanceof FormBody) {
TempFormBodyBuilder.Builder newFormBody = new TempFormBodyBuilder.Builder();
FormBody oldFormBody = (FormBody) original.body();
for (int i = 0; i < oldFormBody.size(); i++) {
final String encodedName = oldFormBody.encodedName(i);
String encodedValue = oldFormBody.encodedValue(i);
if (encodedName.equals("info")) {
final String dInfo = URLDecoder.decode(encodedValue, "UTF-8");
LogUtil.i(TAG, "info need to be encode : " + dInfo);
encodedValue = encodeInfo(dInfo);
newFormBody.addEncoded(encodedName, encodedValue);
} else {
newFormBody.addAlEncoded(encodedName, encodedValue);
}
}
final TempFormBodyBuilder builder = newFormBody.build();
requestBuilder.method(original.method(), builder);
requestBuilder.header("Content-Length", builder.contentLength() + "");
final Request request = requestBuilder.build();
Buffer requestBuffer = new Buffer();
if (request.body() != null) {
request.body().writeTo(requestBuffer);
}
LogUtil.i(TAG, request.url() + (request.body() != null ? "?" + _parseParams(request.body(), requestBuffer) : ""));
return chain.proceed(request);
}
}
};

private final Interceptor DECODE_INTERCEPTOR = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
final Response response = chain.proceed(chain.request());

String type = chain.request().header("TYPE");
String encryption = chain.request().header("encryption");
Request request = chain.request()
.newBuilder()
.headers(chain.request()
.headers()
.newBuilder()
.removeAll("encryption")
.build())
.build();
if (encryption) {
Response originalResponse = chain.proceed(request);
ResponseBody body = originalResponse.body();
String result = body.string();
try {
result = DES.decode(result, key);
} catch (Exception e) {
LogUtil.log(e);
RxBus.getInstance().postEvent(new AuthException(e, AuthException.MORE_PHONES_LOGIN_CODE));
throw new AuthException(e, AuthException.MORE_PHONES_LOGIN_CODE);
}
return originalResponse.newBuilder().body(ResponseBody.create(MediaType.parse("application/json"), result))
.build();
} else {
return response;
}
}
};

private Manager(Cache cache) {
final OkHttpClient.Builder builder = new OkHttpClient.Builder().
retryOnConnectionFailure(true).
addNetworkInterceptor(PARAMS_INTERCEPTOR).
addInterceptor(DECODE_INTERCEPTOR).
addInterceptor(LOGGING_INTERCEPTOR).
connectTimeout(TIMEOUT_VALUE, TimeUnit.SECONDS).
readTimeout(TIMEOUT_VALUE, TimeUnit.SECONDS).
writeTimeout(TIMEOUT_VALUE, TimeUnit.SECONDS);
if (cache != null) {
builder.cache(cache);
}
this.mOkHttpClient = builder.build();

}

关于android - 是否可以在改造库中添加网络拦截器来加密和解密数据有效负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48637188/

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