2021年9月

思想借鉴:https://www.zhihu.com/question/266180268

从严格意义上来说, 这并不是某个加密算法,能够同时允许多个密钥解密。 而是对称加密和非对称加密算法配合,实现将数据加密分发给多个人,能够让不同人使用自己的密钥解密数据

在线测试:Secret (caoxuan.top)

Demo(AES+RSA):

package t20210921;

import util.AESUtil;
import util.RSAUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Cao Xuan
 * @date 2021/9/21 17:15
 */
public class Test {
    public static void main(String[] args) throws Exception {
        String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrfQnSoLWLyob3d7PgZySgzRtAHCxyJaYs+Mqc2WeqwaxAlYV1vunmoadaVX00XzCqZej9HOxUbyxIJswrbKsK39mlj6LQ0VIVRngYgaYF5M7JsshiNnY0BCdmA39TzGtmdUDxppKQjyVT6MeUfqFO5NV9emWcrBKCad1Hql16AQIDAQAB";
        String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKt9CdKgtYvKhvd3s+BnJKDNG0AcLHIlpiz4ypzZZ6rBrECVhXW+6eahp1pVfTRfMKpl6P0c7FRvLEgmzCtsqwrf2aWPotDRUhVGeBiBpgXkzsmyyGI2djQEJ2YDf1PMa2Z1QPGmkpCPJVPox5R+oU7k1X16ZZysEoJp3UeqXXoBAgMBAAECgYBK/wrtr1TWBsNRtGrZB6KQJEL3nZqebPKolkFdqQJaPgrLz9gm71AxiZ67tclkvwB0dM8Y6q48GsZ942HE1+hQx/AUXiQH/TyTy6ZtgIV3zwNFD/fhlD57gpdxblFpoOvRrZGhwWEyut97DL8dugdBe6dMM5vBmtR3N+RG7/AsAQJBAOf2hULHYrYG45CfrxKG/dd8SVS4d2b/s192Y0WMYFkzOJcO9aRGz82Mg8q/8ET3QylYwLvdZR/rBKh1jYWtDyECQQC9QkIWh3fZWkewoIgrZim2Yu08yDTNTQGze7aZTWx1Msdlh7X5GvTbH2jxF2uM6R549rR7plDflmGt1KwXY27hAkA817agi3fi0u/BsYDUVUAj01/QrwnypENCWOU1CtsCyqKfTLc1vbUn7yFqfungzRJ6t+Ll4+5wbs6r+RmD5J7BAkEAnq4DAyipz87l9K1j/h82MflXt3KU8tzx5C6Btp83pDvi+rmCVU3Ipj/7mMhuIEMEjZ8rS9DyVcScOUbq0uFHgQJAdv7l4FlSd/MMBPGKKEe0fxJivn6+Laf3x+ZJNFOl7G5vFmzOSkA7Q8nlUebrBUpuxliGXlf06E9EQui2Z0ETVQ==";
        String secretKey = "60479f43-b296-4199-8d97-de472e1d37bd";
        String plainText = "cxnb";
        String secretText = AESUtil.encrypt(plainText, secretKey);
        System.out.println("原文:" + plainText);
        System.out.println("密文:" + secretText);
        List<String> mySecretList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            mySecretList.add(RSAUtil.encrypt(secretKey, publicKey));
        }
        System.out.println("多密钥" + mySecretList);
        mySecretList.forEach(e -> {
            System.out.println("使用密钥" + e + "解密");
            try {
                String s = AESUtil.decrypt(secretText, RSAUtil.decrypt(e, privateKey));
                System.out.println("解密成功" + s);
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        });
    }
}