Exemple #1
0
 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 #2
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")
Exemple #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")