mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
112 lines
3.5 KiB
Java
112 lines
3.5 KiB
Java
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;
|
|
}
|