def encrypt(self): """ Encode in base64 the string/file, create a PNG file with previously selected colors; encrypt in AES-256 characters + RGB values dataset, and append to image as zTXt chunk; save PNG file with uuid4 filename or return image PIL object depending on directory parameter. """ if path.isfile(self.data): with open(self.data, "rb") as file: self.data = urlsafe_b64encode(file.read()).decode('utf8') file.close() else: self.data = urlsafe_b64encode(self.data.encode()).decode('utf8') encrypted = self.encrypt_aes256(dataset=None) self.data = encrypted["CipherData"] colorslist, dataset = self.colors_list(self.data) imgsize = int(sqrt(len(colorslist))) img = Image.new(mode = "RGB", size = (imgsize, imgsize)) img.putdata(colorslist) encdataset = self.encrypt_aes256(str(dataset)) metadata = PngInfo() metadata.add_text("Dataset", encdataset["CipherData"], zip = True) metadata.add_text("DatasetNonce", encdataset["Nonce"], zip = True) metadata.add_text("DatasetSalt", encdataset["Salt"], zip = True) metadata.add_text("DatasetTag", encdataset["Tag"], zip = True) metadata.add_text("DataNonce", encrypted["Nonce"], zip = True) metadata.add_text("DataSalt", encrypted["Salt"], zip = True) metadata.add_text("DataTag", encrypted["Tag"], zip = True) if not self.directory: b = BytesIO() img.save(b, "PNG", pnginfo = metadata) b.seek(0) return b img.save(self.directory + sep + str(uuid4()) + ".png", "PNG", pnginfo = metadata)
def b64name_dm(user1, user2): '''Generate b64name If between users, use email + email :return: encrypted base64 ''' list = [user1, user2] list.sort() data = ','.join(list) return pybase64.urlsafe_b64encode(data.encode()).decode()
def encrypt_aes256(self, dataset): """ Encrypt a string in aes256, returning a dictionary with ciphered text, salt, nonce, tag. """ salt = get_random_bytes(AES.block_size) privatekey = scrypt(self.password.encode(), salt = salt, n = 2 ** 14, r = 8, p = 1, dklen = 32) if "Intel" in self.cpuinfo: config = AES.new(privatekey, AES.MODE_GCM, use_aesni = True) else: config = AES.new(privatekey, AES.MODE_GCM) if dataset: cipherdata, tag = config.encrypt_and_digest(bytes(dataset, 'utf-8')) else: cipherdata, tag = config.encrypt_and_digest(bytes(self.data, 'utf-8')) return { "CipherData": urlsafe_b64encode(cipherdata).decode('utf-8'), "Nonce": urlsafe_b64encode(config.nonce).decode('utf-8'), "Salt": urlsafe_b64encode(salt).decode('utf-8'), "Tag": urlsafe_b64encode(tag).decode('utf-8') }
def create_message_html(self, sender, to, subject, message_text, html_message_text): message = MIMEMultipart("alternative") message["To"] = to message["From"] = sender message["Subject"] = subject plainText = MIMEText(message_text, "plain") htmlText = MIMEText(html_message_text, "html") message.attach(plainText) message.attach(htmlText) raw = urlsafe_b64encode(message.as_bytes()) raw = raw.decode() return {'raw': raw}
def create_message(self, sender, to, subject, message_text): """Create a message for an email. Args: sender: Email address of the sender. to: Email address of the receiver. subject: The subject of the email message. message_text: The text of the email message. Returns: An object containing a base64url encoded email object. """ message = MIMEText(message_text) message['to'] = to message['from'] = sender message['subject'] = subject raw = urlsafe_b64encode(message.as_bytes()) raw = raw.decode() return {'raw': raw}
def image_web_save_encode(image): input_bytes = cv2.imencode(".jpg", image)[1].tostring() input_image = base64.urlsafe_b64encode(input_bytes) image_content = input_image.decode("utf-8") return image_content
def b64name_channel(data): '''Generate b64name if channel, use channel name :return: encrypted base64 ''' return pybase64.urlsafe_b64encode(data.encode()).decode()
def encondeDD(chunck): if (len(chunck) % 16 != 0): chunck += ' ' * (16 - len(chunck) % 16) encoded = pybase64.urlsafe_b64encode(cipher.encrypt(chunck)) return encoded
def _base64_url_encode(data: bytes) -> bytes: return pybase64.urlsafe_b64encode(data).replace(b'=', b'')