def test_enc_pyAesCrypt_dec_AesCrypt(self): for pt, ct, ou in zip(filenames, encfilenames, decfilenames): # encrypt file pyAesCrypt.encryptFile(pt, ct, password, bufferSize) # decrypt file subprocess.call(["aescrypt", "-d", "-p", password, "-o", ou, ct]) # check that the original file and the output file are equal self.assertTrue(filecmp.cmp(pt, ou))
def test_enc_pyAesCrypt_dec_pyAesCrypt(self): for pt, ct, ou in zip(filenames, encfilenames, decfilenames): # encrypt file pyAesCrypt.encryptFile(pt, ct, password, bufferSize) # decrypt file pyAesCrypt.decryptFile(ct, ou, password, bufferSize) # check that the original file and the output file are equal self.assertTrue(filecmp.cmp(pt, ou))
def putFile(fname): ''' Processing a file before uploading it ''' #Integritiy integrity.addHash(fname) #Be Careful : integrity perform before crypto : so getFile must have this two function in reverse order #Crypto crypto.encryptFile(fname) #add encryption
def test_dec_wrongpass(self): # encrypt file pyAesCrypt.encryptFile(self.tfile, self.tfile + '.aes', password, bufferSize) # try to decrypt file using a wrong password # and check that ValueError is raised self.assertRaisesRegex(ValueError, ("Wrong password " "\(or file is corrupted\)."), pyAesCrypt.decryptFile, self.tfile + '.aes', self.tfile + '.decr', 'wrongpass', bufferSize) # check that decrypted file was not created self.assertFalse(isfile(self.tfile + '.decr'))
def test_dec_unsupported_AesCrypt_format(self): # encrypt file pyAesCrypt.encryptFile(self.tfile, self.tfile + '.aes', password, bufferSize) # corrupt the 4th byte corruptFile(self.tfile + '.aes', 3) # try to decrypt file # ...and check that ValueError is raised self.assertRaisesRegex(ValueError, ("pyAesCrypt is only " "compatible with version 2 of " "the AES Crypt file format."), pyAesCrypt.decryptFile, self.tfile + '.aes', self.tfile + '.decr', password, bufferSize) # check that decrypted file was not created self.assertFalse(isfile(self.tfile + '.decr'))
def test_dec_not_AesCrypt_format(self): # encrypt file pyAesCrypt.encryptFile(self.tfile, self.tfile + '.aes', password, bufferSize) # corrupt the 2nd byte (the 'E' of 'AES') - offset is 2-1=1 corruptFile(self.tfile + '.aes', 1) # try to decrypt file # ...and check that ValueError is raised self.assertRaisesRegex(ValueError, ("File is corrupted or " "not an AES Crypt " "\(or pyAesCrypt\) file."), pyAesCrypt.decryptFile, self.tfile + '.aes', self.tfile + '.decr', password, bufferSize) # check that decrypted file was not created self.assertFalse(isfile(self.tfile + '.decr'))
def test_dec_trunc_file(self): # encrypt file pyAesCrypt.encryptFile(self.tfile, self.tfile + '.aes', password, bufferSize) # get file size fsize = os.stat(self.tfile + '.aes').st_size # truncate hmac (i.e.: truncate end of the file) with open(self.tfile + '.aes', 'r+b') as ftc: ftc.truncate(fsize - 1) # try to decrypt file # ...and check that ValueError is raised self.assertRaisesRegex(ValueError, "File is corrupted.", pyAesCrypt.decryptFile, self.tfile + '.aes', self.tfile + '.decr', password, bufferSize) # check that decrypted file was deleted self.assertFalse(isfile(self.tfile + '.decr'))
def test_dec_bad_hmac(self): # encrypt file pyAesCrypt.encryptFile(self.tfile, self.tfile + '.aes', password, bufferSize) # get file size fsize = os.stat(self.tfile + '.aes').st_size # corrupt hmac corruptFile(self.tfile + '.aes', fsize - 1) # try to decrypt file # ...and check that ValueError is raised self.assertRaisesRegex(ValueError, ("Bad HMAC " "\(file is corrupted\)."), pyAesCrypt.decryptFile, self.tfile + '.aes', self.tfile + '.decr', password, bufferSize) # check that decrypted file was deleted self.assertFalse(isfile(self.tfile + '.decr'))
def uploadFile(self, path: str, parentFolderId: str = ""): """ Path must be absolute path to the file, and must also include file name and extension Path must use / and not \\ You can use utils.sanitizeFilePath() to convert \\ to / """ if parentFolderId != "": parents = [parentFolderId] else: parents = [self.config['PARENT_FOLDER']] if '/' in path: fileName = path.split('/')[-1] else: fileName = path.split('\\')[-1] fileMetadata = { 'name': fileName, 'parents': parents } # TODO: Encrypt file here crypto.encryptFile(path, self.config['PASSWORD']) # logic to find mime types _mimeType = mimetypes.guess_type(path) mimeType = _mimeType[0] if mimeType == None: raise Exception(f'Mime type of file: {fileName} could not be guessed.') media = MediaFileUpload('encrypted.file', mimetype=mimeType) response = self.service.files().create( body=fileMetadata, media_body=media, fields='id' ).execute() return fileName
import crypto print(crypto.encryptFile('test.txt', 'password')) print('---------------------------------------') print(str(crypto.decryptFile('encrypted.file', 'password'), 'utf-8'))