Exemple #1
0
 def decryptFile(self,  key, sameLocation = False):
     if sameLocation == False:
         outfile = open(self.getOriginalFileName(), "wb")
     else:
         outfileName = os.path.join(os.path.dirname(self.encryptedFileName), self.getOriginalFileName())
         outfile = open(outfileName, "wb")
         # change the name of the encryptedFileName because right now it will be .exelocker file, change it to original extension
     data = self._handle.read(EncryptedFile._CHUNK_SIZE)
     cycle = 1
     while data != "":
         message = EncryptionHelper.decryptCipher(data, key, self._iv)
         if cycle == self._cycles:
             message = EncryptionHelper.stripPadding(message)
         outfile.write(message)
         cycle = cycle + 1
         data = self._handle.read(EncryptedFile._CHUNK_SIZE)
     outfile.close()
 def test_decryptFileWithSameLocation(self):
     validFilePath = r"C:\Users\Gulyani\Dropbox\work\new work\EXE locker\EXE_locker\test_file\BTSync.exelocker"
     eFile = EncryptedFile(validFilePath)
     password = EncryptionHelper.generateKeyHash("password")
     sameLocation = True
     eFile.decryptFile(password, sameLocation)
     decryptedFileName = eFile.getOriginalFileName()
     unencryptedFileLocation = os.path.join(os.path.dirname(validFilePath), decryptedFileName)
     self.assertTrue(os.path.exists(unencryptedFileLocation))
Exemple #3
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)
 def test_encryption(self):
     originalMessage = EncryptionHelper.padString("Hello")
     key = EncryptionHelper.generateKeyHash("world")
     iv = EncryptionHelper.generateIV()
     cipher = EncryptionHelper.encryptText(originalMessage, key, iv)
     self.assertNotEqual(originalMessage, cipher)
     message = EncryptionHelper.decryptCipher(cipher, key, iv)
     self.assertEqual(EncryptionHelper.stripPadding(message), EncryptionHelper.stripPadding(originalMessage))
Exemple #5
0
 def __init__(self, encryptedFileName):
     if os.path.exists(encryptedFileName) and EncryptedFile.isValidFile(encryptedFileName):
         self.encryptedFileName = encryptedFileName
         self._handle = open(encryptedFileName, "rb")
         self._handle.read(10) # skip reading magic number
         self._checksum = self._handle.read(32) # read 32 bytes checksum from file
         self._filename = EncryptionHelper.stripPadding(self._handle.read(255)) # read filename
         # and then strip padding
         self._iv = self._handle.read(16) # read 16 bytes IV
         self._cycles = (os.stat(encryptedFileName).st_size) / (64 * 1024) # divided by 64kbytes
     else:
         raise Exception("File does not exist or not a valid EXELocker File.")
Exemple #6
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")
    def test_generateFileChecksum(self):
        text = "Hello world"
        outfile = open("testfile", "wb+")
        outfile.write(text)
        fileHash = SHA256.new()
        textHash = EncryptionHelper.generateKeyHash(text)
        outfile.seek(0, os.SEEK_SET) # bring file pointer to the beginning
        chunksize = 64 * 1024 # read 64 kilo bytes at a time
        data = outfile.read(chunksize)
        self.assertEquals(text, data)

        while data != "":
            fileHash.update(data)
            data = outfile.read(chunksize)


        outfile.close()
        self.assertEquals(textHash, fileHash.digest())
 def test_decryptFile(self):
     validFilePath = r"C:\Users\Gulyani\Dropbox\work\new work\EXE locker\EXE_locker\test_file\BTSync.exelocker"
     eFile = EncryptedFile(validFilePath)
     password = EncryptionHelper.generateKeyHash("password")
     eFile.decryptFile(password)
     self.assertTrue(os.path.exists(eFile.getOriginalFileName()))
 def test_generateIV(self):
     iv = EncryptionHelper.generateIV()
     self.assertEquals(16, len(iv))
 def test_generateKeyHash(self):
     key = "password"
     hashedKey = EncryptionHelper.generateKeyHash(key)
     self.assertNotEqual(key, hashedKey)
     self.assertEquals(32, len(hashedKey))
 def test_stripPadding(self):
     originalString = "hello"
     paddedString = EncryptionHelper.padString("hello")
     self.assertNotEqual(originalString, paddedString)
     self.assertEquals(originalString, EncryptionHelper.stripPadding(paddedString))
Exemple #12
0
 def test_padString(self):
     self.assertEquals(16, len(EncryptionHelper.padString("hello")))
     self.assertEquals(18, len(EncryptionHelper.padString("hello", 18)))
Exemple #13
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")