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))
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))
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_generateKeyHash(self): key = "password" hashedKey = EncryptionHelper.generateKeyHash(key) self.assertNotEqual(key, hashedKey) self.assertEquals(32, len(hashedKey))
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")