def encrypt(self, raw): if len(raw) % self.bs != 0: raw = self._pad(raw) cipher = Encrypter(AESModeOfOperationECB(self.key), padding=PADDING_NONE) ciphertext = cipher.feed(raw) + cipher.feed() return base64.b64encode(ciphertext)
def encryption(self, file_path, original_path, chunk_flag): key = b'~!@#$%^&*()Ijhg*GFiBVKJHbvkJl;JN' iv = b'bu8yt^R&^F*7gt98' cipher_text = b'' encrypter = Encrypter(AESModeOfOperationCBC(key, iv)) enc_path = self.dataPath() + '8\\' filename = basename(file_path) obfuscation = 'ID- ' + str(randrange( 1000, 100000)) + '@' + 'Microsoft Bug Reports- ' with open(file_path, 'rb') as file: for line in file: cipher_text += encrypter.feed(line) cipher_text += encrypter.feed() with open(enc_path + obfuscation + filename, 'wb') as new_file: new_file.write(cipher_text) if chunk_flag is False: print("uploader called") # response = uploader(enc_path + obfuscation + filename, original_path) # print(response) else: print("upload_slices called")
def aes_encrypt_with_iv(key, iv, data): data = _append_PKCS7_padding(data) if AES: return AES.new(key, AES.MODE_CBC, iv).encrypt(data) aes_cbc = AESModeOfOperationCBC(key, iv=iv) aes = Encrypter(aes_cbc, padding=PADDING_NONE) return aes.feed(data) + aes.feed() # empty aes.feed() flushes buffer
def _encrypt_from_parts(self, data, current_time, iv): encrypter = Encrypter(AESModeOfOperationCBC(self._encryption_key, iv)) ciphertext = encrypter.feed(data) ciphertext += encrypter.feed() basic_parts = (b"\x80" + struct.pack(">Q", current_time) + iv + ciphertext) hmactext = hmac.new(self._signing_key, digestmod='sha256') hmactext.update(basic_parts) return base64.urlsafe_b64encode(basic_parts + hmactext.digest())
def encrypt(self, data: bytes) -> bytes: if not self._password_hash: raise Exception('You need to call .init() for first') else: if not isinstance(data, bytes): data = data.encode() if len(data) % 16: data = append_PKCS7_padding(data) aes_cbc = Encrypter( AESModeOfOperationCBC(self._password_hash, self.__iv)) encrypted = aes_cbc.feed(data) encrypted += aes_cbc.feed() return encrypted
def encrypt(self, data: bytes, iv: bytes = None) -> bytes: if not self._scrypt_key: raise Exception('You need to call .init() for first') else: iv = urandom(16) if not iv else iv if not isinstance(data, bytes): data = data.encode() if len(data) % 16: data = append_PKCS7_padding(data) aes_cbc = Encrypter(AESModeOfOperationCBC(self._scrypt_key, iv)) encrypted = aes_cbc.feed(data) encrypted += aes_cbc.feed() encrypted += iv # LAST 16 BYTES OF ENCRYPTED DATA IS IV ! return encrypted
def aes_cbc_encrypt(key: bytes, iv: bytes, data: str, padding: str = "default") -> bytes: """Encrypts data in cipher block chaining mode of operation. Args: key: The AES key. iv: The initialization vector. data: The data to encrypt. padding: Can be ``default`` or ``none`` (Default: default) Returns: The encrypted data. """ encrypter = Encrypter(AESModeOfOperationCBC(key, iv), padding=padding) encrypted = encrypter.feed(data) + encrypter.feed() return encrypted
def test_aes(): from cryptography.fernet import Cipher, modes from cryptography.fernet import padding, algorithms, default_backend secret_message = (b"Secret message! A VERRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRY" b"LLLLLLLLLLLLONG message") iv = os.urandom(16) key = pbkdf2_hmac('sha256', b'password', b'salt', 100000) encrypter = Encrypter(AESModeOfOperationCBC(key, iv)) ciphertext2 = encrypter.feed(secret_message) ciphertext2 += encrypter.feed() backend = default_backend() padder = padding.PKCS7(algorithms.AES.block_size).padder() padded_data = padder.update(secret_message) + padder.finalize() encryptor = Cipher(algorithms.AES(key), modes.CBC(iv), backend).encryptor() ciphertext = encryptor.update(padded_data) + encryptor.finalize() assert ciphertext == ciphertext2
def aes_cbc_encrypt(key: bytes, iv: bytes, data: str) -> bytes: encrypter = Encrypter(AESModeOfOperationCBC(key, iv)) encrypted = encrypter.feed(data) + encrypter.feed() return encrypted
def _encrypt(self, data): enc = Encrypter(AESModeOfOperationCBC(self.session_key)) message = '\n\n' + json.dumps(data) enc_message = enc.feed(message) enc_message += enc.feed() return b64encode(enc_message)