mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
136 lines
6.3 KiB
Java
136 lines
6.3 KiB
Java
package minsecurity.crypto;
|
|
|
|
import minsecurity.crypto.sm2.SM2Base;
|
|
import minsecurity.crypto.sm2.SM2PrivateKey;
|
|
import minsecurity.crypto.sm2.SM2PublicKey;
|
|
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.bouncycastle.pqc.math.linearalgebra.ByteUtils;
|
|
import org.junit.Test;
|
|
import static org.junit.Assert.*;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.nio.ByteBuffer;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/*
|
|
* @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 = "7b227462734365727469666963617465223a7b2276657273696f6e223a312c2273657269616c4e756d626572223a312c227075626c69634b6579223a22424a304d7047714e5958706b695879575a4d6e5838776d6b694a51326f576d37336257615932525473706b695058342f37705443666332764e7272596e766c6a644d3448647850537531753272616363506c74485245593d222c227369676e6174757265416c676f726974686d223a302c227075626c69634b6579416c676f726974686d223a302c226973737565546f223a226973737565546f222c22697373756572223a22697373756572222c226e6f744265666f7265223a313631353032303130393339352c226e6f744166746572223a313631353032303130393539352c226b65795573616765223a322c2274696d657374616d70223a313631353032303130393339352c226361223a747275657d2c227369676e6174757265416c676f726974686d223a302c227369676e617475726556616c7565223a6e756c6c7d";
|
|
byte[] digest = sm2PrivateKey.sign(ByteUtils.fromHexString(content));
|
|
boolean flag = sm2PublicKey.verify(ByteUtils.fromHexString(content), digest);
|
|
assertTrue(flag);
|
|
}
|
|
|
|
// 按照艾宾浩斯记忆曲线
|
|
// 第x天刷完题, 需要在第x+1,x+2,x+4,x+7,x+15时重新记忆
|
|
public static void testMemory(int total, int numsInOneDay){
|
|
int days = total / numsInOneDay + 1;
|
|
List<Integer>[] res = new List[days + 15];
|
|
for(int i = 0; i < days + 15; i++){
|
|
res[i] = new ArrayList<Integer>();
|
|
}
|
|
|
|
for(int i = 0; i < days; i++){
|
|
|
|
for(int j = 1; j <= numsInOneDay; j++){
|
|
if(i * numsInOneDay + j <= total) {
|
|
res[i].add((i * numsInOneDay + j) * -1);
|
|
res[i + 1].add(i * numsInOneDay + j);
|
|
res[i + 2].add(i * numsInOneDay + j);
|
|
res[i + 4].add(i * numsInOneDay + j);
|
|
res[i + 7].add(i * numsInOneDay + j);
|
|
res[i + 15].add(i * numsInOneDay + j);
|
|
}
|
|
}
|
|
|
|
}
|
|
for(int i = 0; i < days + 15; i++){
|
|
System.out.println("第" + (i + 1) + "天: " + res[i]);
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
testMemory(75,2);
|
|
}
|
|
}
|