mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
fix:兼容go版本的sm2
This commit is contained in:
1 parent
74358fbc5e
commit
f69ab1703e
5 files changed
+103
-55
No files matched your search
@@ -177,5 +177,43 @@ public class SM2Base {
|
||||
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 = sb.toString();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import org.bouncycastle.crypto.signers.SM2Signer;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Arrays;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
@@ -55,13 +56,7 @@ public class SM2PrivateKey implements PrivateKeyInterface {
|
||||
@Override
|
||||
public byte[] getBytes() {
|
||||
byte[] bytes = privateKey.getD().toByteArray();
|
||||
// 16进制表示法
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for(int i=0;i < bytes.length;i++) {
|
||||
sb.append(String.format("%02x", bytes[i]));
|
||||
}
|
||||
String hexStr = sb.toString();
|
||||
return hexStr.getBytes();
|
||||
return SM2Base.byteArraytoHexByteArray(bytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -84,11 +79,12 @@ public class SM2PrivateKey implements PrivateKeyInterface {
|
||||
return false;
|
||||
switch (d.length){
|
||||
case 32:
|
||||
case 33:
|
||||
privateKey = new ECPrivateKeyParameters(new BigInteger(d), SM2Base.DOMAIN_PARAMS);
|
||||
break;
|
||||
case 64:
|
||||
String priStr = new String(d);
|
||||
privateKey = new ECPrivateKeyParameters(new BigInteger(priStr, 16), SM2Base.DOMAIN_PARAMS);
|
||||
d = SM2Base.hexByteArrayToByteArray(d);
|
||||
privateKey = new ECPrivateKeyParameters(new BigInteger(d), SM2Base.DOMAIN_PARAMS);
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -12,6 +12,7 @@ import org.bouncycastle.math.ec.ECCurve;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Arrays;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
@@ -46,18 +47,32 @@ public class SM2PublicKey implements PublicKeyInterface {
|
||||
|
||||
|
||||
@Override
|
||||
// TODO 实现不同
|
||||
public byte[] getBytes() {
|
||||
if(publicKey != null)
|
||||
return publicKey.getQ().getEncoded(false); //不压缩
|
||||
if(publicKey != null){
|
||||
byte[] bytes = publicKey.getQ().getEncoded(false); //不压缩
|
||||
byte[] basic = SM2Base.byteArraytoHexByteArray(bytes);
|
||||
return basic;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
// TODO 实现不同
|
||||
public boolean setBytes(byte[] bytes) {
|
||||
if(bytes.length != 65)
|
||||
return false;
|
||||
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];
|
||||
@@ -65,7 +80,6 @@ public class SM2PublicKey implements PublicKeyInterface {
|
||||
encoded.get(x,0,32);
|
||||
encoded.get(y,0,32);
|
||||
publicKey = createPublicKey(x,y);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -47,8 +47,8 @@ public class TestSM2 {
|
||||
buffer.put(y);
|
||||
sm2PublicKey.setBytes(buffer.array());
|
||||
|
||||
assertArrayEquals(d, sm2PrivateKey.getBytes());
|
||||
assertArrayEquals(buffer.array(), sm2PublicKey.getBytes());
|
||||
assertArrayEquals(d, SM2Base.hexByteArrayToByteArray(sm2PrivateKey.getBytes()));
|
||||
assertArrayEquals(buffer.array(), SM2Base.hexByteArrayToByteArray(sm2PublicKey.getBytes()));
|
||||
|
||||
String content = "hello world";
|
||||
byte[] encBytes = sm2PublicKey.encrypt(content.getBytes());
|
||||
|
||||
@@ -30,42 +30,42 @@ class OperationThread extends Thread{
|
||||
System.out.println(String.format("Operation %d starting", this.threadNo));
|
||||
try{
|
||||
// 测试PersistIdentity
|
||||
SM2KeyPair pair = SM2KeyPair.generateKeyPair();
|
||||
Identity identity = new Identity();
|
||||
identity.setName("wzq"+this.threadNo);
|
||||
KeyParam keyParam = new KeyParam();
|
||||
keyParam.PublicKeyAlgorithm = 0;
|
||||
keyParam.SignatureAlgorithm = 0;
|
||||
identity.setKeyParam(keyParam);
|
||||
identity.setPrikey(pair.getSm2PrivateKey());
|
||||
identity.setPubkey(pair.getSm2PublicKey());
|
||||
identity.setPasswd("2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99");
|
||||
identity.lock("0123456789abcdef", Common.SM4ECB);
|
||||
Certificate cert = new Certificate();
|
||||
cert.setVersion(1);
|
||||
cert.setSerialNumber(1);
|
||||
cert.setPublicKey(pair.getSm2PublicKey());
|
||||
cert.setSignatureAlgorithm(Common.SM3withSM2); // TODO 名字有误? SM2withSM3?
|
||||
cert.setPublicKeyAlgorithm(Common.SM2);
|
||||
cert.setIssueTo("root");
|
||||
cert.setIssuer("root");
|
||||
long timestamp = System.currentTimeMillis() / 1000;
|
||||
cert.setTimestamp(timestamp); // 10bit timestamp
|
||||
cert.setNotAfter(timestamp); // 10bit timestamp
|
||||
cert.setNotBefore(timestamp + 1000); // 10bit timestamp
|
||||
cert.setKeyUsage(Common.CertSign);
|
||||
cert.setCA(true);
|
||||
CertUtils.signCert(cert, pair.getSm2PrivateKey());
|
||||
|
||||
identity.setCert(cert);
|
||||
Db.persistIdentity(identity);
|
||||
|
||||
Identity id = Db.getIdentityByNameFromStorage("wzq"+this.threadNo);
|
||||
|
||||
System.out.println("插入身份:" + id.getName());
|
||||
System.out.println(String.format("开始设置 wzq%d 为default 身份", this.threadNo));
|
||||
|
||||
Db.SetDefaultIdentityByNameInStorage("wzq"+this.threadNo);
|
||||
// SM2KeyPair pair = SM2KeyPair.generateKeyPair();
|
||||
// Identity identity = new Identity();
|
||||
// identity.setName("wzq"+this.threadNo);
|
||||
// KeyParam keyParam = new KeyParam();
|
||||
// keyParam.PublicKeyAlgorithm = 0;
|
||||
// keyParam.SignatureAlgorithm = 0;
|
||||
// identity.setKeyParam(keyParam);
|
||||
// identity.setPrikey(pair.getSm2PrivateKey());
|
||||
// identity.setPubkey(pair.getSm2PublicKey());
|
||||
// identity.setPasswd("2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99");
|
||||
// identity.lock("0123456789abcdef", Common.SM4ECB);
|
||||
// Certificate cert = new Certificate();
|
||||
// cert.setVersion(1);
|
||||
// cert.setSerialNumber(1);
|
||||
// cert.setPublicKey(pair.getSm2PublicKey());
|
||||
// cert.setSignatureAlgorithm(Common.SM3withSM2); // TODO 名字有误? SM2withSM3?
|
||||
// cert.setPublicKeyAlgorithm(Common.SM2);
|
||||
// cert.setIssueTo("root");
|
||||
// cert.setIssuer("root");
|
||||
// long timestamp = System.currentTimeMillis() / 1000;
|
||||
// cert.setTimestamp(timestamp); // 10bit timestamp
|
||||
// cert.setNotAfter(timestamp); // 10bit timestamp
|
||||
// cert.setNotBefore(timestamp + 1000); // 10bit timestamp
|
||||
// cert.setKeyUsage(Common.CertSign);
|
||||
// cert.setCA(true);
|
||||
// CertUtils.signCert(cert, pair.getSm2PrivateKey());
|
||||
//
|
||||
// identity.setCert(cert);
|
||||
// Db.persistIdentity(identity);
|
||||
//
|
||||
// Identity id = Db.getIdentityByNameFromStorage("wzq"+this.threadNo);
|
||||
//
|
||||
// System.out.println("插入身份:" + id.getName());
|
||||
// System.out.println(String.format("开始设置 wzq%d 为default 身份", this.threadNo));
|
||||
//
|
||||
// Db.SetDefaultIdentityByNameInStorage("wzq"+this.threadNo);
|
||||
|
||||
Identity id2 = Db.getDefaultIdentityFromStorage();
|
||||
System.out.println("当前default身份:" + id2.getName());
|
||||
@@ -101,7 +101,7 @@ public class DBTest {
|
||||
identities.get(0).unLock("0123456789abcdef", Common.SM4ECB);
|
||||
PrivateKeyInterface id = identities.get(0).getPrikey();
|
||||
byte[] bytes = id.getBytes();
|
||||
System.out.println(id.getBytes());
|
||||
System.out.println(new String(id.getBytes()));
|
||||
}catch (Exception ex){
|
||||
System.out.println(ex.getMessage());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user