Untitled

mail@pastecode.io avatar
unknown
python
a year ago
1.4 kB
2
Indexable
Never
# This is a sample Python script.
import json

# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.
import qrcode
from sys import getsizeof
import cv2
from cryptography.fernet import Fernet
from instagrapi import Client

key = (("1" * 43) + "=").encode()


def write():
    pre_data = {'user': 1, 'password': 2, 'date': '12.02.2019'}
    data = json.dumps(pre_data, indent = 4)
    print("Text to image: " + data)

    # https://i.stack.imgur.com/KzV6s.png
    # print(getsizeof(data))
    fernet = Fernet(key)
    enc_message = fernet.encrypt(data.encode())
    print("Text to image encrypted: " + enc_message.decode())
    # print(getsizeof(enc_message))

    qr = qrcode.QRCode(
        version=None,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        box_size=10,
        border=4, )
    qr.add_data(str(enc_message.decode()))
    qr.make(fit=True)

    img = qr.make_image(fill_color="black", back_color="white")

    type(img)
    img.save("some_file.png")


def read():
    fernet = Fernet(key)

    img = cv2.imread("some_file.png")
    det = cv2.QRCodeDetector()
    val, pts, st_code = det.detectAndDecode(img)
    print("Text from image: " + val)
    decMessage = fernet.decrypt(val.encode()).decode()
    print("Text from image decrypted: " + decMessage)


if __name__ == '__main__':
    write()
    read()