mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
将VMSConnection模块加入min-dev-java,以支持与VMS进行通信,执行注册、登录等功能
This commit is contained in:
1 parent
a26f908470
commit
ba105bdc3c
36 files changed
+2961
-2
No files matched your search
@@ -0,0 +1,14 @@
|
||||
package VMSConnection.Security.SM2;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 10:51 2021/03/12
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class AsymKeyException extends Exception{
|
||||
public AsymKeyException(String msg){
|
||||
super(msg);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package VMSConnection.Security.SM2;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description: 归纳所用常用变量
|
||||
* @Version: 1.0.0
|
||||
* @Date: 16:40 2021/03/04
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class Common {
|
||||
public static final String BC_NAME = "BC";
|
||||
public static final String X509 = "X.509";
|
||||
public static final String ALGO_NAME_EC = "EC";
|
||||
|
||||
/* Storage */
|
||||
public static final int SQLITE = 0;
|
||||
/* PublicKeyAlgorithm */
|
||||
public static final int SM2 = 0;
|
||||
/* SignatureAlgorithm */
|
||||
public static final int SM3withSM2 = 0;
|
||||
/* SymmetricAlgorithm */
|
||||
public static final int SM4ECB = 0;
|
||||
public static final int SM4CBC = 1;
|
||||
/* KeyUsage */
|
||||
public static final int ContentCommitment = 0;
|
||||
public static final int DataEncipherment = 1;
|
||||
public static final int CertSign = 2;
|
||||
/* tpyeOfCert */
|
||||
public static final int InnerCertificate = 0;
|
||||
public static final int Certificate = 1;
|
||||
public static final int TbsCertificate = 2;
|
||||
// public enum Storage {
|
||||
// SQLITE;
|
||||
// }
|
||||
// public enum PublicKeyAlgorithm {
|
||||
// SM2;
|
||||
// }
|
||||
// public enum SignatureAlgorithm {
|
||||
// SM3withSM2
|
||||
// }
|
||||
// public enum SymmetricAlgorithm {
|
||||
// SM4ECB,
|
||||
// SM4CBC;
|
||||
// }
|
||||
// public enum KeyUsage {
|
||||
// ContentCommitment,
|
||||
// DataEncipherment,
|
||||
// CertSign;
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package VMSConnection.Security.SM2;
|
||||
|
||||
import org.bouncycastle.crypto.CryptoException;
|
||||
import org.bouncycastle.crypto.InvalidCipherTextException;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description: 所有对称加密方法必须实现的基本方法
|
||||
* @Version: 1.0.0
|
||||
* @Date: 17:28 2021/03/03
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public interface PrivateKeyInterface {
|
||||
public byte[] getBytes();
|
||||
public boolean setBytes(byte[] bytes);
|
||||
public byte[] decrypt(byte[] cipher) throws InvalidCipherTextException;
|
||||
public byte[] sign(byte[] content) throws CryptoException;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package VMSConnection.Security.SM2;
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description: 所有对称加密方法必须实现的基本方法
|
||||
* @Version: 1.0.0
|
||||
* @Date: 17:06 2021/03/03
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
|
||||
import org.bouncycastle.crypto.InvalidCipherTextException;
|
||||
|
||||
public interface PublicKeyInterface {
|
||||
public byte[] getBytes();
|
||||
public boolean setBytes(byte[] bytes);
|
||||
public byte[] encrypt(byte[] content) throws InvalidCipherTextException;
|
||||
public boolean verify(byte[] content, byte[] digest);
|
||||
}
|
||||
@@ -0,0 +1,219 @@
|
||||
package VMSConnection.Security.SM2;
|
||||
|
||||
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
|
||||
import org.bouncycastle.crypto.generators.ECKeyPairGenerator;
|
||||
import org.bouncycastle.crypto.params.ECDomainParameters;
|
||||
import org.bouncycastle.crypto.params.ECKeyGenerationParameters;
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
import org.bouncycastle.crypto.util.PrivateKeyInfoFactory;
|
||||
import org.bouncycastle.crypto.util.SubjectPublicKeyInfoFactory;
|
||||
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;
|
||||
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
import org.bouncycastle.jce.spec.ECParameterSpec;
|
||||
import org.bouncycastle.math.ec.ECPoint;
|
||||
import org.bouncycastle.math.ec.custom.gm.SM2P256V1Curve;
|
||||
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.security.*;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
|
||||
import static VMSConnection.Security.SM2.Common.ALGO_NAME_EC;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description: SM2推荐曲线参数
|
||||
* @Version: 1.0.0
|
||||
* @Date: 18:48 2021/03/03
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class SM2Base {
|
||||
|
||||
static {
|
||||
Security.addProvider(new BouncyCastleProvider());
|
||||
}
|
||||
|
||||
// private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SM2Base.class);
|
||||
|
||||
/*
|
||||
* 以下为SM2推荐曲线参数
|
||||
*/
|
||||
public static final SM2P256V1Curve CURVE = new SM2P256V1Curve();
|
||||
public final static BigInteger SM2_ECC_P = CURVE.getQ();
|
||||
public final static BigInteger SM2_ECC_A = CURVE.getA().toBigInteger();
|
||||
public final static BigInteger SM2_ECC_B = CURVE.getB().toBigInteger();
|
||||
public final static BigInteger SM2_ECC_N = CURVE.getOrder();
|
||||
public final static BigInteger SM2_ECC_H = CURVE.getCofactor();
|
||||
public final static BigInteger SM2_ECC_GX = new BigInteger(
|
||||
"32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7", 16);
|
||||
public final static BigInteger SM2_ECC_GY = new BigInteger(
|
||||
"BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0", 16);
|
||||
public static final ECPoint G_POINT = CURVE.createPoint(SM2_ECC_GX, SM2_ECC_GY);
|
||||
public static final ECDomainParameters DOMAIN_PARAMS = new ECDomainParameters(CURVE, G_POINT,
|
||||
SM2_ECC_N, SM2_ECC_H);
|
||||
|
||||
private static final String PEM_STRING_PUBLIC = "PUBLIC KEY";
|
||||
private static final String PEM_STRING_ECPRIVATEKEY = "EC PRIVATE KEY";
|
||||
|
||||
|
||||
/**
|
||||
* 生成BC自定义的SM2公私钥
|
||||
* @param
|
||||
* @return org.bouncycastle.crypto.AsymmetricCipherKeyPair
|
||||
* @author hongyu guo
|
||||
* @date 2021/3/7
|
||||
**/
|
||||
public static AsymmetricCipherKeyPair generateKeyPairParameter(){
|
||||
SecureRandom random = new SecureRandom();
|
||||
ECKeyGenerationParameters keyGenerationParams = new ECKeyGenerationParameters(DOMAIN_PARAMS,
|
||||
random);
|
||||
ECKeyPairGenerator keyGen = new ECKeyPairGenerator();
|
||||
keyGen.init(keyGenerationParams);
|
||||
AsymmetricCipherKeyPair keyPair = keyGen.generateKeyPair();
|
||||
return keyPair;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成java.security.KeyPair类型的SM2公私钥
|
||||
* @param
|
||||
* @return java.security.KeyPair
|
||||
* @author hongyu guo
|
||||
* @date 2021/3/7
|
||||
**/
|
||||
public static KeyPair generateKeyPair() throws NoSuchProviderException, NoSuchAlgorithmException,
|
||||
InvalidAlgorithmParameterException {
|
||||
SecureRandom random = new SecureRandom();
|
||||
return generateKeyPair(DOMAIN_PARAMS, random);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成java.security.KeyPair类型的SM2公私钥, 需自定义曲线参数
|
||||
* @param domainParameters
|
||||
* @param random
|
||||
* @return
|
||||
* @author hongyu guo
|
||||
* @date 2021/3/7
|
||||
**/
|
||||
public static KeyPair generateKeyPair(ECDomainParameters domainParameters, SecureRandom random)
|
||||
throws NoSuchProviderException, NoSuchAlgorithmException,
|
||||
InvalidAlgorithmParameterException {
|
||||
KeyPairGenerator kpg = KeyPairGenerator.getInstance(ALGO_NAME_EC, BouncyCastleProvider.PROVIDER_NAME);
|
||||
ECParameterSpec parameterSpec = new ECParameterSpec(domainParameters.getCurve(), domainParameters.getG(),
|
||||
domainParameters.getN(), domainParameters.getH());
|
||||
kpg.initialize(parameterSpec, random);
|
||||
return kpg.generateKeyPair();
|
||||
}
|
||||
|
||||
/**
|
||||
* BC公私钥转换为java.security.KeyPair公私钥
|
||||
* @param bcKeyPair
|
||||
* @return
|
||||
* @author hongyu guo
|
||||
* @date 2021/3/7
|
||||
**/
|
||||
public static KeyPair convertBcToJceKeyPair(AsymmetricCipherKeyPair bcKeyPair) throws Exception {
|
||||
byte[] pkcs8Encoded = PrivateKeyInfoFactory.createPrivateKeyInfo(bcKeyPair.getPrivate()).getEncoded();
|
||||
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(pkcs8Encoded);
|
||||
byte[] spkiEncoded = SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(bcKeyPair.getPublic()).getEncoded();
|
||||
X509EncodedKeySpec spkiKeySpec = new X509EncodedKeySpec(spkiEncoded);
|
||||
KeyFactory keyFac = KeyFactory.getInstance(ALGO_NAME_EC, BouncyCastleProvider.PROVIDER_NAME);
|
||||
return new KeyPair(keyFac.generatePublic(spkiKeySpec), keyFac.generatePrivate(pkcs8KeySpec));
|
||||
}
|
||||
|
||||
/**
|
||||
* java.security.KeyPair转换为BC类型的公私钥对
|
||||
* @param keyPair
|
||||
* @return
|
||||
* @author hongyu guo
|
||||
* @date 2021/3/7
|
||||
**/
|
||||
public static AsymmetricCipherKeyPair convertKeyPair2Bc(KeyPair keyPair) {
|
||||
ECPrivateKeyParameters priKey = convertPrivateKeyToParameters((BCECPrivateKey) keyPair.getPrivate());
|
||||
ECPublicKeyParameters pubKey = convertPublicKeyToParameters((BCECPublicKey) keyPair.getPublic());
|
||||
return new AsymmetricCipherKeyPair(priKey,pubKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* BCECPrivateKey类型私钥转化为ECPrivateKeyParameters类型的私钥,
|
||||
* 调用者若想将java.security.PrivateKey转化为ECPrivateKeyParameters, 调用时将其强制转换为BCECPrivateKey类型
|
||||
* @param ecPriKey
|
||||
* @return ECPrivateKeyParameters
|
||||
* @author hongyu guo
|
||||
* @date 2021/3/7
|
||||
**/
|
||||
public static ECPrivateKeyParameters convertPrivateKeyToParameters(BCECPrivateKey ecPriKey) {
|
||||
ECParameterSpec parameterSpec = ecPriKey.getParameters();
|
||||
ECDomainParameters domainParameters = new ECDomainParameters(parameterSpec.getCurve(), parameterSpec.getG(),
|
||||
parameterSpec.getN(), parameterSpec.getH());
|
||||
return new ECPrivateKeyParameters(ecPriKey.getD(), domainParameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* BCECPublicKey类型私钥转化为ECPublicKeyParameters类型的私钥,
|
||||
* 调用者若想将java.security.PublicKey转化为ECPublicKeyParameters, 调用时将其强制转换为BCECPublicKey类型
|
||||
* @param ecPubKey
|
||||
* @return ECPrivateKeyParameters
|
||||
* @author hongyu guo
|
||||
* @date 2021/3/7
|
||||
**/
|
||||
public static ECPublicKeyParameters convertPublicKeyToParameters(BCECPublicKey ecPubKey) {
|
||||
ECParameterSpec parameterSpec = ecPubKey.getParameters();
|
||||
ECDomainParameters domainParameters = new ECDomainParameters(parameterSpec.getCurve(), parameterSpec.getG(),
|
||||
parameterSpec.getN(), parameterSpec.getH());
|
||||
return new ECPublicKeyParameters(ecPubKey.getQ(), domainParameters);
|
||||
}
|
||||
/**
|
||||
* 生成一对sm2公私钥
|
||||
* @param
|
||||
* @return minsecurity.crypto.sm2.SM2KeyPair
|
||||
* @throws
|
||||
* @author hongyu guo
|
||||
* @date 2021/3/15
|
||||
**/
|
||||
public static SM2KeyPair generateSM2KeyPair(){
|
||||
return SM2KeyPair.generateKeyPair();
|
||||
}
|
||||
|
||||
/**
|
||||
* SM2字节数组->16进制字节数组 辅助函数
|
||||
* @param bytes byte[]
|
||||
* @return byte[]
|
||||
* @throws
|
||||
* @author zhengqi wu
|
||||
* @date 2021/4/12
|
||||
**/
|
||||
public static byte[] byteArraytoHexByteArray(byte[] bytes){
|
||||
// 16进制表示法
|
||||
// StringBuilder sb = new StringBuilder();
|
||||
// for(int i=0;i < bytes.length;i++) {
|
||||
// sb.append(String.format("%02x", bytes[i]));
|
||||
// }
|
||||
String hexStr = ByteUtils.toHexString(bytes);
|
||||
return hexStr.getBytes();
|
||||
}
|
||||
|
||||
/**
|
||||
* 16进制字节数组->SM2字节数组 辅助函数
|
||||
* @param bytes byte[]
|
||||
* @return byte[]
|
||||
* @throws
|
||||
* @author zhengqi wu
|
||||
* @date 2021/4/12
|
||||
**/
|
||||
public static byte[] hexByteArrayToByteArray(byte[] bytes){
|
||||
String hex = new String(bytes);
|
||||
if (hex.length() < 1) {
|
||||
return null;
|
||||
} else {
|
||||
byte[] result = new byte[hex.length() / 2];
|
||||
int j = 0;
|
||||
for(int i = 0; i < hex.length(); i+=2) {
|
||||
result[j++] = (byte)Integer.parseInt(hex.substring(i,i+2), 16);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package VMSConnection.Security.SM2;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description: sm2的自定义异常
|
||||
* @Version: 1.0.0
|
||||
* @Date: 19:00 2021/03/03
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class SM2Exception extends AsymKeyException {
|
||||
public SM2Exception(String msg){
|
||||
super(msg);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package VMSConnection.Security.SM2;
|
||||
|
||||
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 09:57 2021/03/11
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class SM2KeyPair {
|
||||
private final SM2PublicKey sm2PublicKey;
|
||||
private final SM2PrivateKey sm2PrivateKey;
|
||||
|
||||
public SM2KeyPair(SM2PublicKey sm2PublicKey, SM2PrivateKey sm2PrivateKey) {
|
||||
this.sm2PublicKey = sm2PublicKey;
|
||||
this.sm2PrivateKey = sm2PrivateKey;
|
||||
}
|
||||
|
||||
public static SM2KeyPair generateKeyPair(){
|
||||
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();
|
||||
return new SM2KeyPair(new SM2PublicKey(x,y), new SM2PrivateKey(d));
|
||||
}
|
||||
|
||||
|
||||
public SM2PublicKey getSm2PublicKey() {
|
||||
return sm2PublicKey;
|
||||
}
|
||||
|
||||
public SM2PrivateKey getSm2PrivateKey() {
|
||||
return sm2PrivateKey;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package VMSConnection.Security.SM2;
|
||||
|
||||
import org.bouncycastle.crypto.CipherParameters;
|
||||
import org.bouncycastle.crypto.CryptoException;
|
||||
import org.bouncycastle.crypto.InvalidCipherTextException;
|
||||
import org.bouncycastle.crypto.engines.SM2Engine;
|
||||
import org.bouncycastle.crypto.params.ECDomainParameters;
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ParametersWithRandom;
|
||||
import org.bouncycastle.crypto.signers.SM2Signer;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description: 国密sm2 非对称加密私钥
|
||||
* @Version: 1.0.0
|
||||
* @Date: 19:40 2021/03/03
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class SM2PrivateKey implements PrivateKeyInterface {
|
||||
|
||||
public SM2PrivateKey(){}
|
||||
|
||||
/*
|
||||
* @param d
|
||||
* @return
|
||||
* @author hongyu guo
|
||||
* @date 2021/3/7
|
||||
**/
|
||||
|
||||
/*
|
||||
* @param d 大数
|
||||
* @return
|
||||
* @author hongyu guo
|
||||
* @date 2021/3/7
|
||||
**/
|
||||
public SM2PrivateKey(byte[] d) {
|
||||
privateKey = new ECPrivateKeyParameters(new BigInteger(d), SM2Base.DOMAIN_PARAMS);
|
||||
}
|
||||
public SM2PrivateKey(byte[] d, ECDomainParameters parameters){
|
||||
if(d.length == 32 || d.length == 33)
|
||||
privateKey = new ECPrivateKeyParameters(new BigInteger(d), parameters);
|
||||
else {
|
||||
try {
|
||||
throw new SM2Exception("SM2私钥中,参数d长度应为32/33");
|
||||
} catch (SM2Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getBytes() {
|
||||
byte[] bytes = privateKey.getD().toByteArray();
|
||||
return SM2Base.byteArraytoHexByteArray(bytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
// public boolean setBytes(byte[] d) {
|
||||
// // TODO 待与go版本进行兼容 目前版本无法使用
|
||||
// if(d.length != 32 && d.length != 33)
|
||||
// return false;
|
||||
// privateKey = new ECPrivateKeyParameters(new BigInteger(d), SM2Base.DOMAIN_PARAMS);
|
||||
// return true;
|
||||
// }
|
||||
|
||||
/**
|
||||
* @description 与external_amd64同步
|
||||
* @params d {byte[]}
|
||||
* @return boolean
|
||||
*/
|
||||
// TODO 实现不同
|
||||
public boolean setBytes(byte[] d){
|
||||
if (d.length != 64 && d.length != 66 && d.length != 32 && d.length != 33)
|
||||
return false;
|
||||
switch (d.length){
|
||||
case 32:
|
||||
case 33:
|
||||
privateKey = new ECPrivateKeyParameters(new BigInteger(d), SM2Base.DOMAIN_PARAMS);
|
||||
break;
|
||||
case 64:
|
||||
case 66:
|
||||
d = SM2Base.hexByteArrayToByteArray(d);
|
||||
privateKey = new ECPrivateKeyParameters(new BigInteger(d), SM2Base.DOMAIN_PARAMS);
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] decrypt(byte[] cipher) throws InvalidCipherTextException {
|
||||
SM2Engine engine = new SM2Engine(SM2Engine.Mode.C1C2C3);
|
||||
engine.init(false, privateKey);
|
||||
return engine.processBlock(cipher, 0, cipher.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] sign(byte[] content) throws CryptoException {
|
||||
SM2Signer signer = new SM2Signer();
|
||||
CipherParameters param = null;
|
||||
ParametersWithRandom pwr = new ParametersWithRandom(privateKey, new SecureRandom());
|
||||
param = pwr;
|
||||
signer.init(true, param);
|
||||
signer.update(content, 0, content.length);
|
||||
return signer.generateSignature();
|
||||
}
|
||||
|
||||
private ECPrivateKeyParameters privateKey;
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package VMSConnection.Security.SM2;
|
||||
|
||||
import org.bouncycastle.crypto.CipherParameters;
|
||||
import org.bouncycastle.crypto.InvalidCipherTextException;
|
||||
import org.bouncycastle.crypto.engines.SM2Engine;
|
||||
import org.bouncycastle.crypto.params.ECDomainParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ParametersWithRandom;
|
||||
import org.bouncycastle.crypto.signers.SM2Signer;
|
||||
import org.bouncycastle.math.ec.ECCurve;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description: 国密sm2 非对称加密公钥, 实现PublicKeyInterface中的方法
|
||||
* @Version: 1.0.0
|
||||
* @Date: 17:32 2021/03/03
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class SM2PublicKey implements PublicKeyInterface {
|
||||
|
||||
public SM2PublicKey(){}
|
||||
|
||||
public SM2PublicKey(byte[] xBytes, byte[] yBytes, ECCurve curve, ECDomainParameters domainParameters){
|
||||
publicKey = createPublicKey(xBytes,yBytes,curve,domainParameters);
|
||||
}
|
||||
/**
|
||||
* 使用SM2推荐曲线参数
|
||||
* @param xBytes 曲线x参数
|
||||
* @param yBytes 曲线y参数
|
||||
* @return
|
||||
* @author ghy
|
||||
* @date 2021/3/7
|
||||
**/
|
||||
public SM2PublicKey(byte[] xBytes, byte[] yBytes){
|
||||
this(xBytes,yBytes, SM2Base.CURVE, SM2Base.DOMAIN_PARAMS);
|
||||
}
|
||||
|
||||
public SM2PublicKey(byte[] bytes){
|
||||
setBytes(bytes);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public byte[] getBytes() {
|
||||
if(publicKey != null){
|
||||
byte[] bytes = publicKey.getQ().getEncoded(false); //不压缩
|
||||
byte[] basic = SM2Base.byteArraytoHexByteArray(bytes);
|
||||
return basic;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setBytes(byte[] bytes) {
|
||||
switch (bytes.length){
|
||||
case 65:
|
||||
createPub(bytes);
|
||||
break;
|
||||
case 130:
|
||||
bytes = SM2Base.hexByteArrayToByteArray(bytes); // 16进制表示法转为十进制表示法
|
||||
createPub(bytes);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void createPub(byte[] bytes){
|
||||
ByteBuffer encoded = ByteBuffer.wrap(bytes);
|
||||
byte[] x = new byte[32];
|
||||
byte[] y = new byte[32];
|
||||
encoded.position(1);
|
||||
encoded.get(x,0,32);
|
||||
encoded.get(y,0,32);
|
||||
publicKey = createPublicKey(x,y);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param content
|
||||
* @return byte[] 密文
|
||||
* @author ghy
|
||||
* @date 2021/3/7
|
||||
**/
|
||||
@Override
|
||||
public byte[] encrypt(byte[] content) throws InvalidCipherTextException {
|
||||
SM2Engine engine = new SM2Engine(SM2Engine.Mode.C1C2C3);
|
||||
ParametersWithRandom pwr = new ParametersWithRandom(publicKey, new SecureRandom());
|
||||
engine.init(true, pwr);
|
||||
return engine.processBlock(content, 0, content.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean verify(byte[] content, byte[] digest) {
|
||||
SM2Signer signer = new SM2Signer();
|
||||
CipherParameters param = publicKey;
|
||||
signer.init(false, param);
|
||||
signer.update(content, 0, content.length);
|
||||
return signer.verifySignature(digest);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////// private start //////////////////////////////////////////////
|
||||
private static ECPublicKeyParameters createPublicKey(
|
||||
byte[] xBytes, byte[] yBytes, ECCurve curve, ECDomainParameters domainParameters){
|
||||
final byte uncompressedFlag = 0x04;
|
||||
int curveLength = getCurveLength(domainParameters);
|
||||
xBytes = fixToCurveLengthBytes(curveLength, xBytes);
|
||||
yBytes = fixToCurveLengthBytes(curveLength, yBytes);
|
||||
byte[] encodedPubKey = new byte[1 + xBytes.length + yBytes.length];
|
||||
encodedPubKey[0] = uncompressedFlag;
|
||||
System.arraycopy(xBytes, 0, encodedPubKey, 1, xBytes.length);
|
||||
System.arraycopy(yBytes, 0, encodedPubKey, 1 + xBytes.length, yBytes.length);
|
||||
return new ECPublicKeyParameters(curve.decodePoint(encodedPubKey), domainParameters);
|
||||
}
|
||||
|
||||
private static ECPublicKeyParameters createPublicKey(byte[] xBytes, byte[] yBytes){
|
||||
return createPublicKey(xBytes,yBytes, SM2Base.CURVE, SM2Base.DOMAIN_PARAMS);
|
||||
}
|
||||
|
||||
private static int getCurveLength(ECDomainParameters domainParams) {
|
||||
return (domainParams.getCurve().getFieldSize() + 7) / 8;
|
||||
}
|
||||
|
||||
private static byte[] fixToCurveLengthBytes(int curveLength, byte[] src) {
|
||||
if (src.length == curveLength) {
|
||||
return src;
|
||||
}
|
||||
|
||||
byte[] result = new byte[curveLength];
|
||||
if (src.length > curveLength) {
|
||||
System.arraycopy(src, src.length - result.length, result, 0, result.length);
|
||||
} else {
|
||||
System.arraycopy(src, 0, result, result.length - src.length, src.length);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private ECPublicKeyParameters publicKey;
|
||||
////////////////////////////////////////////// private end //////////////////////////////////////////////
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user