mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
identity(without persist & without test)
This commit is contained in:
1 parent
9d370a5353
commit
755ddc2129
21 files changed
+588
-144
No files matched your search
@@ -1,9 +1,9 @@
|
||||
package minsecurity.certificate.cert;
|
||||
|
||||
import minsecurity.Common;
|
||||
import minsecurity.crypto.sm2.Sm2Base;
|
||||
import minsecurity.crypto.sm2.Sm2PrivateKey;
|
||||
import minsecurity.crypto.sm2.Sm2PublicKey;
|
||||
import minsecurity.crypto.sm2.SM2Base;
|
||||
import minsecurity.crypto.sm2.SM2PrivateKey;
|
||||
import minsecurity.crypto.sm2.SM2PublicKey;
|
||||
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
@@ -22,15 +22,15 @@ public class TestCert {
|
||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestCert.class);
|
||||
@Test
|
||||
public void testCertSignAndVerify() throws Exception {
|
||||
AsymmetricCipherKeyPair keyPair = Sm2Base.generateKeyPairParameter();
|
||||
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();
|
||||
Sm2PrivateKey sm2PrivateKey = new Sm2PrivateKey(d);
|
||||
Sm2PublicKey sm2PublicKey = new Sm2PublicKey(x,y);
|
||||
SM2PrivateKey sm2PrivateKey = new SM2PrivateKey(d);
|
||||
SM2PublicKey sm2PublicKey = new SM2PublicKey(x,y);
|
||||
|
||||
|
||||
Certificate certificate = new Certificate(1,1,sm2PublicKey,
|
||||
@@ -46,15 +46,15 @@ public class TestCert {
|
||||
assertTrue(verify);
|
||||
|
||||
|
||||
AsymmetricCipherKeyPair keyPair2 = Sm2Base.generateKeyPairParameter();
|
||||
AsymmetricCipherKeyPair keyPair2 = SM2Base.generateKeyPairParameter();
|
||||
ECPrivateKeyParameters priKey2 = (ECPrivateKeyParameters) keyPair2.getPrivate();
|
||||
ECPublicKeyParameters pubKey2 = (ECPublicKeyParameters) keyPair2.getPublic();
|
||||
byte[] d2 = priKey2.getD().toByteArray();
|
||||
// d = Arrays.copyOf(d,32);
|
||||
byte[] x2 = pubKey2.getQ().getAffineXCoord().getEncoded();
|
||||
byte[] y2 = pubKey2.getQ().getAffineYCoord().getEncoded();
|
||||
Sm2PrivateKey sm2PrivateKey2 = new Sm2PrivateKey(d2);
|
||||
Sm2PublicKey sm2PublicKey2 = new Sm2PublicKey(x2, y2);
|
||||
SM2PrivateKey sm2PrivateKey2 = new SM2PrivateKey(d2);
|
||||
SM2PublicKey sm2PublicKey2 = new SM2PublicKey(x2, y2);
|
||||
|
||||
Certificate sub = new Certificate(1,2,sm2PublicKey2,
|
||||
null, Common.SM3withSM2,Common.SM2,
|
||||
@@ -69,15 +69,15 @@ public class TestCert {
|
||||
|
||||
@Test
|
||||
public void testCert2Pem() throws Exception {
|
||||
AsymmetricCipherKeyPair keyPair = Sm2Base.generateKeyPairParameter();
|
||||
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();
|
||||
Sm2PrivateKey sm2PrivateKey = new Sm2PrivateKey(d);
|
||||
Sm2PublicKey sm2PublicKey = new Sm2PublicKey(x,y);
|
||||
SM2PrivateKey sm2PrivateKey = new SM2PrivateKey(d);
|
||||
SM2PublicKey sm2PublicKey = new SM2PublicKey(x,y);
|
||||
|
||||
|
||||
Certificate certificate = new Certificate(1,1,sm2PublicKey,
|
||||
|
||||
@@ -3,7 +3,7 @@ package minsecurity.certificate.x509;
|
||||
import minsecurity.Common;
|
||||
import minsecurity.certificate.X509.X509CertMaker;
|
||||
import minsecurity.certificate.X509.X509CertUtil;
|
||||
import minsecurity.crypto.sm2.Sm2Base;
|
||||
import minsecurity.crypto.sm2.SM2Base;
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
|
||||
import org.bouncycastle.util.io.pem.PemObject;
|
||||
@@ -50,7 +50,7 @@ public class TestX509 {
|
||||
public void testRebuildKeyPair(){
|
||||
try {
|
||||
|
||||
KeyPair rootKP = Sm2Base.generateKeyPair();
|
||||
KeyPair rootKP = SM2Base.generateKeyPair();
|
||||
X509Certificate rootCA = makeRootCA(rootKP,"CN","MIN",
|
||||
"MIN-GROUP","MIN ROOT CA",
|
||||
365L * 24 * 60 * 60 * 1000, "SM3withSM2");
|
||||
@@ -77,7 +77,7 @@ public class TestX509 {
|
||||
logger.debug("origin: {}", ByteUtils.toHexString(rootKP.getPublic().getEncoded()));
|
||||
logger.debug("rebuild:{}", ByteUtils.toHexString(rootKeyPair.getPublic().getEncoded()));
|
||||
|
||||
KeyPair subKeyPair = Sm2Base.generateKeyPair();
|
||||
KeyPair subKeyPair = SM2Base.generateKeyPair();
|
||||
X509Certificate subCert =
|
||||
X509CertMaker.makeSubCACert(rootKeyPair,
|
||||
cert,
|
||||
|
||||
+14
-14
@@ -1,8 +1,8 @@
|
||||
package minsecurity.crypto;
|
||||
|
||||
import minsecurity.crypto.sm2.Sm2Base;
|
||||
import minsecurity.crypto.sm2.Sm2PrivateKey;
|
||||
import minsecurity.crypto.sm2.Sm2PublicKey;
|
||||
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;
|
||||
@@ -24,11 +24,11 @@ import java.util.List;
|
||||
* @Date: 20:15 2021/03/03
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class TestSm2 {
|
||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestSm2.class);
|
||||
public class TestSM2 {
|
||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestSM2.class);
|
||||
@Test
|
||||
public void testSetGet() throws InvalidCipherTextException {
|
||||
AsymmetricCipherKeyPair keyPair = Sm2Base.generateKeyPairParameter();
|
||||
AsymmetricCipherKeyPair keyPair = SM2Base.generateKeyPairParameter();
|
||||
ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate();
|
||||
ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic();
|
||||
byte[] d = priKey.getD().toByteArray();
|
||||
@@ -36,8 +36,8 @@ public class TestSm2 {
|
||||
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 sm2PrivateKey = new SM2PrivateKey();
|
||||
SM2PublicKey sm2PublicKey = new SM2PublicKey();
|
||||
|
||||
sm2PrivateKey.setBytes(d);
|
||||
|
||||
@@ -62,7 +62,7 @@ public class TestSm2 {
|
||||
|
||||
@Test
|
||||
public void testEncAndDec() throws InvalidCipherTextException {
|
||||
AsymmetricCipherKeyPair keyPair = Sm2Base.generateKeyPairParameter();
|
||||
AsymmetricCipherKeyPair keyPair = SM2Base.generateKeyPairParameter();
|
||||
ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate();
|
||||
ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic();
|
||||
byte[] d = priKey.getD().toByteArray();
|
||||
@@ -71,8 +71,8 @@ public class TestSm2 {
|
||||
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);
|
||||
SM2PrivateKey sm2PrivateKey = new SM2PrivateKey(d);
|
||||
SM2PublicKey sm2PublicKey = new SM2PublicKey(x,y);
|
||||
|
||||
String content = "hello world";
|
||||
byte[] encBytes = sm2PublicKey.encrypt(content.getBytes());
|
||||
@@ -83,7 +83,7 @@ public class TestSm2 {
|
||||
|
||||
@Test
|
||||
public void testSignAndVerify() throws CryptoException {
|
||||
AsymmetricCipherKeyPair keyPair = Sm2Base.generateKeyPairParameter();
|
||||
AsymmetricCipherKeyPair keyPair = SM2Base.generateKeyPairParameter();
|
||||
ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate();
|
||||
ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic();
|
||||
byte[] d = priKey.getD().toByteArray();
|
||||
@@ -93,8 +93,8 @@ public class TestSm2 {
|
||||
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);
|
||||
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);
|
||||
+11
-12
@@ -2,7 +2,6 @@ package minsecurity.crypto;
|
||||
|
||||
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -14,7 +13,7 @@ import java.util.Arrays;
|
||||
* @Date: 16:28 2021/03/04
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class TestSm4 {
|
||||
public class TestSM4 {
|
||||
public static final byte[] SRC_DATA = new byte[]{1, 2, 3, 4, 5, 6, 7, 8};
|
||||
public static final byte[] SRC_DATA_16B = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8};
|
||||
public static final byte[] SRC_DATA_24B = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8};
|
||||
@@ -25,42 +24,42 @@ public class TestSm4 {
|
||||
@Test
|
||||
public void testEncryptAndDecrypt() {
|
||||
try {
|
||||
byte[] key = Sm4.generateKey();
|
||||
byte[] iv = Sm4.generateKey();
|
||||
byte[] key = SM4.generateKey();
|
||||
byte[] iv = SM4.generateKey();
|
||||
byte[] cipherText = null;
|
||||
byte[] decryptedData = null;
|
||||
System.out.println("key: " + ByteUtils.toHexString(key));
|
||||
System.out.println("iv: " + ByteUtils.toHexString(iv));
|
||||
|
||||
cipherText = Sm4.encrypt_ECB_NoPadding(key, SRC_DATA_16B);
|
||||
cipherText = SM4.encrypt_ECB_NoPadding(key, SRC_DATA_16B);
|
||||
System.out.println("SM4 ECB NoPadding encrypt result:\n" + Arrays.toString(cipherText));
|
||||
System.out.println(ByteUtils.toHexString(cipherText));
|
||||
decryptedData = Sm4.decrypt_ECB_NoPadding(key, cipherText);
|
||||
decryptedData = SM4.decrypt_ECB_NoPadding(key, cipherText);
|
||||
System.out.println("SM4 ECB NoPadding decrypt result:\n" + Arrays.toString(decryptedData));
|
||||
if (!Arrays.equals(decryptedData, SRC_DATA_16B)) {
|
||||
Assert.fail();
|
||||
}
|
||||
|
||||
cipherText = Sm4.encrypt_ECB_Padding(key, SRC_DATA_16B);
|
||||
cipherText = SM4.encrypt_ECB_Padding(key, SRC_DATA_16B);
|
||||
System.out.println("SM4 ECB Padding encrypt result:\n" + Arrays.toString(cipherText));
|
||||
System.out.println(ByteUtils.toHexString(cipherText));
|
||||
decryptedData = Sm4.decrypt_ECB_Padding(key, cipherText);
|
||||
decryptedData = SM4.decrypt_ECB_Padding(key, cipherText);
|
||||
System.out.println("SM4 ECB Padding decrypt result:\n" + Arrays.toString(decryptedData));
|
||||
if (!Arrays.equals(decryptedData, SRC_DATA_16B)) {
|
||||
Assert.fail();
|
||||
}
|
||||
|
||||
cipherText = Sm4.encrypt_CBC_Padding(key, iv, SRC_DATA);
|
||||
cipherText = SM4.encrypt_CBC_Padding(key, iv, SRC_DATA);
|
||||
System.out.println("SM4 CBC Padding encrypt result:\n" + Arrays.toString(cipherText));
|
||||
decryptedData = Sm4.decrypt_CBC_Padding(key, iv, cipherText);
|
||||
decryptedData = SM4.decrypt_CBC_Padding(key, iv, cipherText);
|
||||
System.out.println("SM4 CBC Padding decrypt result:\n" + Arrays.toString(decryptedData));
|
||||
if (!Arrays.equals(decryptedData, SRC_DATA)) {
|
||||
Assert.fail();
|
||||
}
|
||||
|
||||
cipherText = Sm4.encrypt_CBC_NoPadding(key, iv, SRC_DATA_16B);
|
||||
cipherText = SM4.encrypt_CBC_NoPadding(key, iv, SRC_DATA_16B);
|
||||
System.out.println("SM4 CBC NoPadding encrypt result:\n" + Arrays.toString(cipherText));
|
||||
decryptedData = Sm4.decrypt_CBC_NoPadding(key, iv, cipherText);
|
||||
decryptedData = SM4.decrypt_CBC_NoPadding(key, iv, cipherText);
|
||||
System.out.println("SM4 CBC NoPadding decrypt result:\n" + Arrays.toString(decryptedData));
|
||||
if (!Arrays.equals(decryptedData, SRC_DATA_16B)) {
|
||||
Assert.fail();
|
||||
Reference in new issue
Block a user