Thread: exe to txt

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    42

    exe to txt

    How do i change a exe file to a txt file and then back to exe using c++?
    Sorry, but i'm totally clueless on how to do this.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Quite simply you can't.


    You are viewing the exe as a text file - it means nothing as a text file.

    EXEs are binary files - they contain an EXE header and some type of (x)Z at the beginning depending on the version of the EXE and if it's a Windows PE or not. Basically all an exe is, is the assembly opcodes for the instructions within the program. Since the opcodes do not correspond correctly with the ASCII character codes or any text for that matter...you get garbage in a text editor...but in reality you are getting exactly what the opcode is.

    For instance if a jmp instruction is coded as 65h (it's not, but bear with me) then the jmp opcode in ASCII text would appear as capital A.

    You could use a disassembler to correctly interpret the opcode sequences in the EXE but it would still only be assembly. To actually go back to C++ or even just C would be nearly impossible
    w/o knowing which C functions were called and what parts of the EXE are simply the C static libraries and which are not. As well it would be close to impossible to re-interpret a switch() block simply based on opcodes. The interpreter would have to know a sufficient amount of information about how the compiler translated the C text into assembly. Not easy and nearly impossible. I say nearly because nothing in computers is truly impossible.

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    42
    Thank for the info bubba, i learn something.
    I need to encrypt exe file using my own encryption program that could only encrypt characters ( its lousy, i know ), so i was thinking of converting the exe file to a text file or something.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >that could only encrypt characters
    Characters or bytes? Unless you went out of your way to only correctly encrypt printable characters, there shouldn't be a problem.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    42
    characters and numbers shouldn't a problem, i could encrypt a normal text file.
    i could also program it to take in bytes but i need to get the exe file to a txt file or a binary file.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but i need to get the exe file to a txt file or a binary file
    Just open the .exe as binary in C++ and you can read the raw bytes for encryption. Then write the cipher code to another binary file.
    My best code is written with the delete key.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Encrypting an EXE file is not a good idea. You will more than likely break it...and in a sense they are already encrypted to the average viewer.

    An easy way to encrypt it is to retrieve words from the file. Swap the low and high bytes and write that back to the disk. But be warned this will break the EXE until you swap it back. A simple way to swap the low and high bytes is by using the assembly instruction ROR or ROL rotate right or rotate left. You need to rotate the WORD by 8 bits either left or right to swap the order.

    If you really wanted to get goofy you could swap the word's in a DWORD after you swapped the BYTES in a WORD or you can use XOR encryption or anything else you can come up with. Just make sure you can get it all put back together correctly.

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    You are probably much better off making your algorithm work with binary data. However if what you want to do is convert an arbitrary file into a sequence of printable characters and back again then there are lots of ways to do this. Radix-64 of mime and PGP fame is probably the most popular and portable. The plan here is basically the same as printing a byte as hex digits. the first character is based on the byte divided by 16, the second based on the remainder. With radix-64 you devide a 24 bit int by 64 creating 4 characters. Naturally this can all be done with shifts and or's and you treat the 24 bit int as 3 bytes. There is also good old uuencode that back in my day you had to use when someone sent you some pornagraphy, uh, I mean fractals, I need you to up my quota because we are sharing fractals and pictures of Shoemaker-Levy 9.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to monitor exe and dll interactions?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 10-26-2007, 04:22 AM
  2. how to run an exe command in c++ and get back the results?
    By mitilkhatoon in forum C++ Programming
    Replies: 5
    Last Post: 09-21-2006, 06:00 PM
  3. Close another exe from another exe??
    By Tony in forum Windows Programming
    Replies: 1
    Last Post: 06-12-2002, 07:19 AM
  4. insert another exe into exe
    By lliero in forum C Programming
    Replies: 8
    Last Post: 04-12-2002, 12:22 PM
  5. adding bytes to EXE to call another EXE
    By lliero in forum C Programming
    Replies: 2
    Last Post: 03-30-2002, 07:23 AM