有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

用Java加密PE文件

我正在尝试用Java制作一个文件加密程序,它可以完美地处理txt文件,但是当我尝试加密一个文件时。exe文件,该文件正在升级。我在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) 个答案