5回答

0收藏

【已完结】一个java的加解密方法转PHP

悬赏求解 悬赏求解 4318 人阅读 | 5 人回复 | 2020-04-24

package com.szcx.lib.encrypt;
import android.text.TextUtils;
import com.szcx.lib.encrypt.utils.AesCfbUtil;
import com.szcx.lib.encrypt.utils.MD5Util;
import com.szcx.lib.encrypt.utils.StringUtil;
import com.szcx.lib.encrypt.utils.TimeUtil;
import java.io.PrintStream;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.json.JSONException;
import org.json.JSONObject;
public class EncryptManager {
    private static final String TAG = "EncryptManager";
    private static volatile EncryptManager instance;
    private String appKey = "scb37537f85scxpcm59f7e318b9epa51";
    private Cipher cipher;
    private String encryptKey;
    private boolean isDebug = false;
    private byte[][] key_iv;
    private SecretKeySpec skeySpec;
    private EncryptManager() {
        try {
            this.cipher = Cipher.getInstance("AES/CFB/NoPadding");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            this.cipher = null;
        } catch (NoSuchPaddingException e2) {
            e2.printStackTrace();
            this.cipher = null;
        }
    }
    public static EncryptManager getInstance() {
        if (instance == null) {
            synchronized (EncryptManager.class) {
                if (instance == null) {
                    instance = new EncryptManager();
                }
            }
        }
        return instance;
    }
    public void init(String str, String str2) {
        this.encryptKey = str;
        this.appKey = str2;
        try {
            this.key_iv = AesCfbUtil.EVP_BytesToKey(32, 16, null, str.getBytes("UTF-8"), 0);
            this.skeySpec = new SecretKeySpec(this.key_iv[0], "AES");
        } catch (String str3) {
            str3.printStackTrace();
            this.skeySpec = null;
        } catch (String str32) {
            str32.printStackTrace();
            this.skeySpec = null;
        }
    }
    private String encrypt(String str) {
        logInfo(str);
        if (StringUtil.isEmpty(str) || this.cipher == null || this.skeySpec == null) {
            return null;
        }
        try {
            this.cipher.init(1, this.skeySpec);
            try {
                return AesCfbUtil.byte2hex(AesCfbUtil.byteMerger(this.cipher.getIV(), this.cipher.doFinal(str.getBytes("UTF-8"))));
            } catch (String str2) {
                str2.printStackTrace();
                return null;
            } catch (String str22) {
                str22.printStackTrace();
                return null;
            } catch (String str222) {
                str222.printStackTrace();
                return null;
            }
        } catch (String str2222) {
            str2222.printStackTrace();
            return null;
        }
    }
    private String decrypt(String str) {
        logInfo(str);
        if (StringUtil.isEmpty(str) || this.cipher == null || this.skeySpec == null) {
            return null;
        }
        str = AesCfbUtil.hex2byte(str);
        byte[] copyOfRange = Arrays.copyOfRange(str, 0, 16);
        str = Arrays.copyOfRange(str, 16, str.length);
        try {
            this.cipher.init(2, this.skeySpec, new IvParameterSpec(copyOfRange));
            String str2 = new String(this.cipher.doFinal(str), "UTF-8");
            logInfo(str2);
            return str2;
        } catch (String str3) {
            str3.printStackTrace();
            return null;
        } catch (String str32) {
            str32.printStackTrace();
            return null;
        } catch (String str322) {
            str322.printStackTrace();
            return null;
        } catch (String str3222) {
            str3222.printStackTrace();
            return null;
        } catch (String str32222) {
            str32222.printStackTrace();
            return null;
        }
    }
    public String getRequestJson(String str) throws JSONException {
        JSONObject jSONObject = new JSONObject();
        String secondTime = TimeUtil.getSecondTime();
        str = encrypt(str);
        StringBuilder stringBuilder = new StringBuilder("data=");
        stringBuilder.append(str);
        stringBuilder.append("&timestamp=");
        stringBuilder.append(secondTime);
        stringBuilder.append(this.appKey);
        String md5 = getMd5(AesCfbUtil.getSHA256StrJava(stringBuilder.toString()));
        jSONObject.put("timestamp", secondTime);
        jSONObject.put("data", str);
        jSONObject.put("sign", md5);
        return jSONObject.toString();
    }
    public String getReusltJson(String str) throws JSONException {
        JSONObject jSONObject = new JSONObject(str);
        if (jSONObject == null) {
            return null;
        }
        Object optString = jSONObject.optString("data");
        if (StringUtil.isEmpty(optString)) {
            return null;
        }
        return decrypt(optString);
    }
    private String getMd5(String str) {
        if (StringUtil.isEmpty(str)) {
            return null;
        }
        logInfo(str);
        return MD5Util.getMD5(str);
    }
    public boolean isCanEncrypt() {
        return (TextUtils.isEmpty(this.encryptKey) || TextUtils.isEmpty(this.appKey)) ? false : true;
    }
    private void logInfo(String str) {
        PrintStream printStream = System.out;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(TAG);
        stringBuilder.append(" log=");
        stringBuilder.append(str);
        printStream.print(stringBuilder.toString());
    }
}



[ttlogin]

加密的key

encryptKey=74029765cfeaf8dd791322dfd24691b4

appkey=0d27dfacef1338483561a46b246bf36d




数据对比请QQ联系

[/ttlogin]









分享到:
回复

使用道具 举报

回答|共 5 个

ShadowDOM

发表于 2020-4-24 17:53:37 | 显示全部楼层

已联系
回复

使用道具 举报

kalvin

发表于 2020-4-25 11:16:00 | 显示全部楼层

已完成,感谢
回复

使用道具 举报

chuangyi

发表于 2020-6-2 14:19:23 | 显示全部楼层

我想获得这个包的所有java源码,方便请qq 82051027联系一下
回复

使用道具 举报

jxnk25

发表于 2020-8-13 22:43:10 | 显示全部楼层

同求完整java代码,邮箱[email protected],感谢
回复

使用道具 举报

haoxp8

发表于 2020-8-29 12:16:59 | 显示全部楼层

同求php算法,付费求。[email protected]
回复

使用道具 举报