mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
sm2 & sm2 test
This commit is contained in:
1 parent
5938be8893
commit
e515af4123
11 files changed
+705
-2
No files matched your search
@@ -0,0 +1,99 @@
|
||||
package minsecurity.crypto.sm2;
|
||||
|
||||
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
|
||||
import org.bouncycastle.crypto.CryptoException;
|
||||
import org.bouncycastle.crypto.InvalidCipherTextException;
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description: 测试Sm2方法
|
||||
* @Version: 1.0.0
|
||||
* @Date: 20:15 2021/03/03
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class TestSm2 {
|
||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestSm2.class);
|
||||
@Test
|
||||
public void testSetGet() throws InvalidCipherTextException {
|
||||
AsymmetricCipherKeyPair keyPair = Sm2Base.generateKeyPairParameter();
|
||||
ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate();
|
||||
ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic();
|
||||
byte[] d = priKey.getD().toByteArray();
|
||||
byte[] x = pubKey.getQ().getAffineXCoord().getEncoded();
|
||||
byte[] y = pubKey.getQ().getAffineYCoord().getEncoded();
|
||||
logger.debug("d.len = {}, x.len = {}, y.len = {}",d.length, x.length, y.length);
|
||||
// BigInteger bigInteger = priKey.getD();
|
||||
Sm2PrivateKey sm2PrivateKey = new Sm2PrivateKey();
|
||||
Sm2PublicKey sm2PublicKey = new Sm2PublicKey();
|
||||
|
||||
sm2PrivateKey.setBytes(d);
|
||||
|
||||
ByteBuffer buffer = ByteBuffer.allocate(65);
|
||||
buffer.put((byte)4);
|
||||
buffer.put(x);
|
||||
buffer.put(y);
|
||||
sm2PublicKey.setBytes(buffer.array());
|
||||
|
||||
assertArrayEquals(d, sm2PrivateKey.getBytes());
|
||||
assertArrayEquals(buffer.array(), sm2PublicKey.getBytes());
|
||||
|
||||
String content = "hello world";
|
||||
byte[] encBytes = sm2PublicKey.encrypt(content.getBytes());
|
||||
byte[] decBytes = sm2PrivateKey.decrypt(encBytes);
|
||||
// logger.debug(new String(decBytes, StandardCharsets.UTF_8));
|
||||
assertArrayEquals(content.getBytes(), decBytes);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testEncAndDec() throws InvalidCipherTextException {
|
||||
AsymmetricCipherKeyPair keyPair = Sm2Base.generateKeyPairParameter();
|
||||
ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate();
|
||||
ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic();
|
||||
byte[] d = priKey.getD().toByteArray();
|
||||
// d = Arrays.copyOf(d,32);
|
||||
byte[] x = pubKey.getQ().getAffineXCoord().getEncoded();
|
||||
byte[] y = pubKey.getQ().getAffineYCoord().getEncoded();
|
||||
logger.debug("d.len = {}, x.len = {}, y.len = {}",d.length, x.length, y.length);
|
||||
// BigInteger bigInteger = priKey.getD();
|
||||
Sm2PrivateKey sm2PrivateKey = new Sm2PrivateKey(d);
|
||||
Sm2PublicKey sm2PublicKey = new Sm2PublicKey(x,y);
|
||||
|
||||
String content = "hello world";
|
||||
byte[] encBytes = sm2PublicKey.encrypt(content.getBytes());
|
||||
byte[] decBytes = sm2PrivateKey.decrypt(encBytes);
|
||||
// logger.debug(new String(decBytes, StandardCharsets.UTF_8));
|
||||
assertArrayEquals(content.getBytes(), decBytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSignAndVerify() throws CryptoException {
|
||||
AsymmetricCipherKeyPair keyPair = Sm2Base.generateKeyPairParameter();
|
||||
ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate();
|
||||
ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic();
|
||||
byte[] d = priKey.getD().toByteArray();
|
||||
// d = Arrays.copyOf(d,32);
|
||||
byte[] x = pubKey.getQ().getAffineXCoord().getEncoded();
|
||||
byte[] y = pubKey.getQ().getAffineYCoord().getEncoded();
|
||||
logger.debug("d.len = {}, x.len = {}, y.len = {}",d.length, x.length, y.length);
|
||||
// BigInteger bigInteger = priKey.getD();
|
||||
Sm2PrivateKey sm2PrivateKey = new Sm2PrivateKey(d);
|
||||
Sm2PublicKey sm2PublicKey = new Sm2PublicKey(x,y);
|
||||
String content = "this is hell";
|
||||
byte[] digest = sm2PrivateKey.sign(content.getBytes());
|
||||
boolean flag = sm2PublicKey.verify(content.getBytes(), digest);
|
||||
assertTrue(flag);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user