Runt
9 days ago a3dc9d5522d8a5f2ebc71c8f4b7cd7695a6812d3
网络请求加密相关修复
4 files modified
29 ■■■■■ changed files
libmvi/src/main/java/com/runt/open/mvi/retrofit/Interceptor/OpenInterceptor.java 4 ●●●● patch | view | raw | blame | history
libmvi/src/main/java/com/runt/open/mvi/retrofit/converter/DecryptGsonResponseBodyConverter.java 4 ●●●● patch | view | raw | blame | history
libmvi/src/main/java/com/runt/open/mvi/retrofit/utils/RSAUtils.java 19 ●●●●● patch | view | raw | blame | history
libmvi/src/main/java/com/runt/open/mvi/retrofit/utils/RetrofitUtils.java 2 ●●● patch | view | raw | blame | history
libmvi/src/main/java/com/runt/open/mvi/retrofit/Interceptor/OpenInterceptor.java
@@ -33,7 +33,7 @@
public class OpenInterceptor implements Interceptor {
    protected final Charset UTF8 = Charset.forName("UTF-8");
    protected final String ENCRYPT = "body";
    protected final String ENCRYPT = "paramsString";
    @Override
    public Response intercept(Chain chain) throws IOException {
@@ -108,7 +108,7 @@
    protected String encryptJson(String json){
        try {
            return RSAUtils.encrypt(json,RSAUtils.getPublicKey(RSAUtils.PUBLIC_KEY));
            return RSAUtils.encrypt(json);
        }catch (Exception e){
            e.printStackTrace();
            return e.getMessage();
libmvi/src/main/java/com/runt/open/mvi/retrofit/converter/DecryptGsonResponseBodyConverter.java
@@ -51,10 +51,10 @@
            response = decryptJsonStr(val);//解密
        } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
            e.printStackTrace();
            response = "{\"code\":412,\"message\":\""+"解密数据出错"+e.getMessage()+"\"}";
            response = "{\"code\":412,\"msg\":\""+"解密数据出错"+e.getMessage()+"\"}";
        } catch (JSONException e) {
            e.printStackTrace();
            response = "{\"code\":414,\"message\":\"非标准json\"}";
            response = "{\"code\":414,\"msg\":\"非标准json\"}";
        }catch (Exception e){
            e.printStackTrace();
            JsonReader jsonReader = gson.newJsonReader(value.charStream());
libmvi/src/main/java/com/runt/open/mvi/retrofit/utils/RSAUtils.java
@@ -79,14 +79,17 @@
    /**
     * RSA加密
     *
     * @param data 待加密数据
     * @param key 密钥
     * @param bodyJson 待加密数据
     * @return
     */
    public static String encrypt(String data, Key key) throws Exception {
    public static String encrypt(String bodyJson) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        int inputLen = data.getBytes().length;
        cipher.init(Cipher.ENCRYPT_MODE, getPublicKey(PUBLIC_KEY));
        JSONObject object = new JSONObject();
        object.put("body",bodyJson);
        object.put("sign",sign(bodyJson,RSAUtils.getPrivateKey(PRIVATE_KEY)));
        String json = object.toString();
        int inputLen = json.getBytes().length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offset = 0;
        byte[] cache;
@@ -94,9 +97,9 @@
        // 对数据分段加密
        while (inputLen - offset > 0) {
            if (inputLen - offset > MAX_ENCRYPT_BLOCK) {
                cache = cipher.doFinal(data.getBytes(), offset, MAX_ENCRYPT_BLOCK);
                cache = cipher.doFinal(json.getBytes(), offset, MAX_ENCRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(data.getBytes(), offset, inputLen - offset);
                cache = cipher.doFinal(json.getBytes(), offset, inputLen - offset);
            }
            out.write(cache, 0, cache.length);
            i++;
@@ -147,7 +150,7 @@
        return decryptVerify(jsonBody.getString(key));
    }
    public static String decryptVerify(String body) throws Exception{
        String decrypted = RSAUtils.decrypt(body, RSAUtils.getPrivateKey(RSAUtils.PRIVATE_KEY));
        String decrypted = RSAUtils.decrypt(body, getPrivateKey(RSAUtils.PRIVATE_KEY));
        JSONObject json = new JSONObject(decrypted);
        //服务端公钥验证加签是否正确
        String decryptBody = json.getString("body");
libmvi/src/main/java/com/runt/open/mvi/retrofit/utils/RetrofitUtils.java
@@ -209,7 +209,7 @@
                //设置OKHttpClient
                .client(client)
                //设置baseUrl,注意,baseUrl必须后缀"/"
                .baseUrl(BuildConfig.HOST_IP_ADDR+"api/v4/")
                .baseUrl(BuildConfig.HOST_IP_ADDR+"api/v5/")
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }