关于java:数据加密传输

43次阅读

共计 20672 个字符,预计需要花费 52 分钟才能阅读完成。

加密、解密算法类型

看下常见的加密、解密算法类型:

对称加密:速度快,可逆,常见 DES,AES 等
非对称加密:速度慢,可逆,常见 RSA 等
签名算法:惟一,不可逆,常见 MD5,SHA,SHA256 等

平安数据传输计划

如果平安的数据传输,思考传输数据较大,则能够将几类加密、解密算法联合起来应用:
平安的数据传输计划如下:

这里依照 A、B 作为替换数据的单方,A 向 B 发送加密数据。
前提,A,B 都有本人的 RSA 公钥和私钥且 A,B 曾经替换过 RSA 公钥
A,B 应用雷同的一套加密、解密算法(对称加密 DES、非对称加密 RSA、签名算法 SHA256 举例)

A,B 应用雷同的一套加密、解密算法,能够让 A,B 应用同一套 SDK,SDK 蕴含了规范的对称加密 DES、非对称加密 RSA、签名算法 SHA256 的实现;A,B 也依照雷同的 SDK 解决加密、解密以及数据传输。

源码

不废话了,上面间接上代码:

业务数据的加密、解密:
接口:


import java.util.Map;

public interface BusinessCryptoService {

    /**
     * 依据 key 加密数据
     *
     * @param yourPublicKey 对方公钥
     * @param password      明码
     * @param data          数据
     * @return 加密数据
     */
    Map<String, String> encrypt(String yourPublicKey, String publicKey, String privateKey, String password, String data);

    /**
     * 依据 key 解密数据
     *
     * @param yourPublicKey 对方公钥
     * @param password      加密明码
     * @param data          加密数据
     * @param sign          签名数据
     * @return 解密数据
     */
    Map<String, String> decrypt(String yourPublicKey, String privateKey, String password, String data, String sign);
}

实现类:


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * 业务数据加密 / 解密
 */
@Service
public class BusinessCryptoServiceImpl implements BusinessCryptoService {

    /**
     * 对称加密
     */
    @Autowired
    private DESCryptoServiceImpl desCryptoService;

    /**
     * 非对称加密
     */
    @Autowired
    private RSACryptoServiceImpl rsaCryptoService;

    /**
     * 非对称加密反向算法
     */
    @Autowired
    private RSAReverseCryptoServiceImpl rsaReverseCryptoService;

    /**
     * 签名算法
     */
    @Autowired
    private SHA256CryptoServiceImpl sha256CryptoService;

    @Override
    public Map<String, String> encrypt(String yourPublicKey, String publicKey, String privateKey, String password, String data) {Map<String, String> encryptDataMap = new HashMap<>(16);
        byte[] passwordData = Base64Utils.encode(password.getBytes(StandardCharsets.UTF_8));
        byte[] encryptPassword = rsaCryptoService.encrypt(Base64Utils.decodeFromString(yourPublicKey), passwordData);
        // 明码公钥加密
        encryptDataMap.put("password", Base64Utils.encodeToString(encryptPassword));
        byte[] srcData = Base64Utils.encode(data.getBytes(StandardCharsets.UTF_8));
        byte[] encryptData = desCryptoService.encrypt(passwordData, srcData);
        encryptDataMap.put("data", Base64Utils.encodeToString(encryptData));
        byte[] signData = sha256CryptoService.encrypt(null, encryptData);
        byte[] encryptSignData = rsaReverseCryptoService.encrypt(Base64Utils.decodeFromString(privateKey), signData);
        encryptDataMap.put("sign", Base64Utils.encodeToString(encryptSignData));
        return encryptDataMap;
    }

    @Override
    public Map<String, String> decrypt(String yourPublicKey, String privateKey, String password, String data, String sign) {Map<String, String> decryptDataMap = new HashMap<>();
        // 接收数据签名
        byte[] decryptSignData = sha256CryptoService.encrypt(null, Base64Utils.decodeFromString(data));
        decryptDataMap.put("decryptSign", Base64Utils.encodeToString(decryptSignData));
        // 解密签名
        byte[] signData = rsaReverseCryptoService.decrypt(Base64Utils.decodeFromString(yourPublicKey), Base64Utils.decodeFromString(sign));
        decryptDataMap.put("realSign", Base64Utils.encodeToString(signData));
        // 验签
        if (!Base64Utils.encodeToString(decryptSignData).equals(Base64Utils.encodeToString(signData))) {
            // 验签不通过
            return decryptDataMap;
        }
        // 解密明码
        byte[] decyptPassword = rsaCryptoService.decrypt(Base64Utils.decodeFromString(privateKey), Base64Utils.decodeFromString(password));
        decryptDataMap.put("decyptPassword", new String(Base64Utils.decode(decyptPassword), StandardCharsets.UTF_8));
        // 解密数据
        byte[] decryptData = desCryptoService.decrypt(decyptPassword, Base64Utils.decodeFromString(data));
        decryptDataMap.put("decyptData", new String(Base64Utils.decode(decryptData), StandardCharsets.UTF_8));
        return decryptDataMap;
    }
}

通用加解密:
接口

/**
 * 加解密服务
 */
public interface CryptoService {

    /**
     * 依据 key 加密数据
     *
     * @param password 明码
     * @param data     数据
     * @return 加密数据
     */
    byte[] encrypt(byte[] password, byte[] data);

    /**
     * 依据 key 解密数据
     *
     * @param password 明码
     * @param data     数据
     * @return 解密数据
     */
    byte[] decrypt(byte[] password, byte[] data);
}

DES 对称加密、解密


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

/**
 * DES 加密 / 解密
 */
@Service
@Slf4j
public class DESCryptoServiceImpl implements CryptoService {

    /**
     * 密钥算法
     */
    private static final String KEY_ALGORITHM = "DES";

    /**
     * 加密 / 解密算法 - 工作模式 - 填充模式
     */
    private static final String CIPHER_ALGORITHM = "DES/CBC/PKCS5Padding";

    /**
     * 偏移量参数
     */
    private static final String IV_PARAMETER_SPEC = "00000000";

    public static Key generateKey(byte[] password) {
        Key key = null;
        try {if (null == password || password.length < 8) {log.error("generateKey.exception,req:{}", new String(password),
                        new InvalidKeyException("生成密钥失败,明码不能小于 8 位"));
                return null;
            }
            DESKeySpec desKeySpec = new DESKeySpec(password);
            SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
            key = secretKeyFactory.generateSecret(desKeySpec);
        } catch (InvalidKeyException e) {log.error("generateKey.exception,req:{}", new String(password), e);
        } catch (NoSuchAlgorithmException e) {log.error("generateKey.exception,req:{}", new String(password), e);
        } catch (InvalidKeySpecException e) {log.error("generateKey.exception,req:{}", new String(password), e);
        }
        return key;
    }

    @Override
    public byte[] encrypt(byte[] password, byte[] data) {byte[] encryptData = null;
        if (null == password) {log.error("encrypt.exception:password is null");
            return null;
        }
        if (null == data) {log.error("encrypt.exception:data is null");
            return null;
        }
        try {IvParameterSpec ivParameterSpec = new IvParameterSpec(IV_PARAMETER_SPEC.getBytes(StandardCharsets.UTF_8));
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, generateKey(password), ivParameterSpec);
            encryptData = cipher.doFinal(data);
        } catch (NoSuchPaddingException e) {log.error("encrypt.exception:", e);
        } catch (NoSuchAlgorithmException e) {log.error("encrypt.exception:", e);
        } catch (InvalidKeyException e) {log.error("encrypt.exception:", e);
        } catch (IllegalBlockSizeException e) {log.error("encrypt.exception:", e);
        } catch (BadPaddingException e) {log.error("encrypt.exception:", e);
        } catch (InvalidAlgorithmParameterException e) {log.error("encrypt.exception:", e);
        }
        return encryptData;
    }

    @Override
    public byte[] decrypt(byte[] password, byte[] data) {byte[] decryptData = null;
        if (null == password) {log.error("decrypt.exception:key is null");
            return null;
        }
        if (null == data) {log.error("decrypt.exception:data is null");
            return null;
        }
        try {IvParameterSpec ivParameterSpec = new IvParameterSpec(IV_PARAMETER_SPEC.getBytes(StandardCharsets.UTF_8));
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, generateKey(password), ivParameterSpec);
            decryptData = cipher.doFinal(data);
        } catch (NoSuchPaddingException e) {log.error("decrypt.exception:", e);
        } catch (IllegalBlockSizeException e) {log.error("decrypt.exception:", e);
        } catch (NoSuchAlgorithmException e) {log.error("decrypt.exception:", e);
        } catch (BadPaddingException e) {log.error("decrypt.exception:", e);
        } catch (InvalidKeyException e) {log.error("decrypt.exception:", e);
        } catch (InvalidAlgorithmParameterException e) {log.error("decrypt.exception:", e);
        }
        return decryptData;
    }
}

RSA 非对称公钥加密、私钥解密


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.ArrayList;
import java.util.List;

/**
 * REA 正向公钥加密 / 私钥解密
 */
@Service
@Slf4j
public class RSACryptoServiceImpl implements CryptoService {

    /**
     * 密钥算法
     */
    private static final String ALGORITHM = "RSA";

    public static List<Key> generateKey(int modulus) {
        List<Key> keyList = null;
        try {KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
            keyPairGenerator.initialize(modulus);
            KeyPair keyPair = keyPairGenerator.generateKeyPair();
            keyList = new ArrayList<>();
            keyList.add(keyPair.getPublic());
            keyList.add(keyPair.getPrivate());
        } catch (NoSuchAlgorithmException e) {log.error("generateKey.exception,req:{}", modulus, e);
        }
        return keyList;
    }

    public static List<byte[]> generateKeyByte(int modulus) {List<byte[]> keyStringList = null;
        List<Key> keyList = generateKey(modulus);
        if(null == keyList){return keyStringList;}
        keyStringList = new ArrayList<>();
        keyStringList.add(keyList.get(0).getEncoded());
        keyStringList.add(keyList.get(1).getEncoded());
        return keyStringList;
    }

    @Override
    public byte[] encrypt(byte[] password, byte[] data) {byte[] encryptData = null;
        try {X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(password);
            KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, keyFactory.generatePublic(x509EncodedKeySpec));
            encryptData = cipher.doFinal(data);
        } catch (NoSuchPaddingException e) {log.error("encrypt.exception:", e);
        } catch (IllegalBlockSizeException e) {log.error("encrypt.exception:", e);
        } catch (NoSuchAlgorithmException e) {log.error("encrypt.exception:", e);
        } catch (InvalidKeySpecException e) {log.error("encrypt.exception:", e);
        } catch (BadPaddingException e) {log.error("encrypt.exception:", e);
        } catch (InvalidKeyException e) {log.error("encrypt.exception:", e);
        }
        return encryptData;
    }

    @Override
    public byte[] decrypt(byte[] password, byte[] data) {byte[] decryptData = null;
        try {PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(password);
            KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, keyFactory.generatePrivate(pkcs8EncodedKeySpec));
            decryptData = cipher.doFinal(data);
        } catch (NoSuchPaddingException e) {log.error("decrypt.exception:", e);
        } catch (IllegalBlockSizeException e) {log.error("decrypt.exception:", e);
        } catch (NoSuchAlgorithmException e) {log.error("decrypt.exception:", e);
        } catch (InvalidKeySpecException e) {log.error("decrypt.exception:", e);
        } catch (BadPaddingException e) {log.error("decrypt.exception:", e);
        } catch (InvalidKeyException e) {log.error("decrypt.exception:", e);
        }
        return decryptData;
    }
}

RSA 非对称,(反向),私钥加密,公钥解密

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 * REA 反向私钥加密 / 公钥解密
 */
@Service
@Slf4j
public class RSAReverseCryptoServiceImpl implements CryptoService {

    /**
     * 密钥算法
     */
    private static final String ALGORITHM = "RSA";

    @Override
    public byte[] encrypt(byte[] password, byte[] data) {byte[] encryptData = null;
        try {PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(password);
            KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, keyFactory.generatePrivate(pkcs8EncodedKeySpec));
            encryptData = cipher.doFinal(data);
        } catch (NoSuchPaddingException e) {log.error("encrypt.exception:", e);
        } catch (IllegalBlockSizeException e) {log.error("encrypt.exception:", e);
        } catch (NoSuchAlgorithmException e) {log.error("encrypt.exception:", e);
        } catch (InvalidKeySpecException e) {log.error("encrypt.exception:", e);
        } catch (BadPaddingException e) {log.error("encrypt.exception:", e);
        } catch (InvalidKeyException e) {log.error("encrypt.exception:", e);
        }
        return encryptData;
    }

    @Override
    public byte[] decrypt(byte[] password, byte[] data) {byte[] decryptData = null;
        try {X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(password);
            KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, keyFactory.generatePublic(x509EncodedKeySpec));
            decryptData = cipher.doFinal(data);
        } catch (NoSuchPaddingException e) {log.error("decrypt.exception:", e);
        } catch (IllegalBlockSizeException e) {log.error("decrypt.exception:", e);
        } catch (NoSuchAlgorithmException e) {log.error("decrypt.exception:", e);
        } catch (InvalidKeySpecException e) {log.error("decrypt.exception:", e);
        } catch (BadPaddingException e) {log.error("decrypt.exception:", e);
        } catch (InvalidKeyException e) {log.error("decrypt.exception:", e);
        }
        return decryptData;
    }
}

SHA256 签名(加密),无解密


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * SHA256 签名
 */
@Service
@Slf4j
public class SHA256CryptoServiceImpl implements CryptoService {

    /**
     * 密钥算法
     */
    private static final String ALGORITHM = "SHA-256";

    @Override
    public byte[] encrypt(byte[] password, byte[] data) {byte[] encryptData = null;
        if (null == data || data.length <= 0) {return encryptData;}
        try {MessageDigest messageDigest = MessageDigest.getInstance(ALGORITHM);
            messageDigest.update(data);
            encryptData = messageDigest.digest();} catch (NoSuchAlgorithmException e) {log.error("encrypt.exception:", e);
        }
        return encryptData;
    }

    @Override
    public byte[] decrypt(byte[] password, byte[] data) {return null;}
}

测试验证

上面进行验证和测试:


import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Map;

/**
 * 业务数据加密 / 解密传输
 */
@SpringBootTest
@Slf4j
public class BusinessCryptoServiceImplTest {

    @Autowired
    private BusinessCryptoServiceImpl businessCryptoService;

    /**
     * A 发送数据给 B,先加密操作
     */
    @Test
    public void testEncrypt() {
        // B 公钥
        String yourPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvWeKj9gNoLMQLRGNSrFhYE6DggghW9B8JBwNzR8Dz0mwGs10QKhNM52nFIyxFEDrBtgtTpDj2UrO+NwD2+GYjyqrMEP3lYlsq4jEemls/31IPRZmcCzOBOppWeU02cB3aIcUZRE1j0FtnmijKtjMSy9o4sDqgCsvqCLFN8WoArusXuKcLMJAQ0bckcwUwDswnGjw6jvX0NdN0KyyASt6qZvSewyCa9f+gLyFo75/L65nh6rxMYNPMXCP85/q4bykYvMRAEM0rGYR9bJWsGa6tOhkig4RE2eXyn0wnJ/j8ZJibX9dHWdyeQFLAmPaLF4oRS2iUJXaQSnPeJIrZ4zkXQIDAQAB";

        // A 公钥
        String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyo5vP+mlLV0kAXyXVFKM1UkjWiUllWb0UiPv4ChdrfNuskPgWLlIjJ8ePRxNmmH5TmovQVgz97Ks+7sh+lgMmsRin7MPXDqHzmgKLVjj8b+prrRd6oRDeoe9t8YCquNkEZzi5Tr1VoDMtxh27Ie7f6crLg/MDlbXCrXvMNVHqZyZP9RL38e84JJnFo4VHWpJboO8JuHyJtxkO5+gB6N2rKjGHFRbjyvKwee6Dtaxd/fs46vi7sRLcOLen0rLtj+Cc+VvMaRMYhsjDNkuXkN/qbPC76m5RxOuVwP6cqc0aKfm6Aw78q/J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vjGJAhS1fqSXwI9oCNeK3BXJScTea8Jywd6Q04/vXV03mO9ECYUWUhrh+7iniyHHTJDzQKBgCHqmmiOUEfuefJIVFk5F3MbUEoAhVJQeH0419NQjrT+i/vd+4tC1d8VkbiO/GMktGTQrGhScA52q5p9gBPc4xFrtj53yxjjVI+ZCHVxkH3jUjOTLXEilfBa33TXHfZk4xAc+ryuJhlzEm6Nev3udIqIPUnwDvFjx9HBersS9o/JAoGBAK2hKT43ls2vyuwpqiqeYUqbJQUEMfbAusAPuPQMv2poplYMztQpK6dVNUimAuQt+vGIN8Kc8IcF8pQxj9OftmCY3KlZi7dY6cFtS80NLhJ3Xr+8PgdVEuMaAGzMNRBph9iORR8vegS50H7GkWccellSkBf6JTXOrgorncVcSaXZAoGAZw+UY76Ef+IqiOBd1vQGO46XleAvNA8aVXbZeo9nhR7MdmV3atEYKyffPoUzLD/Ip0t1BsuW4h1qgwKeXwni12qtedGX5jv7Sp+AP2xEP6ESDJWoPUomGUplHBBaLWIeYWe8Jdi15XaNg4yvuRstNe2LN5rtRFy4UdQOTLwLPJY=";

        // 明文原始明码
        String password = "12345678";

        // 明文原始数据
        String data = "HelloWorld";

        // 加密数据
        Map<String, String> encryptDataMap = businessCryptoService.encrypt(yourPublicKey, publicKey, privateKey, password, data);

        // 传输给 B 的加密数据
//        {
//            "password": "rzNIe4bF2FO3otFQSisMwn62UX3gvLPHQ7BIPPcC2ohiknMBEeSlvS004Qj0FcJgk/k8KqjMOhm1bcvWWYCPtIpAyCRCikkaxJcf8et8vJ7C2jtTUFH64XNRnjp5FNX+CT4LeheHJ+4hH/LCpA+fSPBigd0o1DqDoQR7Oi+XIQ34Z7lA84dNvoTWimKvVSAMs4MoXU5ax+KVOl/jugxABZmNlbqoWGiJO2rCW+yiAY8667q8olGFYDKximv8L45esunWg0dz6+7DPpEk0EfS5afU/WSm4yP9mESJToT/RuP+3+PLLDSg7jna75BMF3/sOtDCXzgUrDXOThRRZIKfyA==",
//            "data": "l+n0DZpcCHcg2WC2pmeDQ95+fgtiIbkl",
//            "sign": "psT9Zok//YuKG+5KKPASCYyCUxEkFnAQ3thjsfMgb2XFx/nx0VtulpPr5RC9Aof3BmdiaBuEIa0cq0quNV24OVB7GQr8oANAsPjgrYiVTBlRgeYNV3fZ6Xrv03FPrkuscozc2tRuMypUerTFHQ1cef04zRd9ll7SEyrxlbPnpMX5FyoZC0zMiBftXYC8RyEoafCSAprcAV+thPkT8LjVVyzidOk0RKowFso/SMOzfUMJZ5GihjIEzvXn3+jVVVZ840YGpmQX5KblTjPfRQ1P3f5YsNvAR2odVvwOyUUU0/RMHu2Gfu79ODf4m039vME+qLpHV6aJttvQu1ID7iRhWw==",
//        }
        log.info("testEncrypt:{}", JSON.toJSONString(encryptDataMap));
    }

    @Test
    public void testDecrypt() {
        // B 接管的加密数据
//        {
//            "password": "rzNIe4bF2FO3otFQSisMwn62UX3gvLPHQ7BIPPcC2ohiknMBEeSlvS004Qj0FcJgk/k8KqjMOhm1bcvWWYCPtIpAyCRCikkaxJcf8et8vJ7C2jtTUFH64XNRnjp5FNX+CT4LeheHJ+4hH/LCpA+fSPBigd0o1DqDoQR7Oi+XIQ34Z7lA84dNvoTWimKvVSAMs4MoXU5ax+KVOl/jugxABZmNlbqoWGiJO2rCW+yiAY8667q8olGFYDKximv8L45esunWg0dz6+7DPpEk0EfS5afU/WSm4yP9mESJToT/RuP+3+PLLDSg7jna75BMF3/sOtDCXzgUrDXOThRRZIKfyA==",
//            "data": "l+n0DZpcCHcg2WC2pmeDQ95+fgtiIbkl",
//            "sign": "psT9Zok//YuKG+5KKPASCYyCUxEkFnAQ3thjsfMgb2XFx/nx0VtulpPr5RC9Aof3BmdiaBuEIa0cq0quNV24OVB7GQr8oANAsPjgrYiVTBlRgeYNV3fZ6Xrv03FPrkuscozc2tRuMypUerTFHQ1cef04zRd9ll7SEyrxlbPnpMX5FyoZC0zMiBftXYC8RyEoafCSAprcAV+thPkT8LjVVyzidOk0RKowFso/SMOzfUMJZ5GihjIEzvXn3+jVVVZ840YGpmQX5KblTjPfRQ1P3f5YsNvAR2odVvwOyUUU0/RMHu2Gfu79ODf4m039vME+qLpHV6aJttvQu1ID7iRhWw==",
//        }

        // 加密明码
        String password = "rzNIe4bF2FO3otFQSisMwn62UX3gvLPHQ7BIPPcC2ohiknMBEeSlvS004Qj0FcJgk/k8KqjMOhm1bcvWWYCPtIpAyCRCikkaxJcf8et8vJ7C2jtTUFH64XNRnjp5FNX+CT4LeheHJ+4hH/LCpA+fSPBigd0o1DqDoQR7Oi+XIQ34Z7lA84dNvoTWimKvVSAMs4MoXU5ax+KVOl/jugxABZmNlbqoWGiJO2rCW+yiAY8667q8olGFYDKximv8L45esunWg0dz6+7DPpEk0EfS5afU/WSm4yP9mESJToT/RuP+3+PLLDSg7jna75BMF3/sOtDCXzgUrDXOThRRZIKfyA==";

        // 加密数据
        String data = "l+n0DZpcCHcg2WC2pmeDQ95+fgtiIbkl";

        // 签名
        String sign = "psT9Zok//YuKG+5KKPASCYyCUxEkFnAQ3thjsfMgb2XFx/nx0VtulpPr5RC9Aof3BmdiaBuEIa0cq0quNV24OVB7GQr8oANAsPjgrYiVTBlRgeYNV3fZ6Xrv03FPrkuscozc2tRuMypUerTFHQ1cef04zRd9ll7SEyrxlbPnpMX5FyoZC0zMiBftXYC8RyEoafCSAprcAV+thPkT8LjVVyzidOk0RKowFso/SMOzfUMJZ5GihjIEzvXn3+jVVVZ840YGpmQX5KblTjPfRQ1P3f5YsNvAR2odVvwOyUUU0/RMHu2Gfu79ODf4m039vME+qLpHV6aJttvQu1ID7iRhWw==";

        // A 公钥
        String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyo5vP+mlLV0kAXyXVFKM1UkjWiUllWb0UiPv4ChdrfNuskPgWLlIjJ8ePRxNmmH5TmovQVgz97Ks+7sh+lgMmsRin7MPXDqHzmgKLVjj8b+prrRd6oRDeoe9t8YCquNkEZzi5Tr1VoDMtxh27Ie7f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oPVNsf2eTnmwF7qgrW1uzQU3R2aSy+AOysgmtbTpy/7APEf3yTx0axOkCgYEA5iIBYJ1HSvRX3J03T5uEFnyAMwgnEhpfaIwx20HpD4wRi8fnjD02lcRbUXNGFxz8RvSwxlDymF91u6vk2bG1lYP7QDBUfmEIzdFE3zDk6eBPyHWpkJeWmc+hfUnW7DYnKcXvonEfA5KcQ/8FV7U08Td0vsG+JNn8KWVgk0Nni1UCgYARwyEBCEUBUtqMaFgwg2ieC8c6+rNMm/14ZQN4tqx0mnIS6BLnPvCF0o8G/x1COM5nuy7VVPlQo6MbYyLi3caHHOKXbTp8RfQbIQc/B6M27ynOt7mwp/fle6P49a1vPKIfZZQKJqftCHd5hrYDzKQC+Gfjfnr1bU1H8gW8f7KjCQKBgHBF5NAiyxZJUNgjp0liQLXLtauv7YfKcAOMagJjDHX2StUcCN4FOy0j57079NTv4jAnzlpiQYiPAdw2R2bhaQpxolE1gvNJGcd3shcFPpjPQ30z0i7llaaiglGihYUmAkNB+uAQOiY/D5eowg2BLTwFZyJtWPuXkYsMl2og91IlAoGBALQgrQfnsoOkv1XdRMgbtnNRKKa2FPhhE6HNoyGPXp1Z05R6DDXGSTIjyj3gE1ElyhG87njNA7sDhROYqK23wcV/ran9Af2fC23vBzxwwEVMxNrfeDOVt6S84ikTIGGyoc/iYNcKIAX51v62i8uAelpXmViHo3WTIhuB+JbFvGUb";

        // 解密数据
        Map<String, String> decryptDataMap = businessCryptoService.decrypt(publicKey, yourPrivateKey, password, data, sign);

//        {
//            "decyptPassword": "12345678",
//            "realSign": "twr5gosQ4joV0tdfzfeCNL8iUY40Z4/ul01kGmvG53A=",
//            "decryptSign": "twr5gosQ4joV0tdfzfeCNL8iUY40Z4/ul01kGmvG53A=",
//            "decyptData": "HelloWorld"
//        }
// 其中, 对密文数据做签名与解密的签名比照统一
        log.info("testDecrypt:{}", JSON.toJSONString(decryptDataMap));
    }
}

正文完
 0