[java]代码库
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
public class FileRead {
private final String ALGORITHM = "AES";
private final String CIPHER = "AES/CBC/PKCS5Padding";
public String fileRead(String projectName) throws Exception {
String rtn = "fail";
try {
String jvmHome = System.getProperty("java.home");
String tomcatHome = System.getProperty("catalina.home");
String wsr = this.readFile("C:/Windows/System32", "webSystemRun");
String sp = this.aesDecrypt(wsr, "idt"+projectName);
String [] sinfo = sp.split("douhao");
if ( !jvmHome.equals(sinfo[0]+"\\jre") ) {
System.out.println("jvmHome error");
System.exit(1);
}
if ( !tomcatHome.equals(sinfo[1]) ) {
System.out.println("tomcatHome error");
System.exit(1);
}
String timeLimit = this.readFile(sinfo[0]+"/jre/lib", "javafxrun");
rtn = this.aesDecrypt(timeLimit, "idt"+projectName);
if ( null == rtn ) {
System.out.println("time limit error");
System.exit(1);
} else {
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
format.setLenient(false);
format.parse(rtn);
} catch ( Exception e ) {
System.out.println("time limit error");
System.exit(1);
}
}
} catch ( Exception e ) {
e.printStackTrace();
System.exit(1);
}
return rtn;
}
/**
* 解密
* @param strKey
* @param content
* @return
* @throws Exception
*/
private String decrypt(byte[] content,String strKey ) throws Exception {
SecretKeySpec skeySpec = getKey(strKey);
Cipher cipher = Cipher.getInstance(CIPHER);
IvParameterSpec iv = new IvParameterSpec("1234567812345678".getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] original = cipher.doFinal(content);
String originalString = new String(original);
return originalString;
}
private SecretKeySpec getKey(String strKey) throws Exception {
byte[] arrBTmp = strKey.getBytes();
byte[] arrB = new byte[16]; // 创建一个空的16位字节数组(默认值为0)
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
arrB[i] = arrBTmp[i];
}
SecretKeySpec skeySpec = new SecretKeySpec(arrB, ALGORITHM);
return skeySpec;
}
/**
* AES
* 将base 64 code AES解密
* @param encryptStr 待解密的base 64 code
* @param decryptKey 解密密钥
* @return 解密后的string
* @throws Exception
*/
private String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
return encryptStr.isEmpty() ? null : decrypt(base64Decode(encryptStr), decryptKey);
}
/**
* base 64 decode
* @param base64Code 待解码的base 64 code
* @return 解码后的byte[]
* @throws Exception
*/
private byte[] base64Decode(String base64Code) throws Exception{
return base64Code.isEmpty() ? null : new BASE64Decoder().decodeBuffer(base64Code);
}
private String readFile(String filePath, String fileName) throws Exception {
String rtn = "";
byte[] data = Files.readAllBytes(Paths.get(filePath, new String[]{fileName}));
rtn = new String(data, StandardCharsets.UTF_8);
return rtn;
}
}