Runt
9 days ago a3dc9d5522d8a5f2ebc71c8f4b7cd7695a6812d3
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");