hide.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:hide.py 作者: nukeop 项目源码 文件源码
def decode(image, password=""):
    im = Image.open(image)
    px = im.load()

    data = ""

    #Decode the contents of the hidden data
    for i in range(im.height):
        for j in range(im.width):
            data += decode_from_pixel(px[j, i])

    #Optional decryption step
    if len(password) > 0:
        nonce = data[:16]

        #Use key stretching to generate a secure key
        kdf = PBKDF2HMAC(
            algorithm=hashes.SHA256(),
            length=32,
            salt=bytes(password),
            iterations=100000,
            backend=default_backend())

        key = kdf.derive(bytes(password))

        cipher = Cipher(algorithms.AES(key),\
                        modes.CTR(nonce), backend=default_backend())

        dec = cipher.decryptor()
        data = dec.update(data[16:]) + dec.finalize()

    #Create the header for reading
    header = Header()

    headerdata = struct.unpack("4s"+\
                               "I"+\
                               str(Header.MAX_FORMAT_LENGTH)+"s",
                                data[:4+4+Header.MAX_FORMAT_LENGTH])
    header.magicnum = headerdata[0]
    header.size = headerdata[1]
    header.fformat = headerdata[2].strip("\x00")

    #Verify integrity of recovered data
    if header.magicnum != Header.magicnum:
        print "There is no data to recover, quitting"
        exit()

    data = data[4+Header.MAX_FORMAT_LENGTH:4+Header.MAX_FORMAT_LENGTH+header.size]

    print "Saving decoded output as {}"\
        .format("output"+os.extsep+header.fformat)
    with open("output"+os.extsep+header.fformat, 'wb') as outf:
        outf.write(data)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号