diff --git a/hutool-crypto/src/main/java/org/dromara/hutool/crypto/asymmetric/SM2.java b/hutool-crypto/src/main/java/org/dromara/hutool/crypto/asymmetric/SM2.java index e9af0d279..7dc56f818 100644 --- a/hutool-crypto/src/main/java/org/dromara/hutool/crypto/asymmetric/SM2.java +++ b/hutool-crypto/src/main/java/org/dromara/hutool/crypto/asymmetric/SM2.java @@ -41,6 +41,7 @@ import org.dromara.hutool.crypto.bc.SmUtil; import java.math.BigInteger; import java.security.PrivateKey; import java.security.PublicKey; +import java.security.SecureRandom; /** * 国密SM2非对称算法实现,基于BC库
@@ -71,6 +72,10 @@ public class SM2 extends AbstractAsymmetricCrypto { private ECPrivateKeyParameters privateKeyParams; private ECPublicKeyParameters publicKeyParams; + /** + * 自定义随机数 + */ + private SecureRandom random; private DSAEncoding encoding = StandardDSAEncoding.INSTANCE; private Digest digest = new SM3Digest(); @@ -238,7 +243,7 @@ public class SM2 extends AbstractAsymmetricCrypto { if (KeyType.PublicKey != keyType) { throw new IllegalArgumentException("Encrypt is only support by public key"); } - return encrypt(data, new ParametersWithRandom(getCipherParameters(keyType))); + return encrypt(data, new ParametersWithRandom(getCipherParameters(keyType), this.random)); } /** @@ -366,7 +371,7 @@ public class SM2 extends AbstractAsymmetricCrypto { lock.lock(); final SM2Signer signer = getSigner(); try { - CipherParameters param = new ParametersWithRandom(getCipherParameters(KeyType.PrivateKey)); + CipherParameters param = new ParametersWithRandom(getCipherParameters(KeyType.PrivateKey), this.random); if (id != null) { param = new ParametersWithID(param, id); } @@ -483,6 +488,17 @@ public class SM2 extends AbstractAsymmetricCrypto { return this; } + /** + * 设置随机数生成器,可自定义随机数种子 + * + * @param random 随机数生成器,可自定义随机数种子 + * @return this + */ + public SM2 setRandom(final SecureRandom random) { + this.random = random; + return this; + } + /** * 设置DSA signatures的编码为PlainDSAEncoding * diff --git a/hutool-crypto/src/main/java/org/dromara/hutool/crypto/bc/BCCipher.java b/hutool-crypto/src/main/java/org/dromara/hutool/crypto/bc/BCCipher.java index 11403bcdb..675caf057 100644 --- a/hutool-crypto/src/main/java/org/dromara/hutool/crypto/bc/BCCipher.java +++ b/hutool-crypto/src/main/java/org/dromara/hutool/crypto/bc/BCCipher.java @@ -224,7 +224,7 @@ public class BCCipher implements Cipher, Wrapper { /** * 算法的参数 */ - private final CipherParameters parameters; + protected final CipherParameters parameters; /** * 构造