mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix test and sm2 bug
This commit is contained in:
@@ -123,7 +123,7 @@ public class SM2 extends AbstractAsymmetricCrypto<SM2> {
|
||||
if (KeyType.PublicKey != keyType) {
|
||||
throw new IllegalArgumentException("Encrypt is only support by public key");
|
||||
}
|
||||
ckeckKey(keyType);
|
||||
checkKey(keyType);
|
||||
|
||||
lock.lock();
|
||||
final SM2Engine engine = getEngine();
|
||||
@@ -149,7 +149,7 @@ public class SM2 extends AbstractAsymmetricCrypto<SM2> {
|
||||
if (KeyType.PrivateKey != keyType) {
|
||||
throw new IllegalArgumentException("Decrypt is only support by private key");
|
||||
}
|
||||
ckeckKey(keyType);
|
||||
checkKey(keyType);
|
||||
|
||||
lock.lock();
|
||||
final SM2Engine engine = getEngine();
|
||||
@@ -234,6 +234,28 @@ public class SM2 extends AbstractAsymmetricCrypto<SM2> {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public SM2 setPrivateKey(PrivateKey privateKey) {
|
||||
super.setPrivateKey(privateKey);
|
||||
|
||||
// 重新初始化密钥参数,防止重新设置密钥时导致密钥无法更新
|
||||
this.privateKeyParams = null;
|
||||
initCipherParams();
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SM2 setPublicKey(PublicKey publicKey) {
|
||||
super.setPublicKey(publicKey);
|
||||
|
||||
// 重新初始化密钥参数,防止重新设置密钥时导致密钥无法更新
|
||||
this.publicKeyParams = null;
|
||||
initCipherParams();
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置加密类型
|
||||
*
|
||||
@@ -250,7 +272,7 @@ public class SM2 extends AbstractAsymmetricCrypto<SM2> {
|
||||
|
||||
// ------------------------------------------------------------------------------------------------------------------------- Private method start
|
||||
/**
|
||||
* 初始化加密解密参数
|
||||
* 初始化加密解密参数(包括私钥和公钥参数)
|
||||
*
|
||||
* @return this
|
||||
*/
|
||||
@@ -291,7 +313,7 @@ public class SM2 extends AbstractAsymmetricCrypto<SM2> {
|
||||
*
|
||||
* @param keyType key类型
|
||||
*/
|
||||
private void ckeckKey(KeyType keyType) {
|
||||
private void checkKey(KeyType keyType) {
|
||||
switch (keyType) {
|
||||
case PublicKey:
|
||||
if (null == this.publicKey) {
|
||||
|
@@ -154,8 +154,8 @@ public class SM2Engine {
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public static enum SM2Mode {
|
||||
C1C2C3, C1C3C2;
|
||||
public enum SM2Mode {
|
||||
C1C2C3, C1C3C2
|
||||
}
|
||||
|
||||
protected ECMultiplier createBasePointMultiplier() {
|
||||
@@ -348,8 +348,7 @@ public class SM2Engine {
|
||||
/**
|
||||
* 增加字段节点
|
||||
*
|
||||
* @param digest
|
||||
* @param v
|
||||
* @param v 节点
|
||||
*/
|
||||
private void addFieldElement(ECFieldElement v) {
|
||||
final byte[] p = BigIntegers.asUnsignedByteArray(this.curveLength, v.toBigInteger());
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package cn.hutool.crypto.test;
|
||||
|
||||
import java.security.KeyPair;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
|
||||
import org.junit.Assert;
|
||||
@@ -87,17 +88,27 @@ public class SM2Test {
|
||||
@Test
|
||||
public void sm2Base64Test() {
|
||||
String textBase = "我是一段特别长的测试";
|
||||
String text = "";
|
||||
StringBuilder text = new StringBuilder();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
text += textBase;
|
||||
text.append(textBase);
|
||||
}
|
||||
|
||||
final SM2 sm2 = new SM2();
|
||||
SM2 sm2 = new SM2();
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
String encryptStr = sm2.encryptBase64(text, KeyType.PublicKey);
|
||||
String encryptStr = sm2.encryptBase64(text.toString(), KeyType.PublicKey);
|
||||
String decryptStr = StrUtil.utf8Str(sm2.decrypt(encryptStr, KeyType.PrivateKey));
|
||||
Assert.assertEquals(text, decryptStr);
|
||||
Assert.assertEquals(text.toString(), decryptStr);
|
||||
|
||||
// 测试自定义密钥后是否生效
|
||||
PrivateKey privateKey = sm2.getPrivateKey();
|
||||
PublicKey publicKey = sm2.getPublicKey();
|
||||
|
||||
sm2 = SmUtil.sm2();
|
||||
sm2.setPrivateKey(privateKey);
|
||||
sm2.setPublicKey(publicKey);
|
||||
String decryptStr2 = StrUtil.utf8Str(sm2.decrypt(encryptStr, KeyType.PrivateKey));
|
||||
Assert.assertEquals(text.toString(), decryptStr2);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Reference in New Issue
Block a user