This commit is contained in:
Looly 2024-08-14 18:01:34 +08:00
parent 143af4258d
commit 776a8cbba5
4 changed files with 29 additions and 23 deletions

View File

@ -88,13 +88,23 @@ public interface Cipher {
* @return 结果数据
*/
default byte[] processFinal(final byte[] in) {
return processFinal(in, 0, in.length);
}
/**
* 处理数据并返回最终结果
*
* @param in 输入数据
* @param inOffset 输入开始的 input中的偏移量
* @param inputLen 输入长度
* @return 结果数据
*/
default byte[] processFinal(final byte[] in, final int inOffset, final int inputLen) {
final byte[] buf = new byte[getOutputSize(in.length)];
int len = process(in, 0, in.length, buf, 0);
int len = process(in, inOffset, inputLen, buf, 0);
// 处理剩余数据如Padding数据等
len += doFinal(buf, len);
if (len == buf.length) {
return buf;
}
return Arrays.copyOfRange(buf, 0, len);
return (len == buf.length) ? buf : Arrays.copyOfRange(buf, 0, len);
}
/**

View File

@ -121,9 +121,9 @@ public class JceCipher extends SimpleWrapper<javax.crypto.Cipher> implements Cip
}
@Override
public byte[] processFinal(final byte[] data) {
public byte[] processFinal(final byte[] data, final int inOffset, final int inputLen) {
try {
return this.raw.doFinal(data);
return this.raw.doFinal(data, inOffset, inputLen);
} catch (final Exception e) {
throw new CryptoException(e);
}

View File

@ -21,9 +21,6 @@ import org.dromara.hutool.core.io.stream.FastByteArrayOutputStream;
import org.dromara.hutool.crypto.*;
import org.dromara.hutool.crypto.symmetric.SymmetricAlgorithm;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import java.io.IOException;
import java.security.*;
import java.security.spec.AlgorithmParameterSpec;
@ -47,7 +44,7 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto<AsymmetricCrypto>
/**
* Cipher负责完成加密或解密工作
*/
protected JceCipher cipher;
protected Cipher cipher;
/**
* 加密的块大小
*/
@ -253,7 +250,7 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto<AsymmetricCrypto>
final Key key = getKeyByType(keyType);
lock.lock();
try {
final JceCipher cipher = initMode(CipherMode.ENCRYPT, key);
final Cipher cipher = initMode(CipherMode.ENCRYPT, key);
if (this.encryptBlockSize < 0) {
// 在引入BC库情况下自动获取块大小
@ -278,7 +275,7 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto<AsymmetricCrypto>
final Key key = getKeyByType(keyType);
lock.lock();
try {
final JceCipher cipher = initMode(CipherMode.DECRYPT, key);
final Cipher cipher = initMode(CipherMode.DECRYPT, key);
if (this.decryptBlockSize < 0) {
// 在引入BC库情况下自动获取块大小
@ -305,7 +302,7 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto<AsymmetricCrypto>
* @since 5.4.3
*/
public Cipher getCipher() {
return this.cipher.getRaw();
return this.cipher;
}
/**
@ -323,14 +320,12 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto<AsymmetricCrypto>
* @param data 被加密或解密的内容数据
* @param maxBlockSize 最大块分段大小
* @return 加密或解密后的数据
* @throws IllegalBlockSizeException 分段异常
* @throws BadPaddingException padding错误异常
* @throws IOException IO异常不会被触发
* @throws IOException IO异常不会被触发
*/
private byte[] doFinal(final byte[] data, final int maxBlockSize) throws IllegalBlockSizeException, BadPaddingException, IOException {
private byte[] doFinal(final byte[] data, final int maxBlockSize) throws IOException {
// 不足分段
if (data.length <= maxBlockSize) {
return getCipher().doFinal(data, 0, data.length);
return this.cipher.processFinal(data, 0, data.length);
}
// 分段解密
@ -345,7 +340,7 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto<AsymmetricCrypto>
* @return 加密或解密后的数据
*/
@SuppressWarnings("resource")
private byte[] doFinalWithBlock(final byte[] data, final int maxBlockSize) throws IllegalBlockSizeException, BadPaddingException, IOException {
private byte[] doFinalWithBlock(final byte[] data, final int maxBlockSize) throws IOException {
final int dataLength = data.length;
final FastByteArrayOutputStream out = new FastByteArrayOutputStream();
@ -356,7 +351,7 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto<AsymmetricCrypto>
// 对数据分段处理
while (remainLength > 0) {
blockSize = Math.min(remainLength, maxBlockSize);
out.write(getCipher().doFinal(data, offSet, blockSize));
out.write(this.cipher.processFinal(data, offSet, blockSize));
offSet += blockSize;
remainLength = dataLength - offSet;
@ -372,8 +367,8 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto<AsymmetricCrypto>
* @param key 密钥
* @return {@link JceCipher}
*/
private JceCipher initMode(final CipherMode mode, final Key key) {
final JceCipher cipher = this.cipher;
private Cipher initMode(final CipherMode mode, final Key key) {
final Cipher cipher = this.cipher;
cipher.init(mode, new JceCipher.JceParameters(key, this.algorithmParameterSpec, this.random));
return cipher;
}

View File

@ -134,6 +134,7 @@ public class RSATest {
public void rsaWithBlockTest2() {
final RSA rsa = new RSA();
rsa.setEncryptBlockSize(3);
//rsa.setDecryptBlockSize(3);
// 获取私钥和公钥
assertNotNull(rsa.getPrivateKey());