java javax.crypto.IllegalBlockSizeException: Data must not be longer than XXX bytes
今天发现用网上千篇一律的 RSA 加解密算法,待加密数据一旦比较大,就会报以上错误。查了网上一些解决方案——分段法,看起来实在有点繁琐,就想到了 Hutool 大法,还是一个字“香”~
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
/**
* @author Lux Sun
* @date 2023/9/19
*/
public class Demo {
public static void main(String[] args) {
// 公钥Key
String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0CcRgrBS89Bt7QcdrzCL+oli/xRusm4XJzL3u/tZWQ6n78fLnHA64rWQVoJRb0qCz/YMfaEO+cYjZxUO/JxwXfAQedZ7cpklCcA12FBoC02zDQhtnaTgyKEB55bOg+zPfbafWAOafvoqFlSxOsVzXCPvh/jA9T+hToIKHmub35ICAT07yRMD3CMIZdfRs3Kx7Qs2uaj1mHAM4QMS7HwPI/29OKuvgINRF0Z8nwLpooX/7JN9iE+kiZuQLy00CvqJdbLln9iF4MwT7ChUNbnxIHD8Qn30CAFFSCq0rGuWxt1zbhLfy+7REiBO9YOdLKsdA5okoYCHCXKmXkjDudflmQIDAQAB";
// 私钥Key
String privateKey = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDQJxGCsFLz0G3tBx2vMIv6iWL/FG6ybhcnMve7+1lZDqfvx8uccDritZBWglFvSoLP9gx9oQ75xiNnFQ78nHBd8BB51ntymSUJwDXYUGgLTbMNCG2dpODIoQHnls6D7M99tp9YA5p++ioWVLE6xXNcI++H+MD1P6FOggoea5vfkgIBPTvJEwPcIwhl19GzcrHtCza5qPWYcAzhAxLsfA8j/b04q6+Ag1EXRnyfAumihf/sk32IT6SJm5AvLTQK+ol1suWf2IXgzBPsKFQ1ufEgcPxCffQIAUVIKrSsa5bG3XNuEt/L7tESIE71g50sqx0DmiShgIcJcqZeSMO51+WZAgMBAAECggEBALX0w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1F9RZxK4tFchj49XjG18FZR4M8HwHuuDZjOd6Mw5+TJVMV83AR/+13g1FCBjck7Ej+Fi2pklZutH1RtX5Euvixa9AYr9vZno/8fw88N2Y3lSwof0xhjYDUPvAos1ba/v1+z9Hz3MYg==";
// 明文内容
String bigData = "123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789";
// 生产 RSA
RSA rsa = SecureUtil.rsa(privateKey, publicKey);
// 公钥加密原始数据
String encryptData = rsa.encryptBase64(bigData.getBytes(), KeyType.PublicKey);
// 私钥解密加密数据
String decryptData = rsa.decryptStr(encryptData, KeyType.PrivateKey);
// 日志
System.out.println("公钥加密原始数据: " + encryptData);
System.out.println("私钥解密加密数据: " + decryptData);
}
}
公钥加密原始数据: jzn2R1bSg80PP0nzD2kY5IzmzbZzHEJ9ISFpRYYIeZgqqI69HAjc7KeLwYdROkKbl/WNxIs3N9x9WIDeHDBnhTnVbsugqXSAQC6czRvjs2/VMJ+2hhWqKdc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74WjJ4+WoPJv6toII5H+ZCBdxLeLoKMU6Rm1oMSGzG/xY9dfVLYgJdCMyMOg4sURBLImaClXS/q6LafzERYuNbrqRkXsd+jZQ==
私钥解密加密数据: 123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789