JNA結構體巢狀如何呼叫
阿新 • • 發佈:2021-11-19
一、C語言結構體
typedef struct ECCrefPublicKey_st { unsigned int bits; unsigned char x[ECCref_MAX_LEN]; unsigned char y[ECCref_MAX_LEN]; }ECCrefPublicKey; typedef struct ECCCipher_st { unsigned char x[ECCref_MAX_LEN]; unsigned char y[ECCref_MAX_LEN]; unsigned char M[32]; unsigned int L; unsigned char C[1024]; }ECCCipher; typedef ECCCipher ECCCIPHERBLOB; typedef ECCrefPublicKey ECCPUBLICKEYBLOB; typedef struct SDF_ENVELOPEDKEYBLOB { unsigned long ulAsymmAlgID; unsigned long ulSymmAlgID; ECCCIPHERBLOB ECCCipherBlob; ECCPUBLICKEYBLOB PubKey; unsigned char cbEncryptedPriKey[ECCref_MAX_LEN]; }ENVELOPEDKEYBLOB,* PENVELOPEDKEYBLOB;
二、C語言介面
//生成ECC金鑰對的保護結構 int SDF_Ext_GenEnvKeyPairBlob_ECC(void* hSessionHandle, unsigned int symmAlgID, ECCrefPublicKey* pucPublicKey, ENVELOPEDKEYBLOB* pEnvelopedKeyBlob);
三、jna介面
int SDF_Ext_GenEnvKeyPairBlob_ECC(Pointer var1, NativeLong var2, ECCrefPublicKey.ByReference pucPublicKey, ENVELOPEDKEYBLOB.ByReference blob);
四、JNA結構體
class ENVELOPEDKEYBLOB extends Structure { public NativeLong ulAsymmAlgID; public NativeLong ulSymmAlgID; public ECCCipher ECCCipherBlob; public ECCrefPublicKey PubKey; public byte[] cbEncryptedPriKey = new byte[64]; @Override protected List getFieldOrder() { return Arrays.asList("ulAsymmAlgID", "ulSymmAlgID", "ECCCipherBlob", "PubKey", "cbEncryptedPriKey"); } public static class ByValue extends ENVELOPEDKEYBLOB implements Structure.ByValue { public ByValue() { } } public static class ByReference extends ENVELOPEDKEYBLOB implements Structure.ByReference { public ByReference() { } } }
public static class ECCCipher extends Structure { public byte[] x = new byte[64]; public byte[] y = new byte[64]; public byte[] M = new byte[32]; public int L; public byte[] C = new byte[1024]; public static class ByValue extends ECCCipher implements Structure.ByValue { public ByValue() { } } public static class ByReference extends ECCCipher implements Structure.ByReference { public ByReference() { } } }
class ECCrefPublicKey extends Structure { public int bits; public byte[] x = new byte[64]; public byte[] y = new byte[64]; @Override protected List<String> getFieldOrder() { return Arrays.asList(new String[] {"bits", "x","y"}); } @Override public String toString() { return JSON.toJSONString(this); } public static class ByValue extends ECCrefPublicKey implements Structure.ByValue { public ByValue() { } } public static class ByReference extends ECCrefPublicKey implements Structure.ByReference { public ByReference() { } } }
五、jna呼叫demo
TestSDF_API.TestCLibrary.ECCCipher ECCCipherBlob = new TestSDF_API.TestCLibrary.ECCCipher(); TestSDF_API.TestCLibrary.ECCrefPublicKey pubKey = new TestSDF_API.TestCLibrary.ECCrefPublicKey(); TestSDF_API.TestCLibrary.ENVELOPEDKEYBLOB.ByReference pEnvelopedKeyBolb = new TestSDF_API.TestCLibrary.ENVELOPEDKEYBLOB.ByReference(); pEnvelopedKeyBolb.setECCCipherBlob(ECCCipherBlob); pEnvelopedKeyBolb.setPubKey(pubKey); pEnvelopedKeyBolb.setCbEncryptedPriKey(new byte[64]); rv = api.Test_SDF_Ext_GenEnvKeyPairBlob_ECC(hSessionHandle, sm4, EncPubKey, pEnvelopedKeyBolb);
搞定。
需要詳細教程的加QQ:1059585163