Okay, this is what I am trying to do. I am attempting to create my own encrypter. I have the base encryption running, used just to encrypt a string you put in. I am going to add functionality to open a text file's contents and encrypt them, outputing them into another text file. I will be working on that later, so I might post up my questions if I can't figure it out. Anyways, this is what I need help with:
1) How do I open a file's binary code, and convert it to hex?
2) After opening it in hex, it shoud become manipulatable, such as, I can make it into a list, then change each odd charecter, correct?
3) After messing with the hex, how do I convert it back into binary, then save it?
Thank you very much in advance!
~ Hondros

Recommended Answers

All 5 Replies

Which version of Python are you using, it makes a difference.

I am using python 2.6 for windows.

Here is an example for Python25, it should work with Python26 ...

# read a binary data file
# and create a list of 2 digit hexadecimals
# the test data file contains the string 'zoomer'

try:
    # pick a file you have in the working directory
    # or give the full file path ...
    data_file = 'zoomer.txt'
    # open it for binary read
    fin = open(data_file, "rb")
    data = fin.read()
    fin.close()
except IOError:
    print("File %s not found" % data_file)
    raise SystemExit

# testing content and type (test only with text files)
print(data, type(data))  # ('zoomer', <type 'str'>)

# create a list of hexadecimal data
hex_list = []
# iterate the data string byte by byte
for bc in data:
    # convert character into a 2 digit hex
    hexbyte = "%02X" % ord(bc)
    hex_list.append(hexbyte)

print(hex_list)

# manipulate the hex_list and write the binary file back out
# make the 'z' a 'b'
print("%02X" % ord('b'))  # 62
hex_list[0] = '62'

# rebuild the string for the binary write
out_str = ''.join([chr(int(x, 16)) for x in hex_list])

# write the modified binary file out and check it with an editor
# it should show the text 'boomer'
fout = open("zoomer_b.txt", "wb")
fout.write(out_str)
fout.close()

"""
my result with Python25 -->
('zoomer', <type 'str'>)
['7A', '6F', '6F', '6D', '65', '72']
62
"""

Note:
Binary files are most often image files that contain a fair number of unprintable characters, so avoid test printing the contents. I used a text file here to make things easier to understand.

This'll only work with the readable charecters, correct? I am planning on using this for an entire file, not only a .txt. Like, for example, a .exe, .jpg, .etc. Sorry for not making that clear in the first post. >> I feel a bit stupid now. Anyways, how would I get the hex values of the entire file? I understand that "fin = open(data_file, "rb")" is opening the file. What is the "rb", and is this possible with any file? I'm thinking it is, and the ord() gives the ascii code for the given string charecter. I don't think I need that, because I'm not dealing with the actual charecters.

This will work with any binary file. Just don't print the raw contents of the file, it will give you a mess!

Also take a look at:
http://www.daniweb.com/forums/thread71188.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.