mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
hash & sm4 & cert(未测试)
This commit is contained in:
1 parent
e515af4123
commit
3868d65fae
26 files changed
+1873
-15
No files matched your search
@@ -0,0 +1,114 @@
|
||||
package minsecurity.crypto;
|
||||
|
||||
import minsecurity.Common;
|
||||
import minsecurity.certificate.X509.X509CertMaker;
|
||||
import minsecurity.certificate.X509.X509CertUtil;
|
||||
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;
|
||||
import org.bouncycastle.util.io.pem.PemWriter;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import util.FileUtil;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.security.*;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.CertificateFactory;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
|
||||
import static minsecurity.certificate.X509.X509CertMaker.makeRootCA;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 22:55 2021/03/04
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class TestCert {
|
||||
static {
|
||||
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
|
||||
}
|
||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestCert.class);
|
||||
private static final Provider BC = new BouncyCastleProvider();
|
||||
|
||||
@Test
|
||||
public void testRebuildKeyPair(){
|
||||
try {
|
||||
KeyPair rootKP = Sm2Base.generateKeyPair();
|
||||
X509Certificate rootCA = makeRootCA(rootKP,"CN","MIN",
|
||||
"MIN-GROUP","MIN ROOT CA",
|
||||
365L * 24 * 60 * 60 * 1000, "SM3withSM2");
|
||||
FileUtil.writeFile("./target/root.cer",rootCA.getEncoded());
|
||||
FileUtil.writeFile("./target/root.priv",rootKP.getPrivate().getEncoded());
|
||||
rootCA.verify(rootKP.getPublic());
|
||||
|
||||
System.out.println(rootKP.getPrivate().getFormat());
|
||||
CertificateFactory fact = CertificateFactory.getInstance(Common.X509, Common.BC_NAME);
|
||||
X509Certificate cert =
|
||||
(X509Certificate) fact.generateCertificate(
|
||||
new ByteArrayInputStream(rootCA.getEncoded()));
|
||||
|
||||
PublicKey publicKey = cert.getPublicKey();
|
||||
KeyFactory keyFac = KeyFactory.getInstance("EC", BouncyCastleProvider.PROVIDER_NAME);
|
||||
byte[] privBytes = FileUtil.readFile("./target/root.priv");
|
||||
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privBytes);
|
||||
PrivateKey privateKey = keyFac.generatePrivate(keySpec);
|
||||
KeyPair rootKeyPair = new KeyPair(publicKey, privateKey);
|
||||
|
||||
logger.debug("origin: {}", ByteUtils.toHexString(rootKP.getPrivate().getEncoded()));
|
||||
logger.debug("rebuild:{}", ByteUtils.toHexString(rootKeyPair.getPrivate().getEncoded()));
|
||||
|
||||
logger.debug("origin: {}", ByteUtils.toHexString(rootKP.getPublic().getEncoded()));
|
||||
logger.debug("rebuild:{}", ByteUtils.toHexString(rootKeyPair.getPublic().getEncoded()));
|
||||
|
||||
KeyPair subKeyPair = Sm2Base.generateKeyPair();
|
||||
X509Certificate subCert =
|
||||
X509CertMaker.makeSubCACert(rootKeyPair,
|
||||
cert,
|
||||
subKeyPair,
|
||||
"c","o","ou","cn",
|
||||
365L * 24 * 60 * 60 * 1000,
|
||||
"SM3withSM2");
|
||||
FileUtil.writeFile("./target/sub.cer",subCert.getEncoded());
|
||||
|
||||
subCert.verify(rootKeyPair.getPublic());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testVerify() throws IOException, CertificateException, NoSuchProviderException {
|
||||
// 保存在本地的已有证书
|
||||
byte[] bytesOfCert = FileUtil.readFile("./target/root.cer");
|
||||
CertificateFactory fact = CertificateFactory.getInstance(Common.X509, "BC");
|
||||
X509Certificate cert =
|
||||
(X509Certificate) fact.generateCertificate(
|
||||
new ByteArrayInputStream(bytesOfCert));
|
||||
assertFalse(X509CertUtil.isExpired(cert));
|
||||
assertTrue(X509CertUtil.isValid(cert,cert.getPublicKey()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testX509toPem() throws IOException, CertificateException, NoSuchProviderException {
|
||||
byte[] bytesOfCert = FileUtil.readFile("./target/root.cer");
|
||||
CertificateFactory fact = CertificateFactory.getInstance(Common.X509, "BC");
|
||||
X509Certificate cert =
|
||||
(X509Certificate) fact.generateCertificate(
|
||||
new ByteArrayInputStream(bytesOfCert));
|
||||
StringWriter sw = new StringWriter();
|
||||
PemObject pemCSR = new PemObject("CERTIFICATE REQUEST", cert.getEncoded());
|
||||
PemWriter pemWriter = new PemWriter(sw);
|
||||
pemWriter.writeObject(pemCSR);
|
||||
pemWriter.close();
|
||||
logger.debug(sw.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
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;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description: sm4单元测试
|
||||
* @Version: 1.0.0
|
||||
* @Date: 16:28 2021/03/04
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
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};
|
||||
public static final byte[] SRC_DATA_32B = 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, 1, 2, 3, 4, 5, 6, 7, 8};
|
||||
public static final byte[] SRC_DATA_48B = 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, 1, 2, 3, 4, 5, 6, 7, 8 , 1, 2, 3, 4, 5, 6, 7, 8};
|
||||
public static final byte[] WITH_ID = new byte[]{1, 2, 3, 4};
|
||||
|
||||
@Test
|
||||
public void testEncryptAndDecrypt() {
|
||||
try {
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
System.out.println("SM4 CBC Padding encrypt result:\n" + Arrays.toString(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);
|
||||
System.out.println("SM4 CBC NoPadding encrypt result:\n" + Arrays.toString(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();
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
Assert.fail();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,10 +9,7 @@ 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
|
||||
|
||||
Reference in new issue
Block a user