Home:ALL Converter>Python read text file as binary?

Python read text file as binary?

Ask Time:2015-06-01T06:01:20         Author:user2514631

Json Formatter

I was trying to build an encryption program in python 2.7. It would read the binary from a file and then use a key to encrypt it. However, I quickly ran into a problem. Files like image files and executables read as hex values. However, text files do not using open(). Even if i run

file=open("myfile.txt", "rb")

out=file.read()

it still comes out as just text. I'm on windows 7, not linux which i think may make a difference. Is there any way i could read the binary from ANY file (including text files), not just image and executable files?

Author:user2514631,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30563177/python-read-text-file-as-binary
Ynon :

Even when reading a file with the 'rb' flag,\nif your file has the byte '\\x41' it will be printed as the letter 'A' in the console.\nIf you want the hex values, encode the file content as hex, which means:\ncontent = open('text.txt', 'rb').read()\n# Since python 3.5:\nhex = content.hex()\n# else:\nhex = content.encode('hex')\n",
2017-01-22T20:10:04
Ashouri :

Take a look at below code .also it has many points for you\n\nfrom hashlib import md5\nfrom Crypto.Cipher import AES\nfrom Crypto import Random\n\ndef derive_key_and_iv(password, salt, key_length, iv_length):\n d = d_i = ''\n while len(d) < key_length + iv_length:\n d_i = md5(d_i + password + salt).digest()\n d += d_i\n return d[:key_length], d[key_length:key_length+iv_length]\n\ndef encrypt(in_file, out_file, password, key_length=32):\n bs = AES.block_size\n salt = Random.new().read(bs - len('Salted__'))\n key, iv = derive_key_and_iv(password, salt, key_length, bs)\n cipher = AES.new(key, AES.MODE_CBC, iv)\n out_file.write('Salted__' + salt)\n finished = False\n while not finished:\n chunk = in_file.read(1024 * bs)\n if len(chunk) == 0 or len(chunk) % bs != 0:\n padding_length = (bs - len(chunk) % bs) or bs\n chunk += padding_length * chr(padding_length)\n finished = True\n out_file.write(cipher.encrypt(chunk))\n\ndef decrypt(in_file, out_file, password, key_length=32):\n bs = AES.block_size\n salt = in_file.read(bs)[len('Salted__'):]\n key, iv = derive_key_and_iv(password, salt, key_length, bs)\n cipher = AES.new(key, AES.MODE_CBC, iv)\n next_chunk = ''\n finished = False\n while not finished:\n chunk, next_chunk = next_chunk, cipher.decrypt(in_file.read(1024 * bs))\n if len(next_chunk) == 0:\n padding_length = ord(chunk[-1])\n chunk = chunk[:-padding_length]\n finished = True\n out_file.write(chunk)\n\n\n\n\nUsage\n\n\n\nwith open(in_filename, 'rb') as in_file, open(out_filename, 'wb') as out_file:\n encrypt(in_file, out_file, password)\nwith open(in_filename, 'rb') as in_file, open(out_filename, 'wb') as out_file:\n decrypt(in_file, out_file, password)\n",
2015-05-31T22:10:04
yy