fix rsa block

This commit is contained in:
Looly 2020-03-02 21:48:56 +08:00
parent ba8d7b2625
commit 1ae9d35d83
3 changed files with 34 additions and 17 deletions

View File

@ -17,6 +17,7 @@
* 【core 】 增强EnumConvert判断能力issue#I17082@Gitee * 【core 】 增强EnumConvert判断能力issue#I17082@Gitee
* 【all 】 log、template、tokenizer使用SPI机制代替硬编码 * 【all 】 log、template、tokenizer使用SPI机制代替硬编码
* 【poi 】 Word07Writer增加addPicture * 【poi 】 Word07Writer增加addPicture
* 【crypto】 RSA算法中BlockSize长度策略调整issue#721@Github
### Bug修复 ### Bug修复

View File

@ -1,20 +1,19 @@
package cn.hutool.crypto.asymmetric; package cn.hutool.crypto.asymmetric;
import java.io.IOException;
import java.security.Key;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import cn.hutool.core.codec.Base64; import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FastByteArrayOutputStream; import cn.hutool.core.io.FastByteArrayOutputStream;
import cn.hutool.crypto.CryptoException; import cn.hutool.crypto.CryptoException;
import cn.hutool.crypto.SecureUtil; import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm; import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import java.io.IOException;
import java.security.Key;
import java.security.PrivateKey;
import java.security.PublicKey;
/** /**
* 非对称加密算法 * 非对称加密算法
* *
@ -196,12 +195,19 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto<AsymmetricCrypto>
@Override @Override
public byte[] encrypt(byte[] data, KeyType keyType) { public byte[] encrypt(byte[] data, KeyType keyType) {
final Key key = getKeyByType(keyType); final Key key = getKeyByType(keyType);
final int maxBlockSize = this.encryptBlockSize < 0 ? data.length : this.encryptBlockSize;
lock.lock(); lock.lock();
try { try {
cipher.init(Cipher.ENCRYPT_MODE, key); cipher.init(Cipher.ENCRYPT_MODE, key);
return doFinal(data, maxBlockSize);
if(this.encryptBlockSize < 0){
// 在引入BC库情况下自动获取块大小
final int blockSize = this.cipher.getBlockSize();
if(blockSize > 0){
this.encryptBlockSize = blockSize;
}
}
return doFinal(data, this.encryptBlockSize < 0 ? data.length : this.encryptBlockSize);
} catch (Exception e) { } catch (Exception e) {
throw new CryptoException(e); throw new CryptoException(e);
} finally { } finally {
@ -220,12 +226,19 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto<AsymmetricCrypto>
@Override @Override
public byte[] decrypt(byte[] data, KeyType keyType) { public byte[] decrypt(byte[] data, KeyType keyType) {
final Key key = getKeyByType(keyType); final Key key = getKeyByType(keyType);
final int maxBlockSize = this.decryptBlockSize < 0 ? data.length : this.decryptBlockSize;
lock.lock(); lock.lock();
try { try {
cipher.init(Cipher.DECRYPT_MODE, key); cipher.init(Cipher.DECRYPT_MODE, key);
return doFinal(data, maxBlockSize);
if(this.decryptBlockSize < 0){
// 在引入BC库情况下自动获取块大小
final int blockSize = this.cipher.getBlockSize();
if(blockSize > 0){
this.decryptBlockSize = blockSize;
}
}
return doFinal(data, this.decryptBlockSize < 0 ? data.length : this.decryptBlockSize);
} catch (Exception e) { } catch (Exception e) {
throw new CryptoException(e); throw new CryptoException(e);
} finally { } finally {

View File

@ -11,6 +11,7 @@ import java.security.spec.RSAPublicKeySpec;
import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.CryptoException; import cn.hutool.crypto.CryptoException;
import cn.hutool.crypto.GlobalBouncyCastleProvider;
import cn.hutool.crypto.SecureUtil; import cn.hutool.crypto.SecureUtil;
/** /**
@ -186,7 +187,8 @@ public class RSA extends AsymmetricCrypto {
@Override @Override
public byte[] encrypt(byte[] data, KeyType keyType) { public byte[] encrypt(byte[] data, KeyType keyType) {
if (this.encryptBlockSize < 0) { // 在非使用BC库情况下blockSize使用默认的算法
if (this.encryptBlockSize < 0 && null == GlobalBouncyCastleProvider.INSTANCE.getProvider()) {
// 加密数据长度 <= 模长-11 // 加密数据长度 <= 模长-11
this.encryptBlockSize = ((RSAKey) getKeyByType(keyType)).getModulus().bitLength() / 8 - 11; this.encryptBlockSize = ((RSAKey) getKeyByType(keyType)).getModulus().bitLength() / 8 - 11;
} }
@ -195,7 +197,8 @@ public class RSA extends AsymmetricCrypto {
@Override @Override
public byte[] decrypt(byte[] bytes, KeyType keyType) { public byte[] decrypt(byte[] bytes, KeyType keyType) {
if (this.decryptBlockSize < 0) { // 在非使用BC库情况下blockSize使用默认的算法
if (this.decryptBlockSize < 0 && null == GlobalBouncyCastleProvider.INSTANCE.getProvider()) {
// 加密数据长度 <= 模长-11 // 加密数据长度 <= 模长-11
this.decryptBlockSize = ((RSAKey) getKeyByType(keyType)).getModulus().bitLength() / 8; this.decryptBlockSize = ((RSAKey) getKeyByType(keyType)).getModulus().bitLength() / 8;
} }