From 776a8cbba5ba76acf13807d58d7d99a3de478a4b Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 14 Aug 2024 18:01:34 +0800 Subject: [PATCH] fix code --- .../org/dromara/hutool/crypto/Cipher.java | 20 ++++++++++---- .../org/dromara/hutool/crypto/JceCipher.java | 4 +-- .../crypto/asymmetric/AsymmetricCrypto.java | 27 ++++++++----------- .../hutool/crypto/asymmetric/RSATest.java | 1 + 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/hutool-crypto/src/main/java/org/dromara/hutool/crypto/Cipher.java b/hutool-crypto/src/main/java/org/dromara/hutool/crypto/Cipher.java index e27ac7f7d..a131ab464 100644 --- a/hutool-crypto/src/main/java/org/dromara/hutool/crypto/Cipher.java +++ b/hutool-crypto/src/main/java/org/dromara/hutool/crypto/Cipher.java @@ -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); } /** diff --git a/hutool-crypto/src/main/java/org/dromara/hutool/crypto/JceCipher.java b/hutool-crypto/src/main/java/org/dromara/hutool/crypto/JceCipher.java index 21a80bf03..da4326be5 100644 --- a/hutool-crypto/src/main/java/org/dromara/hutool/crypto/JceCipher.java +++ b/hutool-crypto/src/main/java/org/dromara/hutool/crypto/JceCipher.java @@ -121,9 +121,9 @@ public class JceCipher extends SimpleWrapper 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); } diff --git a/hutool-crypto/src/main/java/org/dromara/hutool/crypto/asymmetric/AsymmetricCrypto.java b/hutool-crypto/src/main/java/org/dromara/hutool/crypto/asymmetric/AsymmetricCrypto.java index 22f8b89d1..16fbec9aa 100644 --- a/hutool-crypto/src/main/java/org/dromara/hutool/crypto/asymmetric/AsymmetricCrypto.java +++ b/hutool-crypto/src/main/java/org/dromara/hutool/crypto/asymmetric/AsymmetricCrypto.java @@ -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 /** * Cipher负责完成加密或解密工作 */ - protected JceCipher cipher; + protected Cipher cipher; /** * 加密的块大小 */ @@ -253,7 +250,7 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto 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 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 * @since 5.4.3 */ public Cipher getCipher() { - return this.cipher.getRaw(); + return this.cipher; } /** @@ -323,14 +320,12 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto * @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 * @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 // 对数据分段处理 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 * @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; } diff --git a/hutool-crypto/src/test/java/org/dromara/hutool/crypto/asymmetric/RSATest.java b/hutool-crypto/src/test/java/org/dromara/hutool/crypto/asymmetric/RSATest.java index cf0933a1b..bba26148a 100644 --- a/hutool-crypto/src/test/java/org/dromara/hutool/crypto/asymmetric/RSATest.java +++ b/hutool-crypto/src/test/java/org/dromara/hutool/crypto/asymmetric/RSATest.java @@ -134,6 +134,7 @@ public class RSATest { public void rsaWithBlockTest2() { final RSA rsa = new RSA(); rsa.setEncryptBlockSize(3); + //rsa.setDecryptBlockSize(3); // 获取私钥和公钥 assertNotNull(rsa.getPrivateKey());