0

I'm trying to make a file crypter in Java, and it's working perfectly with txt files, however when I try to encrypt an .exe file, the file is getting f*** up. I had write a simple hello world program in C++ which is printing "hello world" on the command prompt, I call it f(for sake of simplicity). The problem is that when I encrypt the file, and then decrypt it, it's corrupted I mean I can't run it I'm getting message that the file isn't compatible with the 64 architecture. Here is my Java code:

The main class(the program starts from here:

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());
        }
    }
}

The FileHandler is the class which I'm using to work with files

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

The DES class it is just implementing the cryptography algorithm

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

Since I'm encrypting the PE as any other file, I think I'm messing with the sections, however I have no idea how to correct it. Any help is appreciated. I apologize for the bad looking code

Planet_Earth
  • 325
  • 1
  • 3
  • 11
  • This can't work for text files. You generate a new key each time. Your encryption and decryption keys don't match. – Artjom B. Mar 21 '15 at 18:38
  • Sorry I had posted the wrong file, I had notice this problem while I was crypting the txt file, however i'm writing code into notepad++ and jdev, updating now – Planet_Earth Mar 21 '15 at 18:46
  • Now you added a new constructor, but haven't used it. – Artjom B. Mar 21 '15 at 18:52
  • i meessed the whole page, and i post it as I was, now it's as it's in my jdev, still not working. – Planet_Earth Mar 21 '15 at 18:58
  • I don't understand why you encrypt the original file in order to get a key for decryption. Shouldn't you save the key somewhere while encrypting and then use it later? – tzima Mar 21 '15 at 19:04
  • Even with this code, you generate different keys for encryption and decryption. – Artjom B. Mar 21 '15 at 19:06
  • Yeah, I saw this I'm making new DES instances – Planet_Earth Mar 21 '15 at 19:11
  • Please debug a bit further. I don't know why you are using a toy algorithm like DES though. Also note that `"DES"` (or `"AES"` for that matter) defaults to ECB mode of encryption - which means that even if you change the cryptographic primitive, you would still not be secure. – Maarten Bodewes Mar 22 '15 at 00:02
  • I was learning the JCA, nothing to do with real world application. – Planet_Earth Mar 23 '15 at 08:42
  • have you tried CipherOutputStream instead of FileOutputStream? – Sumit Oct 25 '17 at 11:45

0 Answers0