1、准备好.so文件
2、java代码引入jna jar包
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.9.0</version>
</dependency>
3、代码实现
package com.jimi.tracker.util;
import com.google.common.collect.Lists;
import com.sun.jna.Library;
import com.sun.jna.Native;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class JnaUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(JnaUtil.class);
private static final String OS;
private static final boolean IS_WIN;
private static final String ENCODE = "encode";
private static final String USR_LIB_JIMI_SO = "/usr/lib/jimi";
private static final String SO = ".so";
private static final List<String> SO_NAME_LIST = Lists.newArrayList(ENCODE);
static {
OS = System.getProperty("os.name").toUpperCase();
IS_WIN = OS.contains("WIN");
if (!IS_WIN) {
extractSoFromJar(JnaUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath(), USR_LIB_JIMI_SO);
}
}
private static void extractSoFromJar(String jarPath, String extractPath) {
File jarFile = new File(jarPath);
try (JarFile jar = new JarFile(jarFile)) {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
if (!entry.getName().toLowerCase().endsWith(SO)) {
continue;
}
boolean isCopy = false;
for (String soName : SO_NAME_LIST) {
if (entry.getName().contains(soName)) {
isCopy = true;
}
}
if (!isCopy) {
continue;
}
File outputFile = new File(extractPath, entry.getName());
if (outputFile.exists()) {
LOGGER.info("文件已存在,先删除旧的so文件:{}", extractPath + "/" + entry.getName());
outputFile.delete();
}
outputFile = new File(extractPath, entry.getName());
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
try (InputStream is = jar.getInputStream(entry);
OutputStream os = new FileOutputStream(outputFile)) {
byte[] buffer = new byte[1024];
while (is.read(buffer) > 0) {
os.write(buffer);
}
} catch (IOException ioException) {
LOGGER.warn("读取.so文件失败 fileName: {}", entry.getName(), ioException);
}
}
} catch (IOException ioException) {
LOGGER.warn("获取.so库失败 jarPath: {} extractPath: {}", jarPath, extractPath, ioException);
}
}
public static String encryptValue(String imei, String chipId, Integer type) {
byte[] encryptBytes = new byte[32];
try {
if (IS_WIN) {
return imei;
}
EncodeLibrary.INSTANCE.ImeiChipEncrypt(encryptBytes, imei, chipId, type);
} catch (Exception e) {
LOGGER.warn("加密值获取错误 imei:{} chipId: {} type: {}", imei, chipId, type, e);
return null;
}
return new String(encryptBytes, StandardCharsets.UTF_8);
}
public interface EncodeLibrary extends Library {
EncodeLibrary INSTANCE = Native.load(ENCODE, EncodeLibrary.class);
void ImeiChipEncrypt(byte[] output, String imei, String chip, Integer mode);
}
public static void main(String[] args) {
byte[] encryptBytes = new byte[32];
EncodeLibrary.INSTANCE.ImeiChipEncrypt(encryptBytes, "123456789123123456789", null, 0);
}
}