1 前言
RSA算法本身要求加密内容,也就是明文长度m,必须满足 0 < 明文长度m < 密钥长度n。
如果小于这个长度就需要进行 padding,因为如果没有padding,就无法确定解密后内容的真实长度,字符串之类的内容问题还不大,以 0 作为结束符,但对二进制数据就很难,因为不确定后面的 0 是内容还是内容结束符。而只要用到 padding,那么就要占用实际的明文长度,于是实际明文长度需要减去 padding 字节长度。我们一般使用的 padding 标准有 NoPPadding、OAEPPadding、PKCS1Padding 等,其中 PKCS#1 建议的 padding 就占用了11个字节。
这样,对于 1024 长度的密钥。128字节(1024bits)减去 11 字节正好是 117 字节,但对于 RSA 加密来讲,padding 也是参与加密的,所以,依然按照 1024bits 去理解,但实际的明文只有 117 字节了。
如果需要加密的字段过长,就会报错 Data must not be longer than 117 bytes 。
2 分段加密工具类
package com.Util;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.Key;
import java.security.KeyFactory;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class RSAUtilBlock {
/**
* 标准算法
*/
private static final String ALGORITHM = "RSA";
/**
* 加密算法
*/
private static final String CIPHER_DE = "RSA";
/**
* 解密算法
*/
private static final String CIPHER_EN = "RSA";
/**
* RSA最大加密明文大小
*/
private static final int MAX_ENCRYPT_BLOCK = 117;
/**
* RSA最大解密密文大小
*/
private static final int MAX_DECRYPT_BLOCK = 128;
/**
* 公钥加密
*
* @param data 待加密数据
* @param publicKey 公钥
* @return 加密之后的 RSA 乱码
*/
public static String encryptByPublicKey(String data, String publicKey) throws Exception {
// 待加密的数据转换成字节数组
byte[] dataBytes = data.getBytes();
// 把公钥使用 decodeBase64 进行编码,转化为字节数组
byte[] keyBytes = Base64.decodeBase64(publicKey);
// 根据给定的编码密钥创建一个新的 X509EncodedKeySpec
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes);
// 创建 RSA 密钥工厂
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
// 生成公钥对象
Key key = keyFactory.generatePublic(x509EncodedKeySpec);
// 创建 Cipher 对象
Cipher cipher = Cipher.getInstance(CIPHER_EN);
cipher.init(Cipher.ENCRYPT_MODE, key);
// 开始分段加密
int inputLength = dataBytes.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offset = 0;
byte[] cache;
int i = 0;
while (inputLength - offset > 0) {
if (inputLength - offset > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(dataBytes, offset, MAX_ENCRYPT_BLOCK);
} else {
cache = cipher.doFinal(dataBytes, offset, inputLength - offset);
}
out.write(cache, 0, cache.length);
i++;
offset = i * MAX_ENCRYPT_BLOCK;
}
byte[] encryptedBytes = out.toByteArray();
out.close();
// 将加密后的字符数组 base64 解码之后,转化为 String 返回
return new String(Base64.encodeBase64(encryptedBytes));
}
/**
* 私钥解密
*
* @param data 加密后的数据
* @param privateKey 私钥
* @return 解密之后的字符串
*/
public static String decryptByPrivateKey(String data, String privateKey) throws Exception {
// 待解密的数据用 base64 编码转化为字节数组
byte[] dataBytes = Base64.decodeBase64(data);
// 把私钥使用 decodeBase64 进行编码,转化为字节数组
byte[] keyBytes = Base64.decodeBase64(privateKey);
// 根据给定的编码密钥创建一个新的 X509EncodedKeySpec
PKCS8EncodedKeySpec pKCS8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes);
// 创建 RSA 密钥工厂
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
// 生成私钥对象
Key key = keyFactory.generatePrivate(pKCS8EncodedKeySpec);
// 创建 Cipher 对象
Cipher cipher = Cipher.getInstance(CIPHER_DE);
cipher.init(Cipher.DECRYPT_MODE, key);
// 开始分段解密
int inputLength = dataBytes.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offset = 0;
byte[] cache;
int i = 0;
while (inputLength - offset > 0) {
if (inputLength - offset > MAX_DECRYPT_BLOCK) {
cache = cipher.doFinal(dataBytes, offset, MAX_DECRYPT_BLOCK);
} else {
cache = cipher.doFinal(dataBytes, offset, inputLength - offset);
}
out.write(cache);
i++;
offset = i * MAX_DECRYPT_BLOCK;
}
String decryptStr = out.toString();
out.close();
return decryptStr;
}
}
3 使用方法
3.1 生成密钥
搜索 RSA 密钥生成就能生成对应的公钥私钥。
3.2 加密案例
import com.Util.RSAUtilBlock;
import com.alibaba.fastjson.JSONObject;
public class EnCode {
public static void main(String[] args) throws Exception {
// 公钥
String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDQkOtXgzymCTEf+i248oy3QA9qQYSuIivDAH0dBBiG3aARqbrD0aszuYLdvdYqWrXff13BOZyOdXrnOXTlHvb+DekFzTxtTjiOKGPdECr7umEFF19Ueu5umQsyMEtCKIVxLfZMVyEYIx3Ldd43BI0QBHjUrw80AS2dbvpAT/cbiwIDAQAB";
// 私钥
String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBANCQ61eDPKYJMR/6LbjyjLdAD2pBhK4iK8MAfR0EGIbdoBGpusPRqzO5gt291ipatd9/XcE5nI51euc5dOUe9v4N6QXNPG1OOI4oY90QKvu6YQUXX1R67m6ZCzIwS0IohXEt9kxXIRgjHct13jcEjRAEeNSvDzQBLZ1u+kBP9xuLAgMBAAECgYBkA4qHbaxwpmnDPtIcJVurKHH77y0z37kCisJ0XplwWui+b5ja/DlP1X7W3IjNyK/8W/RWi0zy1403Qks28BFaRu+mJwbqZEsMoiiVC5SlenJLPN5EGh8X7barY7AYBw6/tjafFd3dkmSOu+IPe3i8lTkz9e+I/pMtW9usl7JckQJBAPI0RRmPs+tT2EwwCXa35IKpa1/yG1dYn/BnCTFb7XCC8kdA+60JvLw07JkXHTshghXxKeD37071e4/cXc3JyRkCQQDccibFqxmHu8lDDOM/DJ+YS9++wxCFRM3tXO6QCpTG0Aacj9mPTtv/pLzPIpV8Sxe8UhL3DWmUaYNYOqkTwIpDAkBddsI7jPK3GxHaxKbCQ1BRA6PF0J2wZSjDEl+/xmOoCUAJMxBrw9grIJk0snC7QsD6SqgIgmS8qYpXB4jDQtzxAkEAqwbQ99iX1QumyKNOBtNnWkO7UgeKYKqRzU6lMxFSfbBVANBFrP40hnCrn1Qof6bOwmc2mX9JY0A9C41vMYjWewJBAIt4rlJlP7qxWZCBRTViXiBmRxM6/6JWY9EA2vb220Pfz/1jp/kYb/0EiGE6aFreiMSFYQGq4L9WOjClVQ8D15Y=";
System.out.println("==========加密身份证信息==========");
JSONObject idCardJson = new JSONObject();
idCardJson.put("idcard", "350123197007072434");
idCardJson.put("idcard1", "350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434");
idCardJson.put("idcard2", "350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434");
idCardJson.put("idcard3", "350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434");
idCardJson.put("idcard4", "350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434");
idCardJson.put("idcard5", "350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434");
idCardJson.put("idcard6", "350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434");
idCardJson.put("idcard7", "350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434");
idCardJson.put("idcard8", "350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434");
idCardJson.put("idcard9", "350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434");
idCardJson.put("idcard11", "350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434");
idCardJson.put("idcard22", "350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434");
idCardJson.put("idcard33", "350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434");
idCardJson.put("idcard44", "350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434");
idCardJson.put("idcard55", "350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434");
idCardJson.put("idcard66", "350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434");
idCardJson.put("idcard77", "350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434");
idCardJson.put("idcard88", "350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434");
idCardJson.put("idcard99", "350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434");
idCardJson.put("idcard00", "350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434");
String idCardString = idCardJson.toJSONString();
System.out.println("原始身份证数据=" + idCardString);
String encryptStr = RSAUtilBlock.encryptByPublicKey(idCardString, publicKey);
System.out.println("加密后=" + encryptStr);
System.out.println("加密后长度=" + encryptStr.length());
JSONObject json = new JSONObject();
json.put("data", encryptStr);
System.out.println("最终传输密文=" + json);
System.out.println("==========加密成功==========");
}
}
控制台打印出的信息:
==========加密身份证信息==========
原始身份证数据={"idcard11":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard33":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard55":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard77":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard99":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard2":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard1":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard6":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard5":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard4":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard3":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard9":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard8":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard7":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard22":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard44":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard66":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard88":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard00":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard":"350123197007072434"}
加密后=TW/85+d+CCG04yoJCnjpKhBYqANq4WkITVboJcZ74Iz1FLN5Kr+8VIknHUNpl+17gDfeORxrSMoIjHIq8PLzg/M56Dee1jHkk4nZljtFAPX/uhDPmddDKsUMMDNWNliUueAu39jJy3eOQKodjzlTe9xjl3WMIgInwu5Bkkc+Xy5ik/OnzklnlwF10UQ2IpMhqMMRvgYQiFTnHOUmMjxjv9aKkF1V6Q1CpaYhqC0V3fWgbRvntlUoZpiIwZJ2opomFLbuMN3zN71GkFX3oPTs5UgaY8JaPJb79hzw7tATMjDqE566RxqVx9Ykil/Zr861WtrkzGWzregIxa9oyVift3FJhs6yhx9i/v3pMZmAGGFTZadeCu+KsXUVUk60J3bEK6Iz50Q+pjor42X/ULEK0OLfDewzDwQ4ZQdrpICObT0ylzJEFN25ghlY23niB6O4pWHhKRP3W0eQlSYtWlighbxyaCTvjS6mK3qqCTOFqlHyyUuY2zN11gEtKv7mrHVgpBoyi70AvJW5rR11pfUgSBJdtquaCjKF6n1P4a9FYXXXJZlQBuTiVHh8wUH/KgFsJ8kufGJEZ1ZdRaepDTo+1jxni7e1s/gMVmOYqoa0D+j/nCIRAs+rtDlX2h6mTzZ4MK7bNyMGDxwI/hsu5Nq5l1SquLSgxXiD8AH/EhFNlHa2TjNYj6O+ZKLNqlIcrxJXBRVde5P/6s/N0BZYYb7hwKiz++x0rNmU5MoGPeFMMz0oX1kg9zGX0gnZnR5HqKmfQCsFuZB2sVecPLbrMqtq7Vr7QgUfXVoC0JyD5Os9sJ13E3u8sFaDd7TrMkh6+3xzAYFDLgw78JSfOmybU+6IUCBV2N95sRvKn7dBXTtpJ8xT4lxrMImQx+OmufmWY2bzy/oL2f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9YVhGAsNvjdnj1jWn/jUU1d9ToDfAly/aX4Vt+luXxuaTSPh+y9iAYnvKnFtE27WsfevP/yvhg9m2TMsSE4LBwjtuW1PuNDnHZ/1jfllUUihEi2HCE2BhWn7uArLBO4BgcrxGVa1xMGbNQ8TrAlXwYrSEgYTsjq2ztdW0ei/0rIgiYRWPaYYreBiWyLzxC6xH0/DBphaAnRRySDPk8ghKlku21hJPxWZQt7GfaQuVz964zWYaY="}
==========加密成功==========
Process finished with exit code 0
3.3 解密案例
import com.Util.RSAUtilBlock;
public class LnDeCode {
public static void main(String[] args) throws Exception {
// 公钥
String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDQkOtXgzymCTEf+i248oy3QA9qQYSuIivDAH0dBBiG3aARqbrD0aszuYLdvdYqWrXff13BOZyOdXrnOXTlHvb+DekFzTxtTjiOKGPdECr7umEFF19Ueu5umQsyMEtCKIVxLfZMVyEYIx3Ldd43BI0QBHjUrw80AS2dbvpAT/cbiwIDAQAB";
// 私钥
String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBANCQ61eDPKYJMR/6LbjyjLdAD2pBhK4iK8MAfR0EGIbdoBGpusPRqzO5gt291ipatd9/XcE5nI51euc5dOUe9v4N6QXNPG1OOI4oY90QKvu6YQUXX1R67m6ZCzIwS0IohXEt9kxXIRgjHct13jcEjRAEeNSvDzQBLZ1u+kBP9xuLAgMBAAECgYBkA4qHbaxwpmnDPtIcJVurKHH77y0z37kCisJ0XplwWui+b5ja/DlP1X7W3IjNyK/8W/RWi0zy1403Qks28BFaRu+mJwbqZEsMoiiVC5SlenJLPN5EGh8X7barY7AYBw6/tjafFd3dkmSOu+IPe3i8lTkz9e+I/pMtW9usl7JckQJBAPI0RRmPs+tT2EwwCXa35IKpa1/yG1dYn/BnCTFb7XCC8kdA+60JvLw07JkXHTshghXxKeD37071e4/cXc3JyRkCQQDccibFqxmHu8lDDOM/DJ+YS9++wxCFRM3tXO6QCpTG0Aacj9mPTtv/pLzPIpV8Sxe8UhL3DWmUaYNYOqkTwIpDAkBddsI7jPK3GxHaxKbCQ1BRA6PF0J2wZSjDEl+/xmOoCUAJMxBrw9grIJk0snC7QsD6SqgIgmS8qYpXB4jDQtzxAkEAqwbQ99iX1QumyKNOBtNnWkO7UgeKYKqRzU6lMxFSfbBVANBFrP40hnCrn1Qof6bOwmc2mX9JY0A9C41vMYjWewJBAIt4rlJlP7qxWZCBRTViXiBmRxM6/6JWY9EA2vb220Pfz/1jp/kYb/0EiGE6aFreiMSFYQGq4L9WOjClVQ8D15Y=";
System.out.println("==========解析身份证信息==========");
String data = "TW/85+d+CCG04yoJCnjpKhBYqANq4WkITVboJcZ74Iz1FLN5Kr+8VIknHUNpl+17gDfeORxrSMoIjHIq8PLzg/M56Dee1jHkk4nZljtFAPX/uhDPmddDKsUMMDNWNliUueAu39jJy3eOQKodjzlTe9xjl3WMIgInwu5Bkkc+Xy5ik/OnzklnlwF10UQ2IpMhqMMRvgYQiFTnHOUmMjxjv9aKkF1V6Q1CpaYhqC0V3fWgbRvntlUoZpiIwZJ2opomFLbuMN3zN71GkFX3oPTs5UgaY8JaPJb79hzw7tATMjDqE566RxqVx9Ykil/Zr861WtrkzGWzregIxa9oyVift3FJhs6yhx9i/v3pMZmAGGFTZadeCu+KsXUVUk60J3bEK6Iz50Q+pjor42X/ULEK0OLfDewzDwQ4ZQdrpICObT0ylzJEFN25ghlY23niB6O4pWHhKRP3W0eQlSYtWlighbxyaCTvjS6mK3qqCTOFqlHyyUuY2zN11gEtKv7mrHVgpBoyi70AvJW5rR11pfUgSBJdtquaCjKF6n1P4a9FYXXXJZlQBuTiVHh8wUH/KgFsJ8kufGJEZ1ZdRaepDTo+1jxni7e1s/gMVmOYqoa0D+j/nCIRAs+rtDlX2h6mTzZ4MK7bNyMGDxwI/hsu5Nq5l1SquLSgxXiD8AH/EhFNlHa2TjNYj6O+ZKLNqlIcrxJXBRVde5P/6s/N0BZYYb7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Wps7Xx9UPMnlz8G5QbumldQcZlpkamx2QRITu5llG4IC4LmYbXshLI9kz+TwrkmSjReEVH/dity7uq8YrIa1IVxuK1KmWE+oOOBItaIaPr7H3YXHSvM5CWIesAfIIWRytrlwQMihVR9s0r9YVhGAsNvjdnj1jWn/jUU1d9ToDfAly/aX4Vt+luXxuaTSPh+y9iAYnvKnFtE27WsfevP/yvhg9m2TMsSE4LBwjtuW1PuNDnHZ/1jfllUUihEi2HCE2BhWn7uArLBO4BgcrxGVa1xMGbNQ8TrAlXwYrSEgYTsjq2ztdW0ei/0rIgiYRWPaYYreBiWyLzxC6xH0/DBphaAnRRySDPk8ghKlku21hJPxWZQt7GfaQuVz964zWYaY=";
String idCardString = RSAUtilBlock.decryptByPrivateKey(data, privateKey);
System.out.println("解密后=" + idCardString);
System.out.println("解密后长度=" + idCardString.length());
System.out.println("==========解密成功==========");
}
}
控制台打印的信息:
==========解析身份证信息==========
解密后={"idcard11":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard33":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard55":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard77":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard99":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard2":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard1":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard6":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard5":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard4":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard3":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard9":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard8":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard7":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard22":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard44":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard66":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard88":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard00":"350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434350123197007072434","idcard":"350123197007072434"}
解密后长度=2178
==========解密成功==========
Process finished with exit code 0