|
@@ -3,6 +3,7 @@ package cn.cslg.permission.common.utils;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.crypto.Cipher;
|
|
import javax.crypto.Cipher;
|
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
import java.security.*;
|
|
import java.security.*;
|
|
import java.security.spec.PKCS8EncodedKeySpec;
|
|
import java.security.spec.PKCS8EncodedKeySpec;
|
|
import java.security.spec.X509EncodedKeySpec;
|
|
import java.security.spec.X509EncodedKeySpec;
|
|
@@ -22,6 +23,10 @@ public class RSAUtils {
|
|
|
|
|
|
//RSA密钥长度,默认密钥长度是1024,密钥长度必须是64的倍数,在512到65536位之间,不管是RSA还是RSA2长度推荐使用2048
|
|
//RSA密钥长度,默认密钥长度是1024,密钥长度必须是64的倍数,在512到65536位之间,不管是RSA还是RSA2长度推荐使用2048
|
|
private static final int KEY_SIZE = 2048;
|
|
private static final int KEY_SIZE = 2048;
|
|
|
|
+ //RSA最大加密明文大小
|
|
|
|
+ private static final int MAX_ENCRYPT_BLOCK = 245;
|
|
|
|
+ //RSA最大解密密文大小
|
|
|
|
+ private static final int MAX_DECRYPT_BLOCK = 256;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 生成密钥对
|
|
* 生成密钥对
|
|
@@ -76,9 +81,26 @@ public class RSAUtils {
|
|
//用公钥初始化此Cipher对象(加密模式)
|
|
//用公钥初始化此Cipher对象(加密模式)
|
|
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
|
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
|
//对数据加密
|
|
//对数据加密
|
|
- byte[] encrypt = cipher.doFinal(data.getBytes());
|
|
|
|
|
|
+ int inputLen = data.getBytes().length;
|
|
|
|
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
|
+ int offset = 0;
|
|
|
|
+ byte[] cache;
|
|
|
|
+ int i = 0;
|
|
|
|
+ // 对数据分段加密
|
|
|
|
+ while (inputLen - offset > 0) {
|
|
|
|
+ if (inputLen - offset > MAX_ENCRYPT_BLOCK) {
|
|
|
|
+ cache = cipher.doFinal(data.getBytes(), offset, MAX_ENCRYPT_BLOCK);
|
|
|
|
+ } else {
|
|
|
|
+ cache = cipher.doFinal(data.getBytes(), offset, inputLen - offset);
|
|
|
|
+ }
|
|
|
|
+ out.write(cache, 0, cache.length);
|
|
|
|
+ i++;
|
|
|
|
+ offset = i * MAX_ENCRYPT_BLOCK;
|
|
|
|
+ }
|
|
|
|
+ byte[] encryptedData = out.toByteArray();
|
|
|
|
+ out.close();
|
|
//返回base64编码后的字符串
|
|
//返回base64编码后的字符串
|
|
- return Base64.getEncoder().encodeToString(encrypt);
|
|
|
|
|
|
+ return Base64.getEncoder().encodeToString(encryptedData);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -102,10 +124,28 @@ public class RSAUtils {
|
|
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
|
|
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
|
|
//用私钥初始化此Cipher对象(解密模式)
|
|
//用私钥初始化此Cipher对象(解密模式)
|
|
cipher.init(Cipher.DECRYPT_MODE, privateKey);
|
|
cipher.init(Cipher.DECRYPT_MODE, privateKey);
|
|
- //对数据解密
|
|
|
|
- byte[] decrypt = cipher.doFinal(Base64.getDecoder().decode(data));
|
|
|
|
|
|
+ byte[] decodeBytes = Base64.getDecoder().decode(data);
|
|
|
|
+ //对数据分段解密
|
|
|
|
+ int inputLen = decodeBytes.length;
|
|
|
|
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
|
+ int offset = 0;
|
|
|
|
+ byte[] cache;
|
|
|
|
+ int i = 0;
|
|
|
|
+ // 对数据分段解密
|
|
|
|
+ while (inputLen - offset > 0) {
|
|
|
|
+ if (inputLen - offset > MAX_DECRYPT_BLOCK) {
|
|
|
|
+ cache = cipher.doFinal(decodeBytes, offset, MAX_DECRYPT_BLOCK);
|
|
|
|
+ } else {
|
|
|
|
+ cache = cipher.doFinal(decodeBytes, offset, inputLen - offset);
|
|
|
|
+ }
|
|
|
|
+ out.write(cache, 0, cache.length);
|
|
|
|
+ i++;
|
|
|
|
+ offset = i * MAX_DECRYPT_BLOCK;
|
|
|
|
+ }
|
|
|
|
+ byte[] decryptedData = out.toByteArray();
|
|
|
|
+ out.close();
|
|
//返回字符串
|
|
//返回字符串
|
|
- return new String(decrypt);
|
|
|
|
|
|
+ return new String(decryptedData, "UTF-8");
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|