使用Java加密PE文件

时间:2015-03-21 18:29:35

标签: java encryption jca

我正在尝试在Java中创建一个文件crypter,并且它与txt文件完美配合,但是当我尝试加密.exe文件时,该文件正在变为f *** up。我用C ++编写了一个简单的hello world程序,它在命令提示符下打印“hello world”,我称之为f(为简单起见)。问题是,当我加密文件,然后解密它,它已损坏我的意思是我无法运行它我收到消息,该文件与64架构不兼容。这是我的Java代码:

主要课程(该课程从这里开始:

public class Main {
    public static void main(String[] args) {
        try{
            FileLoader fl = new FileLoader("C:\\..\\f.exe");    
            fl.encrypt();
            SecretKey key = fl.get_key();
            FileLoader dk = new FileLoader("C:\\..\\encrypted.exe", key);
            dk.decrypt();
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
}

FileHandler是我用来处理文件的类

public class FileLoader {
    private String fileName;
    private byte[] b_file_data;
    private String s_file_data;
    private List<String> l_file_data;
    private Path path;
    private SecretKey key;
    public FileLoader(String file_name) throws IOException {
        this.fileName = file_name;
        this.b_file_data = read_file_bytes();
        path = Paths.get(this.fileName);
        this.l_file_data = s_read_file_lines();
    }
    public FileLoader(String file_name, SecretKey key) throws IOException {
        this.fileName = file_name;
        this.key = key;
        this.b_file_data = read_file_bytes();
        path = Paths.get(this.fileName);
        this.l_file_data = s_read_file_lines();
    }
    public SecretKey get_key(){
        return this.key;    
    }
    private byte[] read_file_bytes() throws IOException{
        Path path = Paths.get(this.fileName);
        return Files.readAllBytes(path);
    }
    public void write_to_file(byte[] Bytes) throws IOException{
        Path path = Paths.get(this.fileName);
            Files.write(path, Bytes);     
    }
    public byte[] get_file_bytes() {
        return this.b_file_data;
    }

    private List<String> s_read_file_lines() throws IOException {
        Charset charset = Charset.forName("ISO-8859-1");
        return Files.readAllLines(this.path, charset);
    }
    public List<String> get_file_lines() {
        return this.l_file_data;
    }
    public String get_data_string(){
        return get_file_lines().toString();
    }
    public void encrypt() throws Exception {
        DES algorithm = new DES(read_file_bytes());
        key = algorithm.get_key();
        byte[] encrypted = algorithm.get_encrypted_data();
        FileOutputStream out = new FileOutputStream(new File("encrypted.exe"));
        out.write(encrypted);
    }
    public void decrypt() throws Exception {
        DES algorithm = new DES(read_file_bytes());
        key = algorithm.get_key();
        byte[] decrypted = algorithm.get_decrypted_data();
        FileOutputStream out = new FileOutputStream(new File("decrypted.exe"));
        out.write(decrypted);
    }
}

DES类只是实现了加密算法

public class DES {
    private KeyGenerator keyGen;
    private SecretKey secretKey;
    private Cipher cipher;
    private byte[] bytes_to_encrypt;
    private byte[] encrypted_bytes;
    private byte[] decrypted_bytes;
    public DES(byte[] bytes_to_encrypt) {
        this.bytes_to_encrypt = bytes_to_encrypt;
        generate_key();
        init_cipher();
        encrypt_text();
    }
    private void generate_key(){
        try{
            keyGen = KeyGenerator.getInstance("DES");
        }catch(Exception e){
            System.out.println(e.toString());   
        }
        keyGen.init(56);
        secretKey = keyGen.generateKey();
    }
    private void init_cipher(){
        try{
            cipher = Cipher.getInstance("DES");   
        }catch(Exception e){
            System.out.println(e.toString());    
        }
    }
    private void encrypt_text(){
        try{
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            encrypted_bytes = cipher.doFinal(bytes_to_encrypt);
        }catch(Exception e){
            System.out.println(e.toString());
        }
    }
    private void decrypt_text(){
        try{
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            decrypted_bytes = cipher.doFinal(encrypted_bytes);
        }catch(Exception e){
            System.out.println(e.toString());
        }
    }
    public byte[] get_encrypted_data(){
        return this.encrypted_bytes;    
    }
    public byte[] get_decrypted_data(){
        decrypt_text();
        return this.decrypted_bytes;    
    }
    public byte[] get_original_data(){
        return this.bytes_to_encrypt;
    }
    public SecretKey get_key(){
        return this.secretKey;    
    }
}

由于我将PE加密为任何其他文件,我认为我正在搞乱这些部分,但我不知道如何纠正它。任何帮助表示赞赏。我为看起来不好的代码道歉

0 个答案:

没有答案