def encrypt_message(message): config = cherrypy.request.app.config["filelocker"] try: message.encryption_key = Encryption.generatePassword() f = open(os.path.join(config["vault"], "m%s" % str(message.id)), "wb") encrypter, salt = Encryption.new_encrypter(message.encryption_key) padding, endOfFile = (0, False) newFile = StringIO.StringIO(message.body) f.write(salt) data = newFile.read(1024 * 8) # If File is only one block long, handle it here if len(data) < (1024 * 8): padding = 16 - (len(data) % 16) if padding == 16: paddingByte = "%X" % 0 else: paddingByte = "%X" % padding for i in range(padding): data += paddingByte f.write(encrypter.encrypt(data)) else: while 1: if endOfFile: break else: next_data = newFile.read(1024 * 8) # this only happens if we are at the end, meaning the next block is the last # so we have to handle the padding by aggregating the two blocks and determining pad if len(next_data) < (1024 * 8): data += next_data padding = 16 - (len(data) % 16) if padding == 16: paddingByte = "%X" % 0 else: paddingByte = "%X" % padding for i in range(padding): data += paddingByte endOfFile = True f.write(encrypter.encrypt(data)) data = next_data newFile.close() f.close() except IOError, ioe: cherrypy.log.error( "[%s] [encrypt_message] [There was an IOError while checking in new file: %s]" % (message.owner_id, str(ioe)) ) raise Exception( "There was an IO error while uploading: %s. The administrator has been notified of this error." % str(ioe) )
def encrypt_message(message): config = cherrypy.request.app.config['filelocker'] try: message.encryption_key = Encryption.generatePassword() f = open(os.path.join(config['vault'], "m%s" % str(message.id)), "wb") encrypter, salt = Encryption.new_encrypter(message.encryption_key) padding, endOfFile = (0, False) newFile = StringIO.StringIO(message.body) f.write(salt) data = newFile.read(1024 * 8) #If File is only one block long, handle it here if len(data) < (1024 * 8): padding = 16 - (len(data) % 16) if padding == 16: paddingByte = "%X" % 0 else: paddingByte = "%X" % padding for i in range(padding): data += paddingByte f.write(encrypter.encrypt(data)) else: while 1: if endOfFile: break else: next_data = newFile.read(1024 * 8) #this only happens if we are at the end, meaning the next block is the last #so we have to handle the padding by aggregating the two blocks and determining pad if len(next_data) < (1024 * 8): data += next_data padding = 16 - (len(data) % 16) if padding == 16: paddingByte = "%X" % 0 else: paddingByte = "%X" % padding for i in range(padding): data += paddingByte endOfFile = True f.write(encrypter.encrypt(data)) data = next_data newFile.close() f.close() except IOError, ioe: cherrypy.log.error( "[%s] [encrypt_message] [There was an IOError while checking in new file: %s]" % (message.owner_id, str(ioe))) raise Exception( "There was an IO error while uploading: %s. The administrator has been notified of this error." % str(ioe))
(ftype, lo) = data.split(";") del(lo) flFile.type = ftype.strip() else: flFile.type = data.strip() except Exception, e: cherrypy.log.error("[%s] [checkInFile] [Unable to determine file type: %s]" % (user.id, str(e))) #Logic is a little strange here - if the user supplied an encryptionKey, then don't save it with the file encryptionKey = None flFile.encryption_key = Encryption.generatePassword() encryptionKey = flFile.encryption_key os.umask(077) newFile = open(filePath, "rb") f = open(os.path.join(config['vault'], str(flFile.id)), "wb") encrypter, salt = Encryption.new_encrypter(encryptionKey) padding, endOfFile = (0, False) f.write(salt) data = newFile.read(1024*8) #If File is only one block long, handle it here if len(data) < (1024*8): padding = 16-(len(data)%16) if padding == 16: paddingByte = "%X" % 0 else: paddingByte = "%X" % padding for i in range(padding): data+=paddingByte f.write(encrypter.encrypt(data)) else: while 1: if endOfFile: break