当加密的数据过长时,会出现javax.crypto.IllegalBlockSizeException: Data must not be longer than 117 bytes的异常。rsa算法规定一次加密的数据不能超过生成密钥对时的keyLength/8-11,keyLength一般是1024个字节,则加密的数据不能超过117个字节
/**秘钥默认长度*/
public static final int DEFAULT_KEY_SIZE = 1024;
/**加密的数据最大的字节数,即117个字节*/
public static final int DEFAULT_BUFFERSIZE = (DEFAULT_KEY_SIZE / 8) - 11;
/**当加密的数据超过DEFAULT_BUFFERSIZE,则使用分段加密*/
public static final byte[] DEFAULT_SPLIT = "#PART#".getBytes();
/** 使用公钥分段加密 */
public static byte[] encryptByPublicKeyForSpilt(byte[] data, byte[] publicKey) throws Exception{
int dataLen = data.length;
if (dataLen <= DEFAULT_BUFFERSIZE) {
return encryptByPublicKey(data, publicKey);
List<Byte allBytes = new ArrayList<Byte (2048);
int bufIndex = 0;
int subDataLoop = 0;
byte[] buf = new byte[DEFAULT_BUFFERSIZE];
for (int i = 0; i < dataLen; i++) {
buf[bufIndex] = data[i];
if (++bufIndex == DEFAULT_BUFFERSIZE || i == dataLen - 1) {
subDataLoop++;
if (subDataLoop != 1) {
for (byte b : DEFAULT_SPLIT) {
allBytes.add(b);
byte[] encryptBytes = encryptByPublicKey(buf, publicKey);
for (byte b : encryptBytes) {
allBytes.add(b);
bufIndex = 0;
if (i == dataLen - 1) {
buf = null;
} else {
buf = new byte[Math
.min(DEFAULT_BUFFERSIZE, dataLen - i - 1)];
byte[] bytes = new byte[allBytes.size()];
int i = 0;
for (Byte b : allBytes) {
bytes[i++] = b.byteValue();
return bytes;
/** 使用私钥分段解密 */
public static byte[] decryptByPrivateKeyForSpilt(byte[] encrypted, byte[] privateKey) throws Exception {
int splitLen = DEFAULT_SPLIT.length;
if (splitLen <= 0) {
return decryptByPrivateKey(encrypted, privateKey);
int dataLen = encrypted.length;
List<Byte allBytes = new ArrayList<Byte (1024);
int latestStartIndex = 0;
for (int i = 0; i < dataLen; i++) {
byte bt = encrypted[i];
boolean isMatchSplit = false;
if (i == dataLen - 1) {
// 到data的最后了
byte[] part = new byte[dataLen - latestStartIndex];
System.arraycopy(encrypted, latestStartIndex, part, 0, part.length);
byte[] decryptPart = decryptByPrivateKey(part, privateKey);
for (byte b : decryptPart) {
allBytes.add(b);
latestStartIndex = i + splitLen;
i = latestStartIndex - 1;
} else if (bt == DEFAULT_SPLIT[0]) {
// 这个是以split[0]开头
if (splitLen 1) {
if (i + splitLen < dataLen) {
// 没有超出data的范围
for (int j = 1; j < splitLen; j++) {
if (DEFAULT_SPLIT[j] != encrypted[i + j]) {
break;
if (j == splitLen - 1) {
// 验证到split的最后一位,都没有break,则表明已经确认是split段
isMatchSplit = true;
} else {
// split只有一位,则已经匹配了
isMatchSplit = true;
if (isMatchSplit) {
byte[] part = new byte[i - latestStartIndex];
System.arraycopy(encrypted, latestStartIndex, part, 0, part.length);
byte[] decryptPart = decryptByPrivateKey(part, privateKey);
for (byte b : decryptPart) {
allBytes.add(b);
latestStartIndex = i + splitLen;
i = latestStartIndex - 1;
byte[] bytes = new byte[allBytes.size()];
int i = 0;
for (Byte b : allBytes) {
bytes[i++] = b.byteValue();
return bytes;
}
测试分段加密和解密
String data = "hello world hello world hello world hello world hello world hello world hello world hello world " +
"hello world hello world hello world hello world hello world hello world hello world hello world hello world " +
"hello world hello world hello world hello world hello world hello world hello world hello world hello world ";
Log.d("TAG", "要加密的数据:" + data + ", 要加密的数据长度:" + data.length());
try {
//分段加密
byte[] encrypt = RSAUtils.encryptByPublicKeyForSpilt(data.getBytes(), publicKey);
Log.d("TAG", "加密后的数据:" + StringUtils.byteArrayToString(encrypt));
//分段解密
byte[] decrypt = RSAUtils.decryptByPrivateKeyForSpilt(encrypt, privateKey);
Log.d("TAG", "解密后的数据:" + new String(decrypt, "utf-8"));
} catch (Exception e) {
e.printStackTrace();
}