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