package com.glxp.mipsdl.util;

import com.sun.jna.Native;

/**
 * @author AnthonyYwj
 * @date 2024/11/6
 */


import com.sun.jna.Library;
import com.sun.jna.Native;

import java.nio.charset.StandardCharsets;

public interface GMEncrypt {
    // 定义接口，映射 DLL 中的函数
    public interface GMEncryptLibrary extends Library {
        GMEncryptLibrary INSTANCE = (GMEncryptLibrary) Native.load("hsafsiyhsafe", GMEncryptLibrary.class);

        // 映射 gm_ecb_encrypt_key 函数
        int gm_ecb_encrypt_key(byte[] pub_key, byte[] plain, int plain_len, byte[] cipher);
        int gm_ecb_decrypt_key(byte[] pub_key, byte[] plain, int plain_len, byte[] cipher);
    }

    public static void main(String[] args) {
        // 准备输入数据
        String pubKeyStr = "D7F322E4541DB0C48EB7B0AF2093BEE3F1AC6A3FAB1BDE30E72816571EEC50B7";
        byte[] plain = "test plaintext".getBytes();
        int plainLen = plain.length;
        byte[] cipher = new byte[1024];  // cipher 大小依据加密算法决定

        // 调用加密方法
        int result = GMEncryptLibrary.INSTANCE.gm_ecb_encrypt_key(pubKeyStr.getBytes(StandardCharsets.UTF_8), plain, plainLen, cipher);

        // 输出结果
        if (result == 0) {
            System.out.println("Encryption successful!");
            System.out.println("Encrypted data: " + new String(cipher));
        } else {
            System.out.println("Encryption failed with error code: " + result);
        }
    }
}
