add method

This commit is contained in:
Looly 2021-09-06 16:12:33 +08:00
parent 20a30fbc28
commit 7379e27d56
4 changed files with 38 additions and 12 deletions

View File

@ -12,6 +12,7 @@
* 【core 】 OptionalBean弃用pr#1182@Github * 【core 】 OptionalBean弃用pr#1182@Github
* 【setting】 Setting、Props持有URL改为持有Resourcepr#1182@Github * 【setting】 Setting、Props持有URL改为持有Resourcepr#1182@Github
* 【json 】 JSONUtil.toJsonStr增加重载支持JSONConfigissue#I48H5L@Gitee * 【json 】 JSONUtil.toJsonStr增加重载支持JSONConfigissue#I48H5L@Gitee
* 【crypto 】 SymmetricCrypto增加setMode方法update采用累加模式pr#1642@Gitee
### 🐞Bug修复 ### 🐞Bug修复
* 【core 】 修复ListUtil.split方法越界问题issue#I48Q0P@Gitee * 【core 】 修复ListUtil.split方法越界问题issue#I48Q0P@Gitee

View File

@ -89,4 +89,8 @@ public class Base64Test {
final String s = Base64.decodeStr(result, "gbk"); final String s = Base64.decodeStr(result, "gbk");
Assert.assertEquals(orderDescription, s); Assert.assertEquals(orderDescription, s);
} }
@Test
public void decodeEmojiTest(){
}
} }

View File

@ -235,7 +235,7 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto<AsymmetricCrypto>
final Key key = getKeyByType(keyType); final Key key = getKeyByType(keyType);
lock.lock(); lock.lock();
try { try {
initCipher(Cipher.ENCRYPT_MODE, key); initMode(Cipher.ENCRYPT_MODE, key);
if (this.encryptBlockSize < 0) { if (this.encryptBlockSize < 0) {
// 在引入BC库情况下自动获取块大小 // 在引入BC库情况下自动获取块大小
@ -267,7 +267,7 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto<AsymmetricCrypto>
final Key key = getKeyByType(keyType); final Key key = getKeyByType(keyType);
lock.lock(); lock.lock();
try { try {
initCipher(Cipher.DECRYPT_MODE, key); initMode(Cipher.DECRYPT_MODE, key);
if (this.decryptBlockSize < 0) { if (this.decryptBlockSize < 0) {
// 在引入BC库情况下自动获取块大小 // 在引入BC库情况下自动获取块大小
@ -360,14 +360,14 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto<AsymmetricCrypto>
} }
/** /**
* 初始化{@link Cipher} * 初始化{@link Cipher}的模式如加密模式或解密模式
* *
* @param mode 模式可选{@link Cipher#ENCRYPT_MODE}或者{@link Cipher#DECRYPT_MODE} * @param mode 模式可选{@link Cipher#ENCRYPT_MODE}或者{@link Cipher#DECRYPT_MODE}
* @param key 密钥 * @param key 密钥
* @throws InvalidAlgorithmParameterException 异常算法错误 * @throws InvalidAlgorithmParameterException 异常算法错误
* @throws InvalidKeyException 异常KEY错误 * @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) { if (null != this.algorithmParameterSpec) {
cipher.init(mode, key, this.algorithmParameterSpec); cipher.init(mode, key, this.algorithmParameterSpec);
} else { } else {

View File

@ -9,6 +9,7 @@ import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil; import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.CipherMode;
import cn.hutool.crypto.CryptoException; import cn.hutool.crypto.CryptoException;
import cn.hutool.crypto.KeyUtil; import cn.hutool.crypto.KeyUtil;
import cn.hutool.crypto.Padding; import cn.hutool.crypto.Padding;
@ -194,7 +195,27 @@ public class SymmetricCrypto implements Serializable {
// --------------------------------------------------------------------------------- Update // --------------------------------------------------------------------------------- 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;
}
/**
* 更新数据分组加密中间结果可以当作随机数<br>
* 第一次更新数据前需要调用{@link #setMode(CipherMode)}初始化加密或解密模式然后每次更新数据都是累加模式
* *
* @param data 被加密的bytes * @param data 被加密的bytes
* @return update之后的bytes * @return update之后的bytes
@ -203,7 +224,6 @@ public class SymmetricCrypto implements Serializable {
public byte[] update(byte[] data) { public byte[] update(byte[] data) {
lock.lock(); lock.lock();
try { try {
final Cipher cipher = initCipher(Cipher.ENCRYPT_MODE);
return cipher.update(paddingDataWithZero(data, cipher.getBlockSize())); return cipher.update(paddingDataWithZero(data, cipher.getBlockSize()));
} catch (Exception e) { } catch (Exception e) {
throw new CryptoException(e); throw new CryptoException(e);
@ -213,7 +233,8 @@ public class SymmetricCrypto implements Serializable {
} }
/** /**
* 更新数据分组加密中间结果可以当作随机数 * 更新数据分组加密中间结果可以当作随机数<br>
* 第一次更新数据前需要调用{@link #setMode(CipherMode)}初始化加密或解密模式然后每次更新数据都是累加模式
* *
* @param data 被加密的bytes * @param data 被加密的bytes
* @return update之后的hex数据 * @return update之后的hex数据
@ -234,7 +255,7 @@ public class SymmetricCrypto implements Serializable {
public byte[] encrypt(byte[] data) { public byte[] encrypt(byte[] data) {
lock.lock(); lock.lock();
try { try {
final Cipher cipher = initCipher(Cipher.ENCRYPT_MODE); final Cipher cipher = initMode(Cipher.ENCRYPT_MODE);
return cipher.doFinal(paddingDataWithZero(data, cipher.getBlockSize())); return cipher.doFinal(paddingDataWithZero(data, cipher.getBlockSize()));
} catch (Exception e) { } catch (Exception e) {
throw new CryptoException(e); throw new CryptoException(e);
@ -256,7 +277,7 @@ public class SymmetricCrypto implements Serializable {
lock.lock(); lock.lock();
CipherOutputStream cipherOutputStream = null; CipherOutputStream cipherOutputStream = null;
try { try {
final Cipher cipher = initCipher(Cipher.ENCRYPT_MODE); final Cipher cipher = initMode(Cipher.ENCRYPT_MODE);
cipherOutputStream = new CipherOutputStream(out, cipher); cipherOutputStream = new CipherOutputStream(out, cipher);
long length = IoUtil.copy(data, cipherOutputStream); long length = IoUtil.copy(data, cipherOutputStream);
if (this.isZeroPadding) { if (this.isZeroPadding) {
@ -449,7 +470,7 @@ public class SymmetricCrypto implements Serializable {
lock.lock(); lock.lock();
try { try {
final Cipher cipher = initCipher(Cipher.DECRYPT_MODE); final Cipher cipher = initMode(Cipher.DECRYPT_MODE);
blockSize = cipher.getBlockSize(); blockSize = cipher.getBlockSize();
decryptData = cipher.doFinal(bytes); decryptData = cipher.doFinal(bytes);
} catch (Exception e) { } catch (Exception e) {
@ -474,7 +495,7 @@ public class SymmetricCrypto implements Serializable {
lock.lock(); lock.lock();
CipherInputStream cipherInputStream = null; CipherInputStream cipherInputStream = null;
try { try {
final Cipher cipher = initCipher(Cipher.DECRYPT_MODE); final Cipher cipher = initMode(Cipher.DECRYPT_MODE);
cipherInputStream = new CipherInputStream(data, cipher); cipherInputStream = new CipherInputStream(data, cipher);
if (this.isZeroPadding) { if (this.isZeroPadding) {
final int blockSize = cipher.getBlockSize(); final int blockSize = cipher.getBlockSize();
@ -647,7 +668,7 @@ public class SymmetricCrypto implements Serializable {
* @throws InvalidKeyException 无效key * @throws InvalidKeyException 无效key
* @throws InvalidAlgorithmParameterException 无效算法 * @throws InvalidAlgorithmParameterException 无效算法
*/ */
private Cipher initCipher(int mode) throws InvalidKeyException, InvalidAlgorithmParameterException { private Cipher initMode(int mode) throws InvalidKeyException, InvalidAlgorithmParameterException {
final Cipher cipher = this.cipher; final Cipher cipher = this.cipher;
if (null == this.params) { if (null == this.params) {
cipher.init(mode, secretKey); cipher.init(mode, secretKey);