Having worked with txt files, I'm learning about binary now. My code below works for a while then bombs out, can you point me towards a method or module to read binary data line by line? The crypto module requires a buffer (don't know how to create one of those) or a string.

from Crypto.Cipher import AES

USEFILE = 'picture.bmp'

class encryptData():
        def __init__(self, file):
            
            blockSize = 32
            self.file = file
            secretKey = os.urandom(blockSize) 
            self.dataFile = open(file, "rb")
            cipher = AES.new(secretKey, AES.MODE_CFB)
            if self.dataFile:
                newFile = self.dataFile.readlines()
                for lines in newFile:
                        print cipher.encrypt(lines)
                
                
            print secretKey
                

run = encryptData(USEFILE)

I'm looking to read a binary file and encrypt it. Many thx.

In line
print cipher.encrypt(lines)
are you sure all the characters are printable?

BTW, Python relies on indentations, please keep them constant.

In line
print cipher.encrypt(lines)
are you sure all the characters are printable?

BTW, Python relies on indentations, please keep them constant.

Sorry, but DaniWeb is starting to give me a lot of double posts lately. I am suspicious that it happen when my darn Virus program updates in the background!

Edited 7 Years Ago by vegaseat: n/a

In line
print cipher.encrypt(lines)
are you sure all the characters are printable?

BTW, Python relies on indentations, please keep them constant.

Looks like it couldn't print all the characters, this line worked without bombing out.

print base64.b16encode(cipher.encrypt(lines))

I wasn't aware of unprintable characters, thx!

This is a good exercise if you're just playing around with the code because you're trying to learn. But if you're actually planning to implement encryption in an application that needs security then you should follow a couple pieces of advise. The firs is that "If you're typing the letters A E S into your code, you're doing it wrong." http://chargen.matasano.com/chargen/2009/7/22/if-youre-typing-the-letters-a-e-s-into-your-code-youre-doing.html The other advise comes from Nate Lawson at Root Labs. "Don't implement your own crypto. Instead use SSL for transport and GPG for data at rest. If your design doesn't seem to fit that model, rework it until it does. If you can't, then use a high-level crypto library like cryptlib, GPGME, or Keyczar."

This question has already been answered. Start a new discussion instead.