RSA之超過128個字節的加密

記錄一下:

package com.ceshi.demo;
import java.security.MessageDigest;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
public class SignAndUnsin {
    //公私鑰模值
    static String modulus = "";
    static String exponent = "";

    public static void main(String[] args) throws Exception {
        Gson gson = new Gson();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("joininstid", "00000000");
        map.put("joininstssn", "20200622195901330");
        map.put("reqdate", "20200622");
        map.put("reqtime", "195901");
        map.put("ssotoken", "330500010000");
        map.put("reqchanneltype", "10");
        System.out.println(map.toString());
        // 開始簽名
        RSAPublicKey publicKey = RSAUtil.loadPublicKey(modulus, "10001", 16);
        // 2、公鑰加密,後臺使用私鑰解密驗籤
        String strForSign = "";
        // 真正的業務數據
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("logintype", "0");
        dataMap.put("unionid", "123456");
        dataMap.put("loginname", "ceshi");
        dataMap.put("loginpasswd", "5059be4e8d3286e89c1181a6d13f2c2c");
        dataMap.put("loginmode", "0");
        dataMap.put("devicename", "PE-TL10");
        dataMap.put("uuid", "867601025524551");
        dataMap.put("reloginflag", "1");
        Map pp = new HashMap<>();
        pp.put("accoutpassword", "123456");
        dataMap.put("attch", pp);
        for (Map.Entry<String, Object> m : getSortMap(dataMap)) {
            map.put(m.getKey(), gson.toJson(m.getValue()).replaceAll("\"", ""));
        }
        for (Map.Entry<String, Object> m : getSortMap(map)) {
            strForSign += m.getKey() + m.getValue();
        }
        String strForSignSHA1 = calc(strForSign);
        System.out.println("要簽名數據:" + strForSign);
        System.out.println("加密後數據:" + strForSignSHA1);
        byte[] cipherText = RSAUtil.publicKeyEncrypt("RSA", "ECB", "PKCS1Padding", publicKey, strForSignSHA1.getBytes("GBK"));
        System.out.println("加密:" + StringUtil.bytesToHexString(cipherText).toUpperCase());
        map.put("data", dataMap);
        map.put("sign", StringUtil.bytesToHexString(cipherText).toUpperCase());// 簽名結果
        System.out.println("簽名:" + map.get("sign"));
        //驗籤
        Unsign(map);
    }
    public static void Unsign(Map jsonObject) throws Exception {
        Gson gson = new Gson();
        Map<String, Object> signData = new HashMap<String, Object>();
        signData.put("joininstid", (String) jsonObject.get("joininstid"));
        signData.put("joininstssn", (String) jsonObject.get("joininstssn"));
        signData.put("reqdate", (String) jsonObject.get("reqdate"));
        signData.put("reqtime", (String) jsonObject.get("reqtime"));
        signData.put("ssotoken", (String) jsonObject.get("ssotoken"));
        signData.put("reqchanneltype", (String) jsonObject.get("reqchanneltype"));
        String sign = (String) jsonObject.get("sign");
        Map<String, Object> dataMap = (Map<String, Object>) jsonObject.get("data");
        RSAPrivateKey privateKey = RSAUtil.loadPrivateKey(modulus, exponent, 16);
        for (Map.Entry<String, Object> m : dataMap.entrySet()) {
            signData.put(m.getKey(), gson.toJson(m.getValue()).replaceAll("\"", ""));
        }
        String strForSign = "";
        //排序
        for (Map.Entry<String, Object> m : getSortMap(signData)) {
            strForSign += m.getKey() + m.getValue().toString();
        }
        System.out.println("要簽名數據:" + strForSign);

        byte[] plainText = RSAUtil.privateKeyDecrypt("RSA", "ECB", "PKCS1Padding", privateKey, StringUtil.hexStringToBytes(sign));
        String signDecrypt = new String(plainText, "GBK");
        String strForSignSHA1 = calc(strForSign);
        System.out.println("加密後數據:" + strForSignSHA1);
        if (!signDecrypt.equals(strForSignSHA1)) {
            System.out.println("簽名錯誤");
        } else {
            System.out.println("簽名通過");
        }
    }

    public static String calc(String decript) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.update(decript.getBytes());
            byte[] messageDigest = digest.digest();
            StringBuffer hexString = new StringBuffer();

            for (int i = 0; i < messageDigest.length; ++i) {
                String shaHex = Integer.toHexString(messageDigest[i] & 255);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }

                hexString.append(shaHex);
            }

            return hexString.toString();
        } catch (Exception var6) {
            return "ERROR";
        }
    }

    public static List<Map.Entry<String, Object>> getSortMap(Map map) {
        List<Map.Entry<String, Object>> mappingList = null;
        mappingList = new ArrayList<Map.Entry<String, Object>>(map.entrySet());
        Collections.sort(mappingList, new Comparator<Map.Entry<String, Object>>() {
            public int compare(Map.Entry<String, Object> mapping1, Map.Entry<String, Object> mapping2) {
                return mapping1.getKey().compareTo(mapping2.getKey());
            }
        });
        return mappingList;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章