--- title: 隱寫術 tags: sprouts slideOptions: theme: solarized transition: 'fade' --- # 隱寫術 我藏我藏 --- ## Ascii - 電腦裡只有數字 - string裡的其實也是數字 - Ascii 表 - https://zh.wikipedia.org/wiki/ASCII ---- ## Ascii (cont) - 65 : 'A' - 97 : 'a' - 48 : '0' ---- ## Method ( ord ) - 獲得一個字元的Ascii值 - e.g. `ord('A')` ---- ## Method ( chr ) - 獲得一個數字所代表的字元 - e.g. `chr(65)` ---- ## 練習 - 輸出 1-255,所有數字的Ascii,並產生如下的表 ```python= 0 = 1 = 2 = 3 = ... ``` --- ## string 延伸 - Format String - `"a {} b"`.format("!=") ---- ## Format string - '{:f}'.format(123.123) - f代表給float表示 - '{:s}'.format("QQAQQ") - s代表給string表示 - '{:d}'.format(17) - d(decimal)代表用10進位表示 - '{:b}'.format(17) - b(binary)代表用2進位表示 - '{:o}'.format(17) - o(octal)代表用8進位表示 - '{:x}'.format(17) - x(hex)代表用16進位表示 ---- ## Format string (cont) - `'{:[char][location][num][k進位制]}'` - 代表以k進位顯示,如果不滿num位,會用char來padding,並且以location(置中、置左、置右)顯示。 - location - `^`置中、`<`置左、`>`置右 - e.g. '{:乂^11s}'.format("Rilak") - '乂乂乂Rilak乂乂乂' ---- ## int的魔力 - int(string,positional notation) - e.g. `int("101",2)` ---- ## Practice - 任意進制到十進制 - 10進制 to other進制 --- ## 位元運算 - 簡單邏輯運算 - bitwise - only for integer ---- ## And | & | 0 | 1 | | -------- | -------- | -------- | | 0 | False | False | | 1 | False | True | ---- ## Or | \| | 0 | 1 | | -------- | -------- | -------- | | 0 | False | True | | 1 | True | True | ---- ## Xor | ^ | 0 | 1 | | -------- | -------- | -------- | | 0 | False | True | | 1 | True | False | ---- ## Example ```python= 7 | 10 # 7的二進位111 # 10的二進位1010 # result 1111 = 15 ``` --- ## 圖片格式簡介(png) - 用PIL讀入後,轉成RGBA模式 - R(紅通道)、G(綠通道)、B(藍通道)、A(阿法通道) - 都是0~255的數值 ---- ## 人類眼睛不好  ---- ## 差不多啦 - Least Significant Bit Encoding - 把資料藏在倒數第一個bit(可以選擇要用幾個通道) ---- ## 藏圖結果  ---- ## 獲得Bit Array (PNG) ```python= im = Image.open("test.png").convert('RGBA') byte_data = np.array(list(im.getdata())) ``` ---- ## 塞入圖片 ```python= bit_array = "".join(['{0:08b}'.format(ord(c)) for c in data]) for i, x in enumerate(bit_array): byte_data[i][0] = int(byte_data[i][0]) & ((1<<8) - 2) | int(x) QQ = np.array(byte_data).astype(np.uint8).reshape(250,250,4) ``` ---- ## 存回圖片 (PNG) ```python= im = Image.fromarray(QQ,'RGBA') im.save('gg.png') ``` --- # HomeWork - 找到藏在這張圖的秘密 - [origin img](https://www.csie.ntu.edu.tw/~b04611015/origin.png) - [secret img](https://www.csie.ntu.edu.tw/~b04611015/gg.png)  ---- ## Requirements - Report - 隱藏資訊 - How
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up