156 lines
5.6 KiB
Java
156 lines
5.6 KiB
Java
package cmbpoc;
|
|
|
|
import com.sunyard.RetWrap;
|
|
import com.sunyard.SydApi;
|
|
import com.sunyard.proto.PacketSection;
|
|
import com.sunyard.proto.Util;
|
|
import org.junit.*;
|
|
import racal.sunyard.main.SydApi4j;
|
|
|
|
public class FunctionTest {
|
|
|
|
private SydApi4j api;
|
|
private String keypairIndex4Sign = "0000000000000sge00000000001";
|
|
private String keypairIndex4Ende = "0000000000000sge00100000001";
|
|
private byte[] privateKey4Sign;
|
|
private byte[] publicKey4Sign;
|
|
private byte[] privateKey4Ende;
|
|
private byte[] publicKey4Ende;
|
|
private byte[] orgData = new byte[1024];
|
|
private byte[] orgData2K = new byte[ 2 * 1024 ];
|
|
private byte[] orgData2M = new byte[ 2 * 1024 * 1024 ];
|
|
private byte[] orgData10M = new byte[ 10 * 1024 * 1024 ];
|
|
private String sign;
|
|
|
|
@Before
|
|
public void start(){
|
|
// 调试模式
|
|
|
|
System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
|
|
|
// 建立链接(单台)
|
|
this.api = (SydApi4j) new SydApi4j().connect("192.168.100.145", 8889, null, 30000);
|
|
|
|
// RetWrap keypair = this.getPrivateKeyAndPublickKey(keypairIndex4Sign);
|
|
// this.publicKey4Sign = Util.hexString2Bytes( keypair.get("pk").toString() );
|
|
// this.privateKey4Sign = Util.hexString2Bytes( keypair.get("sk").toString() );
|
|
//
|
|
// RetWrap keypair4Ende = this.getPrivateKeyAndPublickKey(keypairIndex4Sign);
|
|
// this.publicKey4Ende = Util.hexString2Bytes( keypair4Ende.get("pk").toString() );
|
|
// this.privateKey4Ende = Util.hexString2Bytes( keypair4Ende.get("sk").toString() );
|
|
|
|
// 使用预置密钥对
|
|
this.publicKey4Sign = Util.hexString2Bytes( "034200046508BC4FE965BEB1D57715C180135FC861FDC6527E82E7FD7A6CA25E7CE9CE75101181D7E2CDD16C1974652B38A045465C7FFBAC700E49F949AE5F4C57AEBEAA" );
|
|
this.privateKey4Sign = Util.hexString2Bytes( "00010000CF945DAFF49B9F3AB67A23E1E1BA700FF1F78F5DFFCBB518FD1303A51DABA6FC0000000000000000000000000000000000000000000000000000000000000000" );
|
|
this.publicKey4Ende = Util.hexString2Bytes("03420004AF3D4E55D26564C6C937E6EA9232691C01AC5E7916AD2136F788CE7C1E4D0AE8C4754DEC2F7F6A78962D51A171A6F5B4F11823390AEC8B5B0246CD2CCF8052B7");
|
|
this.privateKey4Ende = Util.hexString2Bytes("00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000");
|
|
}
|
|
|
|
|
|
@After
|
|
public void stop(){
|
|
if ( null != api ) {
|
|
this.api.disconnect();
|
|
this.api = null;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Ignore
|
|
@Test
|
|
public void genKeyPair(){
|
|
// RetWrap r = this.api.SYD_SM2GenKeyPair();
|
|
// String pk = r.get("publicKey").toString();
|
|
// String sk = r.get("privateKey").toString();
|
|
//
|
|
// System.out.println("pk=" + pk);
|
|
// System.out.println("sk=" + sk);
|
|
//
|
|
// this.privateKey4Sign = Util.hexString2Bytes(sk);
|
|
// this.publicKey4Sign = Util.hexString2Bytes(pk);
|
|
|
|
RetWrap rw = this.api.SM2GenKeyPair();
|
|
byte[] pk = (byte[]) rw.get(SydApi.RET_PUBLIC_KEY);
|
|
byte[] sk = (byte[]) rw.get(SydApi.RET_PRIVATE_KEY);
|
|
System.out.println("pk=" + Util.bytes2HexString(pk));
|
|
System.out.println("sk=" + Util.bytes2HexString(sk));
|
|
}
|
|
|
|
|
|
public RetWrap getPrivateKeyAndPublickKey(String keypairIndex){
|
|
String pk = this.api.SM2GetPublicKeyC(keypairIndex);
|
|
String sk = this.api.SM2GetPrivateKeyC(keypairIndex);
|
|
|
|
System.out.println("pk=" + pk);
|
|
System.out.println("sk=" + sk);
|
|
|
|
RetWrap retWrap = new RetWrap();
|
|
retWrap.put("pk", pk);
|
|
retWrap.put("sk", sk);
|
|
return retWrap;
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
public void SYD_SM2_Sign(){
|
|
String sign = this.api.SYD_SM2_Sign_HA( SydApi.DATA_HASH, privateKey4Sign, publicKey4Sign, orgData);
|
|
this.api.SYD_SM2_Verify_HA(SydApi.DATA_HASH, publicKey4Sign, orgData, sign);
|
|
}
|
|
|
|
|
|
@Test
|
|
public void SYD_SM2_Sign_2M(){
|
|
String sign = this.api.SYD_SM2_Sign_HA( SydApi.DATA_HASH, privateKey4Sign, publicKey4Sign, orgData2M);
|
|
this.api.SYD_SM2_Verify_HA(SydApi.DATA_HASH, publicKey4Sign, orgData2M, sign);
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
public void SYD_SM2_Sign_10M(){
|
|
String sign = this.api.SYD_SM2_Sign_HA( SydApi.DATA_HASH, privateKey4Sign, publicKey4Sign, orgData10M);
|
|
this.api.SYD_SM2_Verify_HA(SydApi.DATA_HASH, publicKey4Sign, orgData10M, sign);
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
public void SYD_SM2_Encrypt(){
|
|
byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData);
|
|
System.out.println("密文=" + Util.bytes2HexString(enData));
|
|
byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData);
|
|
Assert.assertArrayEquals(orgData, deData);
|
|
}
|
|
|
|
|
|
@Test
|
|
public void SYD_SM2_Encrypt_2K(){
|
|
byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData2K);
|
|
System.out.println("密文=" + Util.bytes2HexString(enData));
|
|
byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData);
|
|
Assert.assertArrayEquals(orgData2K, deData);
|
|
}
|
|
|
|
|
|
@Test
|
|
public void SYD_SM2_Encrypt_2M(){
|
|
byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData2M);
|
|
System.out.println("密文长度=" + enData.length);
|
|
byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData);
|
|
Assert.assertArrayEquals(orgData2M, deData);
|
|
}
|
|
|
|
|
|
@Test
|
|
public void SYD_SM2_Encrypt_10M(){
|
|
byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData10M);
|
|
System.out.println("密文=" + Util.bytes2HexString(enData));
|
|
byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData);
|
|
Assert.assertArrayEquals(orgData10M, deData);
|
|
}
|
|
}
|
|
|
|
|