用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

读取文件使用AES解密,不成功则退出java虚拟机

2018-04-04 作者: 加点盐举报

[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;
    }
	
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...