欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

RSA之超过128个字节的加密

程序员文章站 2024-03-16 18:15:40
...

记录一下:

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;
    }
}

相关标签: 记录 java

上一篇: CSS盒模型

下一篇: