You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
226 lines
9.2 KiB
226 lines
9.2 KiB
// GS-EDS加解密接口包,注意:不可修改包名! |
|
package com.hzdatalink.gsdes; |
|
|
|
import com.fr.log.FineLoggerFactory; |
|
|
|
public class FileOperation { |
|
// DEMO命令行程序入口 |
|
public static void main(String[] args) { |
|
if (args.length == 0) { |
|
System.out.println("请在命令行传入测试目标文件路径."); |
|
return; |
|
} |
|
|
|
// 测试文件 |
|
String fileName = args[0]; |
|
System.out.println(String.format("开始测试文件加解密: %s.", fileName)); |
|
|
|
testFileEncDec(fileName); |
|
testFileInfo(fileName); |
|
|
|
try { |
|
Thread.sleep(1000); |
|
} catch (InterruptedException e) { |
|
} |
|
} |
|
|
|
// 文件加密 |
|
public static Result encodeFile(String srcFileName, // 源文件名 |
|
String dstFileName // 目标文件名 |
|
) { |
|
return jniEncodeFile(srcFileName, dstFileName); |
|
} |
|
|
|
// 文件解密 |
|
public static Result decodeFile(String srcFileName, // 源文件名 |
|
String dstFileName // 目标文件名 |
|
) { |
|
return jniDecodeFile(srcFileName, dstFileName); |
|
} |
|
|
|
// 判断文件是否加密函数声明 |
|
public static IsFileEncryptedResult isFileEncrypted(String fileName // 文件名 |
|
) { |
|
return jniIsFileEncrypted(fileName); |
|
} |
|
|
|
// 获取文件信息函数声明 |
|
public static GetFileInfoResult getFileInfo(String fileName // 文件名 |
|
) { |
|
return jniGetFileInfo(fileName); |
|
} |
|
|
|
// 获取标密文件信息 |
|
public static GetFileCLIInfoResult getFileCLIInfo(String fileName // 文件名 |
|
) { |
|
return jniGetFileCLIInfo(fileName); |
|
} |
|
|
|
// 设置加密文件信息 |
|
public static Result setFileInfo(String fileName, // 文件名 |
|
int userOID, // 文件所属用户OID |
|
int orgOID, // 文件所属组织ID |
|
int clID, // 文件密级 |
|
int fileId // 文件ID |
|
) { |
|
return jniSetFileInfo(fileName, userOID, orgOID, clID, fileId); |
|
} |
|
|
|
// 设置标密文件信息 |
|
public static Result setFileCLIInfo(String fileName, // 文件名 |
|
int userOID, // 文件所属用户OID |
|
int orgOID, // 文件所属组织ID |
|
int clID, // 文件密级 |
|
int fileId, // 文件ID |
|
int sp, // 文件保密期限 |
|
int cliTime, // 文件标密时间 |
|
String fileUUID // 文件UUID |
|
) { |
|
return jniSetFileCLIInfo(fileName, userOID, orgOID, clID, fileId, sp, cliTime, fileUUID); |
|
} |
|
|
|
//////////////////////////////////////////////////////////////////// |
|
// 加载libGsDes.so |
|
static { |
|
String os = System.getProperty("os.name"); |
|
//Windows操作系统 |
|
if (os != null && os.toLowerCase().startsWith("windows")) { |
|
FineLoggerFactory.getLogger().info("current system is win"); |
|
System.loadLibrary("gsedsapi"); |
|
} else if (os != null && os.toLowerCase().startsWith("linux")) { |
|
FineLoggerFactory.getLogger().info("current system is linux"); |
|
System.loadLibrary("GsDes"); |
|
} else { //其它操作系统 |
|
FineLoggerFactory.getLogger().info("not support system"); |
|
} |
|
} |
|
|
|
// 文件加密函数声明 |
|
public static native Result jniEncodeFile(String srcFileName, // 源文件名 |
|
String dstFileName // 目标文件名 |
|
); |
|
|
|
// 文件解密函数声明 |
|
public static native Result jniDecodeFile(String srcFileName, // 源文件名 |
|
String dstFileName // 目标文件名 |
|
); |
|
|
|
// 判断文件是否加密函数声明 |
|
public static native IsFileEncryptedResult jniIsFileEncrypted(String fileName // 文件名 |
|
); |
|
|
|
// 获取文件信息函数声明 |
|
public static native GetFileInfoResult jniGetFileInfo(String fileName // 文件名 |
|
); |
|
|
|
// 获取标密文件信息 |
|
public static native GetFileCLIInfoResult jniGetFileCLIInfo(String fileName // 文件名 |
|
); |
|
|
|
// 设置加密文件信息 |
|
public static native Result jniSetFileInfo(String fileName, // 文件名 |
|
int userOID, // 文件所属用户OID |
|
int orgOID, // 文件所属组织ID |
|
int clID, // 文件密级 |
|
int fileId // 文件ID |
|
); |
|
|
|
// 设置标密文件信息 |
|
public static native Result jniSetFileCLIInfo(String fileName, // 文件名 |
|
int userOID, // 文件所属用户OID |
|
int orgOID, // 文件所属组织ID |
|
int clID, // 文件密级 |
|
int fileId, // 文件ID |
|
int sp, // 文件保密期限 |
|
int cliTime, // 文件标密时间 |
|
String fileUUID // 文件UUID |
|
); |
|
|
|
private static void testFileEncDec(String fileName) { |
|
Result result; |
|
|
|
// 解密文件 |
|
result = decodeFile(fileName, fileName); |
|
if (result.getSucceeded()) { |
|
System.out.println("解密文件成功."); |
|
} else { |
|
System.out.println(String.format("解密文件失败, %s", result.getMessage())); |
|
} |
|
|
|
// 加密文件 |
|
result = encodeFile(fileName, fileName); |
|
if (result.getSucceeded()) { |
|
System.out.println("加密文件成功."); |
|
} else { |
|
System.out.println(String.format("加密文件失败, %s", result.getMessage())); |
|
return; |
|
} |
|
|
|
// 校验文件是否加密 |
|
IsFileEncryptedResult isEnc = isFileEncrypted(fileName); |
|
if (isEnc.getSucceeded()) { |
|
System.out.println(String.format("检查文件是否加密成功, 文件%s.", (isEnc.getEncrypted() ? "已加密" : "未加密"))); |
|
} else { |
|
System.out.println(String.format("检查文件是否加密失败, %s", isEnc.getMessage())); |
|
return; |
|
} |
|
|
|
// 解密文件 |
|
result = decodeFile(fileName, fileName); |
|
if (result.getSucceeded()) { |
|
System.out.println("解密文件成功."); |
|
} else { |
|
System.out.println(String.format("解密文件失败, %s", result.getMessage())); |
|
} |
|
} |
|
|
|
private static void testFileInfo(String fileName) { |
|
// 加密文件 |
|
Result result = encodeFile(fileName, fileName); |
|
if (result.getSucceeded()) { |
|
System.out.println("加密文件成功."); |
|
} else { |
|
System.out.println(String.format("加密文件失败, %s", result.getMessage())); |
|
return; |
|
} |
|
|
|
// 设置文件信息 |
|
result = setFileInfo(fileName, 1026, 6, 5, 0); |
|
if (result.getSucceeded()) { |
|
System.out.println("设置文件信息成功."); |
|
} else { |
|
System.out.println(String.format("设置文件信息失败, %s", result.getMessage())); |
|
} |
|
|
|
// 获取文件信息 |
|
GetFileInfoResult fileInfo = getFileInfo(fileName); |
|
if (fileInfo.getSucceeded()) { |
|
System.out.println(String.format("获取文件信息成功, 文件%s, User:%d, Org:%d, cl:%d, fileid:%d.", |
|
(fileInfo.getEncrypted() ? "已加密" : "未加密"), fileInfo.getUserOID(), fileInfo.getOrgOID(), |
|
fileInfo.getCLID(), fileInfo.getFileID())); |
|
} else { |
|
System.out.println(String.format("获取文件信息失败, %s", fileInfo.getMessage())); |
|
} |
|
|
|
// 获取标密文件信息 |
|
GetFileCLIInfoResult fileCliInfo = getFileCLIInfo(fileName); |
|
if (fileCliInfo.getSucceeded()) { |
|
System.out.println(String.format( |
|
"获取标密文件信息成功, 文件%s, User:%d, Org:%d, cl:%d, sp:%d, cliTime:%d, fileUuid:%s.", |
|
(fileCliInfo.getEncrypted() ? "已加密" : "未加密"), fileCliInfo.getUserOID(), fileCliInfo.getOrgOID(), |
|
fileCliInfo.getCLID(), fileCliInfo.getSP(), fileCliInfo.getCLITime(), fileCliInfo.getFileUUID())); |
|
} else { |
|
System.out.println(String.format("获取标密文件信息失败, %s", fileCliInfo.getMessage())); |
|
} |
|
|
|
// 设置标密文件信息 |
|
result = setFileCLIInfo(fileName, // 文件名 |
|
fileCliInfo.getUserOID(), fileCliInfo.getOrgOID(), fileCliInfo.getCLID(), fileCliInfo.getFileId(), |
|
fileCliInfo.getSP(), fileCliInfo.getCLITime(), fileCliInfo.getFileUUID()); |
|
if (result.getSucceeded()) { |
|
System.out.println("设置文件信息成功."); |
|
} else { |
|
System.out.println(String.format("设置文件信息失败, %s", result.getMessage())); |
|
} |
|
} |
|
} |