Beispiel #1
0
def AES_encrypt(data="這是測試用的訊息"):
    # s = time.time()
    # 初始化加密資料
    temp_Um = copy.deepcopy(Um)
    key = copy.deepcopy(X[0])
    aes = AEScharp()
    try:
        dict1 = json.loads(request.get_data())
        data = dict1['data']
        use_key = hashlib.sha256(dict1['key'].encode('utf-8')).digest()
        use_key = list(use_key)
        for i in range(32):
            temp_Um[i] = round(temp_Um[i] + use_key[i], 7)
        # print(temp_Um)
    except:
        pass
    # 加密資料
    sendData = aes.encrypt(data, key)
    global testEN
    testEN = sendData
    # json 黨製作
    sendData = {'encrypt_text': str(sendData), 'Um': str(temp_Um)}
    # e = time.time()
    # print(e - s)
    return json.dumps(sendData)
Beispiel #2
0
def decrypt():
    # 初始化解碼資料
    # s = time.time()
    try:
        # 嘗試
        dict1 = json.loads(request.get_data())
        data = dict1['data']
        data = eval(data)
        temp_Um = dict1['Um']
        temp_Um = temp_Um[1:-1].split(", ")
        use_key = hashlib.sha256(dict1['key'].encode('utf-8')).digest()
        use_key = list(use_key)
        # 加密Um
        for i in range(len(temp_Um)):
            use_key[i] = float(use_key[i])
            temp_Um[i] = float(temp_Um[i])
            temp_Um[i] -= use_key[i]
    except:
        print("has error")
        pass
    # print(len(data))
    # 開始同步
    async_flag = False  # 同步旗標
    times = 0  # 同步失敗次數
    while async_flag is False:

        Y = [random.random(), random.random(), random.random()]
        client = Chaos()
        chck = 0
        for i in range(len(temp_Um) - 1, -1, -1):
            Y = client.runSlave(2, Y, temp_Um[i])
            if i == 1:
                chck = client.createUs(Y)
        # 判斷有沒有同步
        if round(temp_Um[0] + chck, 6):
            # print(temp_X, Y[0], client.createUs(Y), temp_Um[0] + chck)
            async_flag = False
            if times > 12:
                break
            times += 1
            print(round(temp_Um[0] + chck, 6))

        else:
            async_flag = True

    # 解密
    aes = AEScharp()
    getData = aes.decrypt(data, Y[0])
    # json 檔製作
    getData = {'decrypt_text': str(getData), 'flag': str(async_flag)}
    # e = time.time()
    # print(e - s)
    return json.dumps(getData)
    def de_json(self, msg):
        aes = AEScharp()
        msg = bytes.fromhex(msg)
        msg = aes.de_ECB(msg, self.key)

        if self.DEBUG == 1:
            print("未處理前: ", msg)

        msg = msg[:msg.find('\x00')]  # 除掉\x00

        if msg[-1] != "}":  # 有些}會不見 處理回來
            msg += "}"

        if self.DEBUG == 1:
            print("處理後: ", msg)
        msg = eval(msg)
        return msg
Beispiel #4
0
def c(msg):
    print("資料來源端:")
    print("密文: ", msg)
    print(type(msg))

    aes = AEScharp()
    key = Y
    print("現在端口的key", key.hex())
    # 將STR 換成 BIN
    msg = bytes.fromhex(msg)
    print("現在端口轉換完MSG(整串)", msg, "長度:", len(msg))
    msg = aes.de_ECB(msg, key)

    print("結果:")
    a = "{\"_target\":\"456\",\"_key\":\"123\"}"
    print(msg.find('\x00'), msg[:msg.find('\x00')])
    msg = msg[:msg.find('\x00')]
    msg = eval(msg)
    print(type(msg), msg)
Beispiel #5
0
import cv2
import numpy as np
from matplotlib import pyplot
import math
from AESmod import AEScharp

aes = AEScharp()

if __name__ == "__main__":

    cam = cv2.VideoCapture(0)
    # for i in range(200):
    (ret, img) = cam.read()
    print(img.shape)
    # result, imgencode = cv2.imencode('.png', img)  # 圖片編碼
    # stringData = imgencode.tostring()  # 轉成bytes

    # print("標頭: ", stringData[stringData.find(b'IDATx\x01\xec\xc1'):stringData.find(b'IDATx\x01\xec\xc1') + 20])
    # head = stringData[:stringData.find(b'IDATx\x01\xec\xc1') + 8]

    # print("標尾: ", stringData[stringData.find(b'IEND') - 4:])
    # tail = stringData[stringData.find(b'IEND') - 4:]

    # e_sss = aes.encrypt_ECB_by(stringData[stringData.find(b'IDATx\x01\xec\xc1') + 8:stringData.find(b'IEND') - 4], "1")

    # en_img = head + e_sss + tail

    # d_sss = aes.decrypt_ECB_by(e_sss, '1').rstrip()
    # de_img = head + d_sss + tail
    # # print(stringData == de_img)
    # # en_img = head + e_sss[:len(stringData) - len(en_img)] + tail
 def encrypt(self):
     aes = AEScharp()
     self.getData = aes.encrypt_ECB(self.data, round(self.key, 4)).hex()
 def decrypt(self):
     aes = AEScharp()
     self.getData = aes.decrypt_ECB(self.data, round(self.Y[0], 4))
 def en_json(self, msg):
     aes = AEScharp()
     msg = aes.en_ECB(str(msg).encode('utf8'), self.key).hex()
     return msg