From 7379e27d5617b578fdaae4aca5c4e25fc3ebea3b Mon Sep 17 00:00:00 2001 From: Looly Date: Mon, 6 Sep 2021 16:12:33 +0800 Subject: [PATCH] add method --- CHANGELOG.md | 1 + .../java/cn/hutool/core/codec/Base64Test.java | 4 ++ .../crypto/asymmetric/AsymmetricCrypto.java | 8 ++-- .../crypto/symmetric/SymmetricCrypto.java | 37 +++++++++++++++---- 4 files changed, 38 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index acf67eec4..814e56ea0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ * 【core 】 OptionalBean弃用(pr#1182@Github) * 【setting】 Setting、Props持有URL改为持有Resource(pr#1182@Github) * 【json 】 JSONUtil.toJsonStr增加重载,支持JSONConfig(issue#I48H5L@Gitee) +* 【crypto 】 SymmetricCrypto增加setMode方法,update采用累加模式(pr#1642@Gitee) ### 🐞Bug修复 * 【core 】 修复ListUtil.split方法越界问题(issue#I48Q0P@Gitee) diff --git a/hutool-core/src/test/java/cn/hutool/core/codec/Base64Test.java b/hutool-core/src/test/java/cn/hutool/core/codec/Base64Test.java index 69ac11f06..fa5b0fe7a 100644 --- a/hutool-core/src/test/java/cn/hutool/core/codec/Base64Test.java +++ b/hutool-core/src/test/java/cn/hutool/core/codec/Base64Test.java @@ -89,4 +89,8 @@ public class Base64Test { final String s = Base64.decodeStr(result, "gbk"); Assert.assertEquals(orderDescription, s); } + + @Test + public void decodeEmojiTest(){ + } } diff --git a/hutool-crypto/src/main/java/cn/hutool/crypto/asymmetric/AsymmetricCrypto.java b/hutool-crypto/src/main/java/cn/hutool/crypto/asymmetric/AsymmetricCrypto.java index 87f3d839f..4cfbf9b6a 100644 --- a/hutool-crypto/src/main/java/cn/hutool/crypto/asymmetric/AsymmetricCrypto.java +++ b/hutool-crypto/src/main/java/cn/hutool/crypto/asymmetric/AsymmetricCrypto.java @@ -235,7 +235,7 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto final Key key = getKeyByType(keyType); lock.lock(); try { - initCipher(Cipher.ENCRYPT_MODE, key); + initMode(Cipher.ENCRYPT_MODE, key); if (this.encryptBlockSize < 0) { // 在引入BC库情况下,自动获取块大小 @@ -267,7 +267,7 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto final Key key = getKeyByType(keyType); lock.lock(); try { - initCipher(Cipher.DECRYPT_MODE, key); + initMode(Cipher.DECRYPT_MODE, key); if (this.decryptBlockSize < 0) { // 在引入BC库情况下,自动获取块大小 @@ -360,14 +360,14 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto } /** - * 初始化{@link Cipher} + * 初始化{@link Cipher}的模式,如加密模式或解密模式 * * @param mode 模式,可选{@link Cipher#ENCRYPT_MODE}或者{@link Cipher#DECRYPT_MODE} * @param key 密钥 * @throws InvalidAlgorithmParameterException 异常算法错误 * @throws InvalidKeyException 异常KEY错误 */ - private void initCipher(int mode, Key key) throws InvalidAlgorithmParameterException, InvalidKeyException { + private void initMode(int mode, Key key) throws InvalidAlgorithmParameterException, InvalidKeyException { if (null != this.algorithmParameterSpec) { cipher.init(mode, key, this.algorithmParameterSpec); } else { diff --git a/hutool-crypto/src/main/java/cn/hutool/crypto/symmetric/SymmetricCrypto.java b/hutool-crypto/src/main/java/cn/hutool/crypto/symmetric/SymmetricCrypto.java index 4def26ed2..dce23fe9a 100644 --- a/hutool-crypto/src/main/java/cn/hutool/crypto/symmetric/SymmetricCrypto.java +++ b/hutool-crypto/src/main/java/cn/hutool/crypto/symmetric/SymmetricCrypto.java @@ -9,6 +9,7 @@ import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.HexUtil; import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.StrUtil; +import cn.hutool.crypto.CipherMode; import cn.hutool.crypto.CryptoException; import cn.hutool.crypto.KeyUtil; import cn.hutool.crypto.Padding; @@ -194,7 +195,27 @@ public class SymmetricCrypto implements Serializable { // --------------------------------------------------------------------------------- Update /** - * 更新数据,分组加密中间结果可以当作随机数 + * 初始化模式并清空数据 + * + * @param mode 模式枚举 + * @return this + * @since 5.7.12 + */ + public SymmetricCrypto setMode(CipherMode mode){ + lock.lock(); + try { + initMode(mode.getValue()); + } catch (Exception e) { + throw new CryptoException(e); + } finally { + lock.unlock(); + } + return this; + } + + /** + * 更新数据,分组加密中间结果可以当作随机数
+ * 第一次更新数据前需要调用{@link #setMode(CipherMode)}初始化加密或解密模式,然后每次更新数据都是累加模式 * * @param data 被加密的bytes * @return update之后的bytes @@ -203,7 +224,6 @@ public class SymmetricCrypto implements Serializable { public byte[] update(byte[] data) { lock.lock(); try { - final Cipher cipher = initCipher(Cipher.ENCRYPT_MODE); return cipher.update(paddingDataWithZero(data, cipher.getBlockSize())); } catch (Exception e) { throw new CryptoException(e); @@ -213,7 +233,8 @@ public class SymmetricCrypto implements Serializable { } /** - * 更新数据,分组加密中间结果可以当作随机数 + * 更新数据,分组加密中间结果可以当作随机数
+ * 第一次更新数据前需要调用{@link #setMode(CipherMode)}初始化加密或解密模式,然后每次更新数据都是累加模式 * * @param data 被加密的bytes * @return update之后的hex数据 @@ -234,7 +255,7 @@ public class SymmetricCrypto implements Serializable { public byte[] encrypt(byte[] data) { lock.lock(); try { - final Cipher cipher = initCipher(Cipher.ENCRYPT_MODE); + final Cipher cipher = initMode(Cipher.ENCRYPT_MODE); return cipher.doFinal(paddingDataWithZero(data, cipher.getBlockSize())); } catch (Exception e) { throw new CryptoException(e); @@ -256,7 +277,7 @@ public class SymmetricCrypto implements Serializable { lock.lock(); CipherOutputStream cipherOutputStream = null; try { - final Cipher cipher = initCipher(Cipher.ENCRYPT_MODE); + final Cipher cipher = initMode(Cipher.ENCRYPT_MODE); cipherOutputStream = new CipherOutputStream(out, cipher); long length = IoUtil.copy(data, cipherOutputStream); if (this.isZeroPadding) { @@ -449,7 +470,7 @@ public class SymmetricCrypto implements Serializable { lock.lock(); try { - final Cipher cipher = initCipher(Cipher.DECRYPT_MODE); + final Cipher cipher = initMode(Cipher.DECRYPT_MODE); blockSize = cipher.getBlockSize(); decryptData = cipher.doFinal(bytes); } catch (Exception e) { @@ -474,7 +495,7 @@ public class SymmetricCrypto implements Serializable { lock.lock(); CipherInputStream cipherInputStream = null; try { - final Cipher cipher = initCipher(Cipher.DECRYPT_MODE); + final Cipher cipher = initMode(Cipher.DECRYPT_MODE); cipherInputStream = new CipherInputStream(data, cipher); if (this.isZeroPadding) { final int blockSize = cipher.getBlockSize(); @@ -647,7 +668,7 @@ public class SymmetricCrypto implements Serializable { * @throws InvalidKeyException 无效key * @throws InvalidAlgorithmParameterException 无效算法 */ - private Cipher initCipher(int mode) throws InvalidKeyException, InvalidAlgorithmParameterException { + private Cipher initMode(int mode) throws InvalidKeyException, InvalidAlgorithmParameterException { final Cipher cipher = this.cipher; if (null == this.params) { cipher.init(mode, secretKey);