package com.qy.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;


public class EncryptJar {
	public static final int rd_rx=0;
	public static final int qy_rx=4;
	public static final int gdt=1;
	public static final int uniplay=5;
	public static final int pangllin=13;
	public static final int oneway=11;
	private static final int factor=0x23;
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File file=new File("test_classes.jar");
		EncryptJar tools=new EncryptJar();
		//tools.encrypt(file, "test_classes.jar", rd_rx, 1000);
		tools.decrypt(file, "decrypt_classes.jar");
	}
	public File encrypt(File file,String outputFile,String filetype,String ver) {
		return encrypt(file, outputFile, Integer.parseInt(filetype), Integer.parseInt(ver));
	}
	public File encrypt(File file,String outputFile,int filetype,int ver) {
		try {
			int size=0;
			byte[] buf=new byte[1024];
			FileOutputStream fos = new FileOutputStream(outputFile);
			fos.write(int2byte(filetype));
		
			System.out.println("资源包版本号：" + ver);
			fos.write(int2byte(ver));
			
			FileInputStream is = new FileInputStream(file);
			while((size=is.read(buf))>0)
			{
				for(int i=0;i<size;i++)
				{
					buf[i] = (byte)(buf[i]&0xff ^ factor);
				}
				fos.write(buf, 0, size);
			}
			fos.close();
			is.close();
			return new File(outputFile);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
	public FileData decrypt(File file,String outputFile) {
		FileData fileData=new FileData();
		try {
			int size=0;
			byte[] buf=new byte[1024];
			byte[] data=new byte[4];
			FileOutputStream fos = new FileOutputStream(outputFile);
			
			FileInputStream is = new FileInputStream(file);
			is.read(data);
			fileData.type=byte2int(data);
			is.read(data);
			fileData.version=byte2int(data);
			while((size=is.read(buf))>0)
			{
				for(int i=0;i<size;i++)
				{
					buf[i] = (byte)(buf[i]&0xff ^ factor);
				}
				fos.write(buf, 0, size);
			}
			fos.close();
			is.close();
			fileData.file=new File(outputFile);
			return fileData;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
	
	static byte[] int2byte(int val)
    {
        byte[] b4 = new byte[4];
        for(int i=0;i<b4.length;i++)
        {
            b4[b4.length-i-1] = (byte) ( (val>>(i*8)) & 0xff);
        }
        return b4;
    }

    static int byte2int(byte[] b4){
        int val=0;
        for(int i=0;i<b4.length;i++)
        {
            val = (b4[i] & 0xff) + (val<<8);
        }
        return val;
    }
    public class FileData{
    	private int type;
    	private int version;
    	private File file;
		public int getType() {
			return type;
		}
		public void setType(int type) {
			this.type = type;
		}
		public int getVersion() {
			return version;
		}
		public void setVersion(int version) {
			this.version = version;
		}
		public File getFile() {
			return file;
		}
		public void setFile(File file) {
			this.file = file;
		}
		@Override
		public String toString() {
			return "资源类型:"+type + " 版本号:" + version + " 文件路径:" + file.getAbsolutePath();
		}
    	
    }
}
