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.
145 lines
4.6 KiB
145 lines
4.6 KiB
/* |
|
* Copyright (C), 2018-2022 |
|
* Project: starter |
|
* FileName: InteKeyHandler |
|
* Author: xx |
|
* Date: 2022/3/7 11:19 |
|
*/ |
|
package com.fr.plugin.gb.utils; |
|
|
|
import com.fanruan.api.i18n.I18nKit; |
|
import com.fanruan.api.log.LogKit; |
|
import com.fanruan.api.util.IOKit; |
|
import com.fanruan.api.util.StringKit; |
|
import com.fr.general.ComparatorUtils; |
|
import com.fr.plugin.context.PluginContexts; |
|
|
|
import java.io.*; |
|
|
|
/** |
|
* <Function Description><br> |
|
* <InteKeyHandler> |
|
* |
|
* @author xx |
|
* @since 1.0.0 |
|
*/ |
|
public class InteKeyHandler { |
|
private static volatile InteKeyHandler handler = null; |
|
|
|
public InteKeyHandler() { |
|
} |
|
|
|
public static InteKeyHandler getInstance() { |
|
if (handler == null) { |
|
handler = new InteKeyHandler(); |
|
} |
|
return handler; |
|
} |
|
|
|
/** |
|
* 加密文件操作 |
|
* |
|
* @param out |
|
* @param file |
|
*/ |
|
public void encryptFile(OutputStream out, File file) { |
|
if (PluginContexts.currentContext() == null || !PluginContexts.currentContext().isAvailable()) { |
|
LogKit.error(I18nKit.getLocText("Plugin-gb_Licence_Expired")); |
|
return; |
|
} |
|
LogKit.info("gb-InteKeyHandler-encryptFile-filePath:{}", file.getAbsolutePath()); |
|
if (!encrypt(file.getAbsolutePath())) { |
|
return; |
|
} |
|
try { |
|
FileInputStream in = new FileInputStream(file); |
|
byte[] b = new byte[1024]; |
|
int len; |
|
while ((len = in.read(b)) != -1) { |
|
out.write(b, 0, len); |
|
} |
|
out.close(); |
|
in.close(); |
|
} catch (IOException e) { |
|
LogKit.error(e.getMessage(), e); |
|
} |
|
} |
|
|
|
/** |
|
* 输入流解密操作 |
|
* |
|
* @param inputStream |
|
* @return |
|
*/ |
|
public File decrypt(InputStream inputStream) { |
|
if (PluginContexts.currentContext() == null || !PluginContexts.currentContext().isAvailable()) { |
|
LogKit.error(I18nKit.getLocText("Plugin-gb_Licence_Expired")); |
|
return null; |
|
} |
|
byte[] bytes = IOKit.inputStream2Bytes(inputStream); |
|
FileOutputStream fos = null; |
|
File tempFile = null; |
|
try { |
|
tempFile = File.createTempFile("InteKey", StringKit.EMPTY); |
|
fos = new FileOutputStream(tempFile); |
|
fos.write(bytes); |
|
//流关闭,指针null,目的是为了解密的时候这个程序这边不占用,要不然解密的时候会报错,文件被其他文件占用 |
|
fos.flush(); |
|
fos.close(); |
|
fos = null; |
|
String filePath = tempFile.getAbsolutePath(); |
|
tempFile = null; |
|
LogKit.info("gb-InteKeyHandler-decrypt-filePath:{}", filePath); |
|
if (!isEncryptFile(filePath)) { |
|
LogKit.info("gb-InteKeyHandler-decrypt-Not Encrypt File"); |
|
return new File(filePath); |
|
} |
|
if (!decrypt(filePath)) { |
|
LogKit.info("gb-InteKeyHandler-decrypt-File decrypt failed"); |
|
return new File(filePath); |
|
} |
|
LogKit.info("gb-InteKeyHandler-decrypt-FileInputStream"); |
|
return new File(filePath); |
|
} catch (Exception e) { |
|
LogKit.error(e.getMessage(), e); |
|
return null; |
|
} |
|
} |
|
|
|
public boolean encrypt(String path) { |
|
System.setProperty("jna.encoding", "GBK"); |
|
int resultCode; |
|
resultCode = DLLUtil.dllUtil64.Ea(path); |
|
LogKit.info("gb-InteKeyHandler-encrypt-resultCode:{}", resultCode); |
|
return ComparatorUtils.equals(resultCode, 0); |
|
// return true; |
|
} |
|
|
|
public boolean decrypt(String path) { |
|
int resultCode = -1; |
|
int index = 0; |
|
while (true) { |
|
if (!ComparatorUtils.equals(DLLUtil.dllUtil64.Ia(path), 0)) { |
|
resultCode = DLLUtil.dllUtil64.Da(path, 1, ""); |
|
} else { |
|
break; |
|
} |
|
index++; |
|
if (index > 20) { |
|
break; |
|
} |
|
} |
|
LogKit.info("gb-InteKeyHandler-decrypt-resultCode:{}", resultCode); |
|
return ComparatorUtils.equals(resultCode, 0); |
|
// return true; |
|
} |
|
|
|
public boolean isEncryptFile(String path) { |
|
System.setProperty("jna.encoding", "GBK"); |
|
int resultCode; |
|
resultCode = DLLUtil.dllUtil64.Ia(path); |
|
LogKit.info("gb-InteKeyHandler-isEncryptFile-resultCode:{}", resultCode); |
|
return ComparatorUtils.equals(resultCode, 1) || ComparatorUtils.equals(resultCode, 2); |
|
// return true; |
|
} |
|
} |