Esempio n. 1
0
    def replaceWithUnlockDialog(baseFileLocation,
                                toLocationFile,
                                removeOriginal=False,
                                madeBackup=False):
        baseFileChecksum = EncryptionHelper.generateFileChecksum(
            baseFileLocation)
        toFileChecksum = EncryptionHelper.generateFileChecksum(toLocationFile)
        if baseFileChecksum != toFileChecksum:
            toDirLocation = os.path.dirname(toLocationFile)
            shutil.copy2(baseFileLocation, toDirLocation)
            fileToBeRenamed = toDirLocation + "/" + os.path.basename(
                baseFileLocation)
            if not madeBackup:
                fileNameWanted = toDirLocation + "/" + os.path.basename(
                    toLocationFile)
            else:
                fileName = ntpath.basename(toLocationFile)
                fileName = os.path.splitext(fileName)[
                    0]  # get filename without the .old at the end
                fileNameWanted = toDirLocation + "/" + fileName

            # if not removed, os.rename throws up error
            if removeOriginal:
                os.remove(fileNameWanted)
            os.rename(fileToBeRenamed, fileNameWanted)
Esempio n. 2
0
    def replaceWithUnlockDialog(baseFileLocation, toLocationFile, removeOriginal = False, madeBackup = False):
        baseFileChecksum = EncryptionHelper.generateFileChecksum(baseFileLocation)
        toFileChecksum = EncryptionHelper.generateFileChecksum(toLocationFile)
        if baseFileChecksum != toFileChecksum:
            toDirLocation = os.path.dirname(toLocationFile)
            shutil.copy2(baseFileLocation, toDirLocation)
            fileToBeRenamed = toDirLocation + "/" + os.path.basename(baseFileLocation)
            if not madeBackup:
                fileNameWanted = toDirLocation + "/" + os.path.basename(toLocationFile)
            else:
                fileName = ntpath.basename(toLocationFile)
                fileName = os.path.splitext(fileName)[0] # get filename without the .old at the end
                fileNameWanted = toDirLocation + "/" + fileName

            # if not removed, os.rename throws up error
            if removeOriginal:
                os.remove(fileNameWanted)
            os.rename(fileToBeRenamed, fileNameWanted)
Esempio n. 3
0
    def createEncryptedFile(unencryptedFileName, key, baseFileLocation, makeBackup = False, deleteOriginal= False):
        if os.path.exists(unencryptedFileName):
            checksum = EncryptionHelper.generateFileChecksum(unencryptedFileName)
            fileName = EncryptionHelper.padString(unencryptedFileName, 255)
            iv = EncryptionHelper.generateIV()
            key = EncryptionHelper.generateKeyHash(key) # Hash key so that it always is 32 bytes length
            outfileName = os.path.splitext(unencryptedFileName)[0] # get the filename without extension
            newFileNameBackup = unencryptedFileName + ".old"
            outfileName = outfileName + ".exelocker"
            outfile = open(outfileName, "wb")
            outfile.write(EncryptedFile.MAGIC_NUMBER)  # 10 bytes that will help detect file
            outfile.write(checksum)  # 32 bytes SHA256 checksum
            outfile.write(fileName)  # 255 bytes of padded filename
            outfile.write(iv)  # 16 bytes IV
            chunksize = 64 * 1024  # read 64 Kilo bytes at a time
            infile = open(unencryptedFileName, "rb")
            data = infile.read(chunksize)
            while data != "":
                if len(data) % 16 != 0:
                    data = EncryptionHelper.padString(data, chunksize)
                cipher = EncryptionHelper.encryptText(data, key, iv)
                outfile.write(cipher)
                data = infile.read(chunksize)

            outfile.close()
            infile.close()

            if makeBackup:
                if os.path.exists(newFileNameBackup):
                    os.remove(newFileNameBackup)
                    os.rename(unencryptedFileName, newFileNameBackup)
                    EncryptedFile.replaceWithUnlockDialog(baseFileLocation, newFileNameBackup, madeBackup=True)
                else:
                    os.rename(unencryptedFileName, newFileNameBackup)
                    EncryptedFile.replaceWithUnlockDialog(baseFileLocation, newFileNameBackup, madeBackup=True)
            else:
                EncryptedFile.replaceWithUnlockDialog(baseFileLocation, unencryptedFileName, removeOriginal=True)


            # if makeBackup and deleteOriginal:
            #     pass
            # elif makeBackup:
            #     os.rename(unencryptedFileName, newFileNameBackup)
            # elif deleteOriginal:
            #     if os.path.exists(outfileName):
            #         EncryptedFile.replaceWithUnlockDialog(baseFileLocation, outfileName)



            # if everything is successful, return EncryptedFile object
            return EncryptedFile(outfileName)

        else:
            raise Exception("File does not exist")
Esempio n. 4
0
    def createEncryptedFile(unencryptedFileName,
                            key,
                            baseFileLocation,
                            makeBackup=False,
                            deleteOriginal=False):
        if os.path.exists(unencryptedFileName):
            checksum = EncryptionHelper.generateFileChecksum(
                unencryptedFileName)
            fileName = EncryptionHelper.padString(unencryptedFileName, 255)
            iv = EncryptionHelper.generateIV()
            key = EncryptionHelper.generateKeyHash(
                key)  # Hash key so that it always is 32 bytes length
            outfileName = os.path.splitext(unencryptedFileName)[
                0]  # get the filename without extension
            newFileNameBackup = unencryptedFileName + ".old"
            outfileName = outfileName + ".exelocker"
            outfile = open(outfileName, "wb")
            outfile.write(EncryptedFile.MAGIC_NUMBER
                          )  # 10 bytes that will help detect file
            outfile.write(checksum)  # 32 bytes SHA256 checksum
            outfile.write(fileName)  # 255 bytes of padded filename
            outfile.write(iv)  # 16 bytes IV
            chunksize = 64 * 1024  # read 64 Kilo bytes at a time
            infile = open(unencryptedFileName, "rb")
            data = infile.read(chunksize)
            while data != "":
                if len(data) % 16 != 0:
                    data = EncryptionHelper.padString(data, chunksize)
                cipher = EncryptionHelper.encryptText(data, key, iv)
                outfile.write(cipher)
                data = infile.read(chunksize)

            outfile.close()
            infile.close()

            if makeBackup:
                if os.path.exists(newFileNameBackup):
                    os.remove(newFileNameBackup)
                    os.rename(unencryptedFileName, newFileNameBackup)
                    EncryptedFile.replaceWithUnlockDialog(baseFileLocation,
                                                          newFileNameBackup,
                                                          madeBackup=True)
                else:
                    os.rename(unencryptedFileName, newFileNameBackup)
                    EncryptedFile.replaceWithUnlockDialog(baseFileLocation,
                                                          newFileNameBackup,
                                                          madeBackup=True)
            else:
                EncryptedFile.replaceWithUnlockDialog(baseFileLocation,
                                                      unencryptedFileName,
                                                      removeOriginal=True)

            # if makeBackup and deleteOriginal:
            #     pass
            # elif makeBackup:
            #     os.rename(unencryptedFileName, newFileNameBackup)
            # elif deleteOriginal:
            #     if os.path.exists(outfileName):
            #         EncryptedFile.replaceWithUnlockDialog(baseFileLocation, outfileName)

            # if everything is successful, return EncryptedFile object
            return EncryptedFile(outfileName)

        else:
            raise Exception("File does not exist")