首页 > 解决方案 > 如何使用 Interceptor 使用 Retrofit2 进行参数(正文)加密

问题描述

我创建了自定义EncryptionInterceptor类,但我无法加密完美的数据,任何帮助表示赞赏,它将节省我的时间

这是我的代码

public class EncryptionInterceptor implements Interceptor {


private static final String TAG = EncryptionInterceptor.class.getSimpleName();

private static final boolean DEBUG = true;

@Override
public Response intercept(Chain chain) throws IOException {

    Request request = chain.request();

    RequestBody oldBody = request.body();

   Buffer buffer = new Buffer();
    oldBody.writeTo(buffer);
    String strOldBody = buffer.readUtf8();
    //MediaType mediaType = MediaType.parse("text/plain; charset=utf-8");
    MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
    String strNewBody = encrypt(strOldBody);

     RequestBody body = RequestBody.create(mediaType, strNewBody);
 request = request.newBuilder()/*.header("Content-Type", body.contentType().toString()).header("Content-Length", String.valueOf(body.contentLength()))*/
                .method(request.method(), body).build();
        return chain.proceed(request);
    }

标签: androidencryptionretrofitretrofit2

解决方案


private static class NetworkInterceptor implements Interceptor {
    
    @Override
    public Response intercept(Chain chain) throws IOException {
        
        Request request = chain.request();
        RequestBody oldBody = request.body(); //retrieve the current request body
        Buffer buffer = new Buffer();
        oldBody.writeTo(buffer);
        String strOldBody = buffer.readUtf8(); // String representation of the current request body
        buffer.clear();
        buffer.close();

        MediaType mediaType = MediaType.parse("application/json; charset=UTF-8");
        String strNewBody = enDecService.encryptBody(strOldBody); // Your encryption logic
        RequestBody body = RequestBody.create(mediaType, strNewBody); // New request body with the encrypted string of the current request body

        request = request.newBuilder()
                .header("Content-Type", "application/json")
                .header("Content-Length", String.valueOf(body.contentLength()))
                .header("Authorization", "Bearer " + AppConstants.token)
                .method(request.method(), body).build();


        long t1 = System.nanoTime();
        Log.d(TAG, String.format("Sending request %s on %s", request.url(), request.headers()));

        Response response = chain.proceed(request); // sending req. to server. current req. body is a encrypted string.
        int maxAge = 6000; // read from cache for 6000 seconds even if there is internet connection
        response.header("Cache-Control", "public, max-age=" + maxAge);
        response = response.newBuilder().removeHeader("Pragma").build();


        long t2 = System.nanoTime();
        Log.d(TAG, String.format("Received response for %s in %.1fms  %s", response.request().url(), (t2 - t1) / 1e6d, response.toString()));

        try {
            String s = response.body().string(); // retrieve string representation of encrypted response.
            ResponseBody responseBody = ResponseBody.create(mediaType, enDecService.decryptBody(s)); // decrypt the encrypted response. yor decryption logic goes here.
            response = response.newBuilder().body(responseBody).build(); // build a new response with the decrypted response body.
        } catch (JOSEException e) {

        } catch (ParseException e) {

        }
        return response;
    }
}

推荐阅读