# 量子密碼學(QuantumCTF初探與解題) > Author:堇姬Naup ## 量子相關知識 * 薛丁格的貓 * 一種思想實驗 * 如果不看月亮,月亮還存在嗎? * 量子(quantum) * 定義 * 物理量最小單位(離散存在) * 事物以頻率機率狀態存在 * 物體小到有量子效應都能當作量子位元,使其當作資訊的仔仔,如光子或代電粒子 * 狀態 * 疊加態 (Superposition) * 糾纏態 (Entanglement) * 密集傳輸 (Dense coding) * 瞬間傳輸 (Teleportation) * 疊加態 * 消相干(? * 越靠近哪一軸,哪一軸的機率越高 (絕對值平方) * 每次測量都會改變量子態 * ![image](https://hackmd.io/_uploads/r1n-tC_Fa.png) * 糾纏態 * 超距效應 * EPR糾纏 * 糾纏態一定是疊加態 * 密集傳輸 * 傳統傳輸只能傳1 bit,量子通過疊加態可以傳多個bits * 瞬間傳輸 * 超光速? * 無法超光速 * 瞬間傳輸是一種複製嗎? * https://www.youtube.com/watch?v=_Fc_XmWiS0c&themeRefresh=1 * !!注意X高的影片不可當知識頻道!! * 未知的量子態無法被複製,因此瞬間傳輸無法超光速 * 做特定運算 -> 狀態改變 -> 修復 -> 還原 * Alice 做特定運算後,狀態改變 * Bob 那邊的狀態未知,需要從 Alice 那邊傳遞訊息過來修復 ## 密碼學基礎概念 * 凱薩密碼 * 每個字母 shift n 個字母 * ABCDED -> DEFGHI (shift 3) * 此加密方式在 WW2 的時候被改良且做到極致 * 電影: * 攔截密碼戰 EniGMA * 獵殺 U-571 * 模仿遊戲 * Enigma = 「謎」 * 二戰德國使用的加密解密機器 * 密碼本如同金鑰 * 定期更新密碼本 * 密碼學基礎 * **明文** *-加密->* **密文** *-傳輸->* **密文** *-解密->* **明文** * 雙方 key 為同一把 * **==金鑰配置問題==** * 公開資訊需要加密才安全,那一開始要怎麼知道金鑰? * 1976 Diffie, Hellman 兩人研究出公開交換金鑰的方法 * 公開色、Alice 秘密色、Bob 秘密色 * 雙方將自己的秘密色與公開色混合後傳輸 * 拿到對方的混合色後再加入自己的秘密色 * 1978 RSA 公開金鑰算法 * 基於兩個質數相乘後難以分解 * 一個 user 會有兩把鑰匙:公鑰、私鑰 * Alice 和 Bob 各有各的私鑰,公鑰任意散播,私鑰不能公開 * 公鑰加密,私鑰解密 * 假設 Alice 要傳給 Bob,需要用 Bob 的公鑰加密 * 因為 Bob 的公鑰任何人都可知道,所以拿 Bob 的公鑰就可以傳訊息給他 * Bob 可以用自己的私鑰來解開用 Bob 公鑰加密的資訊 * 反過來, Bob 要傳訊息給 Alice 的話,可以用 Alice 的公鑰來加密訊息 * 1994 Shor's algorithm 破解 RSA * 量子演算法演示了找循環的能力 * 需要研究出即使是量子電腦也無法破解的非對稱加密方法 * 後量子密碼 (抗量子密碼): * 用既有的傳統算法抵抗量子計算 * 比RSA慢 * USA 目前主要在推後量子密碼 * 量子密碼 * 比較複雜 * 量子演算法 *(必須運行在量子電腦上)* * 成本高 ## 量子密碼學基礎 (BB84) - BB84 就像 RSA 一樣,是一個基礎的積木,到處都會用到 - 金鑰配置協定 * 1984年的量子金鑰配置問題 * 量子如何解決金鑰配置問題 * 無法得知未知量子態 * 量子位元無法被複製 * 基底轉換可以使用量子位元配置金鑰 * 成功達成了什麼事情? * 達成金鑰配置 * 無限長度的金鑰(絕對安全) * 可以檢測出通道有沒有監聽者 * 一次性密碼 (One-Time Pad) * 把明文和金鑰做XOR * 相同的基底連續重複測會得到相同結果 * 使用基底之後會影響結果 * 再用之前的基底測一次結果會不一樣 ``` |0> ├Z基底─ |0> │   ├Z基底─ |0> │   └X基底─ |+>,|-> └X基底─ |+> ├Z基底─ |0>,|1> └X基底─ |+>,|-> ``` * Alice 傳送 qubit、Bob 隨機選擇測量的基底 ``` Alice -----> Bob |0> --Z--> |0> 保留 |-> --Z--> |1> |+> --X--> |+> 保留 |1> --X--> |+> ``` ```mermaid sequenceDiagram Alice ->> Eve: 1. 產生亂數位元 2. 產生亂數基底 ``` Eve in the middle 公布基底的時候狀態不一可以找到eve 有機率抓不到 如何最大化抓到Eve的機率? ## 環境安裝 * Python * Xampp * 放置index.php至C:\xampp\htdocs * Linux: `/opt/lampp/htdocs` * 開apache server * 開cmd執行qkd.py * https://reurl.cc/677gYO * https://ctftime.org/writeup/18212 ## 題目writeup ### AIS3前測 ```python= import random from math import sqrt #將binary string轉成十進制的function def BinaryToDecimal(binary): binary1 = binary decimal, i, n = 0, 0, 0 while(binary != 0): dec = binary % 10 decimal = decimal + dec * pow(2, i) binary = binary//10 i += 1 return (decimal) #將十進制明文轉成UTF8的函數 def dec2utf8(plaintext_bin): res="" for i in range(0, len(plaintext_bin), 8): temp_data = int(plaintext_bin[i:i + 8]) decimal_data = BinaryToDecimal(temp_data) res = res + chr(decimal_data) return res qubit_num=1024 basis_vectors = { '+': [{'real': 0, 'imag': 1}, {'real': 1, 'imag': 0}], 'x': [{'real': 1/sqrt(2), 'imag': 1/sqrt(2)}, {'real': 1/sqrt(2), 'imag': -1/sqrt(2)}] } Alice_bases = ['+', '+', 'x', '+', '+', 'x', '+', 'x', 'x', '+', 'x', '+', '+', 'x', '+', '+', 'x', '+', 'x', '+', '+', '+', 'x', 'x', '+', 'x', 'x', 'x', 'x', 'x', '+', '+', '+', '+', '+', 'x', 'x', 'x', '+', '+', 'x', '+', '+', 'x', 'x', 'x', '+', 'x', '+', 'x', '+', 'x', '+', 'x', 'x', '+', '+', '+', 'x', '+', '+', 'x', 'x', '+', 'x', '+', '+', '+', 'x', 'x', 'x', '+', 'x', '+', 'x', '+', '+', 'x', '+', '+', '+', 'x', 'x', 'x', 'x', '+', 'x', 'x', '+', '+', 'x', '+', '+', 'x', 'x', 'x', 'x', '+', 'x', '+', 'x', '+', 'x', '+', 'x', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', 'x', 'x', '+', 'x', 'x', '+', '+', 'x', 'x', 'x', '+', '+', 'x', 'x', '+', 'x', 'x', '+', 'x', '+', '+', 'x', '+', 'x', '+', 'x', 'x', '+', 'x', 'x', 'x', 'x', 'x', 'x', 'x', '+', 'x', '+', 'x', '+', 'x', 'x', 'x', 'x', 'x', '+', 'x', 'x', '+', '+', '+', '+', 'x', 'x', '+', '+', 'x', '+', '+', 'x', 'x', '+', '+', 'x', 'x', 'x', 'x', '+', 'x', '+', '+', '+', 'x', 'x', '+', 'x', '+', 'x', 'x', 'x', 'x', 'x', 'x', '+', 'x', '+', '+', '+', '+', 'x', 'x', 'x', 'x', 'x', '+', 'x', 'x', '+', 'x', '+', 'x', '+', 'x', 'x', 'x', '+', '+', 'x', '+', '+', 'x', 'x', 'x', 'x', '+', 'x', 'x', '+', 'x', '+', '+', '+', '+', 'x', 'x', '+', 'x', 'x', '+', '+', '+', 'x', 'x', '+', 'x', 'x', 'x', '+', '+', 'x', '+', '+', 'x', '+', '+', '+', '+', '+', '+', 'x', 'x', 'x', '+', '+', '+', '+', 'x', '+', '+', 'x', 'x', 'x', 'x', '+', '+', 'x', '+', '+', 'x', '+', '+', '+', 'x', '+', 'x', 'x', '+', 'x', 'x', 'x', 'x', '+', 'x', '+', 'x', 'x', 'x', '+', '+', 'x', 'x', '+', 'x', 'x', 'x', '+', 'x', '+', '+', '+', '+', 'x', '+', 'x', 'x', 'x', 'x', '+', '+', 'x', 'x', 'x', 'x', '+', 'x', 'x', '+', '+', '+', '+', '+', '+', 'x', 'x', '+', '+', '+', '+', '+', '+', '+', 'x', 'x', '+', '+', 'x', '+', 'x', '+', '+', 'x', 'x', 'x', 'x', 'x', '+', 'x', 'x', '+', 'x', '+', '+', '+', '+', '+', 'x', '+', 'x', '+', '+', 'x', '+', 'x', '+', 'x', 'x', '+', 'x', 'x', 'x', '+', 'x', '+', 'x', 'x', 'x', '+', 'x', 'x', '+', 'x', '+', 'x', '+', 'x', '+', '+', '+', 'x', 'x', '+', 'x', 'x', 'x', '+', '+', '+', '+', '+', '+', '+', '+', 'x', '+', 'x', '+', 'x', '+', 'x', '+', 'x', '+', 'x', 'x', 'x', '+', '+', '+', 'x', '+', 'x', 'x', '+', '+', '+', '+', 'x', 'x', '+', 'x', 'x', 'x', 'x', 'x', 'x', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', 'x', 'x', '+', 'x', 'x', 'x', 'x', '+', '+', 'x', '+', '+', '+', '+', 'x', '+', '+', 'x', 'x', 'x', '+', 'x', 'x', 'x', '+', 'x', '+', 'x', '+', '+', '+', 'x', '+', '+', '+', 'x', 'x', '+', '+', '+', '+', '+', 'x', '+', 'x', 'x', 'x', 'x', '+', '+', '+', 'x', '+', 'x', 'x', 'x', 'x', '+', 'x', 'x', '+', '+', '+', 'x', '+', '+', '+', 'x', 'x', '+', '+', 'x', '+', 'x', 'x', 'x', '+', 'x', 'x', '+', '+', 'x', 'x', '+', 'x', 'x', 'x', '+', 'x', '+', '+', '+', 'x', '+', '+', '+', 'x', '+', '+', 'x', '+', '+', 'x', 'x', '+', 'x', '+', '+', '+', 'x', 'x', 'x', 'x', 'x', 'x', '+', 'x', '+', 'x', 'x', '+', '+', '+', 'x', '+', 'x', '+', 'x', 'x', '+', '+', 'x', '+', 'x', 'x', '+', '+', 'x', '+', '+', '+', 'x', '+', 'x', 'x', '+', 'x', 'x', '+', '+', 'x', '+', 'x', '+', '+', 'x', 'x', 'x', '+', '+', 'x', '+', 'x', 'x', '+', 'x', 'x', '+', 'x', 'x', '+', 'x', 'x', '+', '+', '+', '+', 'x', 'x', 'x', '+', 'x', '+', 'x', 'x', '+', '+', '+', 'x', '+', '+', 'x', 'x', '+', '+', '+', 'x', 'x', '+', 'x', '+', 'x', 'x', 'x', 'x', '+', 'x', '+', 'x', '+', '+', '+', '+', 'x', '+', '+', '+', 'x', '+', 'x', 'x', '+', 'x', 'x', '+', '+', 'x', '+', '+', 'x', '+', 'x', 'x', '+', '+', '+', 'x', 'x', 'x', 'x', '+', '+', '+', '+', '+', 'x', 'x', 'x', 'x', '+', 'x', 'x', '+', 'x', '+', '+', 'x', '+', '+', 'x', '+', 'x', '+', 'x', '+', 'x', '+', 'x', '+', '+', '+', 'x', '+', 'x', 'x', '+', '+', '+', 'x', '+', 'x', 'x', '+', 'x', 'x', '+', '+', 'x', 'x', '+', 'x', '+', '+', 'x', '+', 'x', 'x', '+', '+', 'x', 'x', 'x', 'x', 'x', '+', '+', '+', 'x', 'x', '+', 'x', 'x', '+', 'x', 'x', '+', 'x', 'x', '+', '+', '+', 'x', 'x', '+', '+', '+', '+', '+', 'x', '+', '+', '+', 'x', '+', 'x', 'x', '+', '+', '+', '+', 'x', '+', 'x', 'x', 'x', '+', '+', 'x', 'x', 'x', '+', '+', '+', '+', '+', 'x', 'x', 'x', 'x', '+', 'x', 'x', '+', 'x', '+', '+', '+', 'x', '+', 'x', '+', 'x', '+', 'x', 'x', '+', 'x', '+', '+', 'x', 'x', 'x', 'x', '+', '+', 'x', '+', '+', 'x', '+', 'x', '+', 'x', '+', '+', 'x', '+', '+', '+', 'x', 'x', 'x', '+', 'x', '+', 'x', '+', '+', '+', 'x', 'x', '+', '+', '+', 'x', 'x', '+', '+', 'x', 'x', '+', 'x', 'x', '+', 'x', '+', '+', '+', 'x', '+', 'x', '+', 'x', '+', 'x', 'x', '+', '+', '+', 'x', '+', '+', 'x', 'x', 'x', 'x', '+', 'x', 'x', 'x', 'x', '+', 'x', 'x', 'x', '+', '+', '+', 'x', 'x', 'x', 'x', 'x', '+', 'x', '+', 'x', 'x', '+', '+', 'x', 'x', 'x', 'x', '+', '+', '+', 'x', 'x', 'x', 'x', 'x', '+', 'x', '+', '+', '+', '+', '+', '+', '+', 'x', '+', '+', 'x', 'x', 'x', '+', 'x', '+', 'x', '+', '+', '+', '+', '+', 'x', 'x', 'x', 'x', '+', 'x', '+', 'x', '+', 'x', 'x', 'x', '+', 'x', 'x', 'x', '+', '+', '+', '+', 'x', '+', '+', 'x', 'x', 'x', '+', '+', 'x', '+', '+', 'x', '+', 'x', 'x', 'x', '+', '+', 'x', 'x', '+', '+', '+', '+', 'x', '+', '+', '+', '+', 'x', 'x', '+', '+', '+', 'x', '+'] qubits = [random.choice(basis_vectors[basis]) for basis in Alice_bases] Alice_bit_string="1100100100011110111000011001011011010010101100010001110100000011001001110111110110110010100011100000010111101011110010110110001110101110110001100101101001111001000011110000111110101110010000111001110000100010010011000111011100000001111101010000110001010111011110111110100110010100001100010101001011011000001011010110001101000000110110000111001100101111100111000001110010101000000010001100010110111111000100111011000100000001101010000111000011000110010110100110110100000000101011111010010101110011000111010110111001101010100011100000010100110010101000100111111110001010001111010000001000000101011001100000010011001011011111101001000000100111011001000101100000100001111000100010010011100000101100101111000110101100101011011110110111001101101101110011010010011010001111001010001100111101110100101100001111011010010001011010111000100011110011000110010100001000100100001000011011101000011111111011011000001111101110011000011010110111011011111000001011000010111000101100110000000011011111010100111100111100101110110001000001011111" Bob_bases=['+', 'x', 'x', '+', 'x', '+', 'x', 'x', '+', '+', 'x', 'x', 'x', 'x', 'x', '+', 'x', '+', '+', 'x', '+', 'x', 'x', '+', '+', '+', 'x', 'x', '+', 'x', 'x', 'x', 'x', 'x', '+', 'x', '+', '+', 'x', 'x', 'x', 'x', 'x', 'x', '+', '+', '+', '+', '+', 'x', '+', '+', '+', 'x', '+', 'x', '+', 'x', '+', '+', '+', '+', 'x', 'x', '+', '+', '+', 'x', 'x', 'x', 'x', '+', '+', 'x', '+', '+', 'x', '+', '+', 'x', 'x', '+', '+', 'x', 'x', '+', '+', '+', '+', '+', '+', '+', 'x', 'x', '+', '+', '+', 'x', 'x', '+', 'x', '+', '+', '+', 'x', '+', 'x', '+', 'x', '+', 'x', '+', '+', '+', 'x', 'x', '+', 'x', '+', '+', '+', 'x', '+', '+', '+', 'x', 'x', 'x', 'x', '+', 'x', 'x', '+', 'x', 'x', 'x', '+', 'x', 'x', '+', 'x', 'x', '+', 'x', '+', 'x', '+', 'x', '+', '+', '+', 'x', 'x', '+', '+', '+', 'x', 'x', 'x', 'x', 'x', 'x', 'x', '+', '+', 'x', 'x', '+', 'x', 'x', '+', '+', 'x', '+', '+', 'x', 'x', 'x', '+', '+', 'x', 'x', 'x', '+', '+', '+', 'x', 'x', 'x', 'x', 'x', '+', '+', '+', '+', 'x', 'x', '+', '+', '+', 'x', 'x', 'x', '+', '+', '+', 'x', '+', '+', '+', '+', 'x', '+', 'x', 'x', 'x', 'x', '+', '+', '+', '+', 'x', '+', 'x', 'x', 'x', '+', 'x', '+', 'x', '+', '+', 'x', 'x', 'x', '+', '+', '+', '+', '+', '+', '+', 'x', '+', '+', 'x', 'x', '+', '+', 'x', 'x', 'x', 'x', '+', 'x', 'x', '+', 'x', 'x', 'x', 'x', '+', '+', 'x', 'x', '+', 'x', '+', 'x', 'x', '+', '+', 'x', 'x', 'x', 'x', '+', 'x', '+', 'x', 'x', 'x', 'x', '+', 'x', '+', '+', 'x', '+', 'x', '+', 'x', '+', 'x', 'x', '+', 'x', 'x', 'x', 'x', '+', 'x', '+', 'x', 'x', '+', 'x', 'x', '+', 'x', '+', 'x', 'x', '+', 'x', '+', '+', '+', '+', '+', '+', 'x', '+', '+', '+', 'x', 'x', 'x', '+', '+', '+', 'x', '+', 'x', '+', '+', '+', '+', 'x', '+', '+', 'x', '+', '+', 'x', '+', '+', 'x', 'x', '+', 'x', '+', '+', 'x', 'x', '+', '+', '+', 'x', 'x', 'x', '+', '+', 'x', 'x', 'x', 'x', '+', '+', 'x', 'x', 'x', '+', '+', 'x', '+', 'x', '+', '+', 'x', 'x', 'x', 'x', 'x', 'x', '+', '+', '+', '+', 'x', '+', '+', 'x', 'x', 'x', 'x', '+', '+', 'x', 'x', '+', 'x', 'x', 'x', 'x', '+', 'x', 'x', '+', '+', 'x', '+', '+', 'x', 'x', '+', 'x', '+', 'x', '+', '+', '+', 'x', 'x', 'x', '+', '+', 'x', 'x', 'x', '+', 'x', '+', 'x', '+', 'x', '+', 'x', 'x', 'x', '+', '+', '+', '+', 'x', 'x', '+', '+', 'x', 'x', '+', '+', '+', 'x', '+', '+', 'x', 'x', '+', 'x', '+', '+', 'x', '+', 'x', '+', 'x', '+', 'x', '+', 'x', '+', '+', 'x', 'x', 'x', '+', '+', 'x', '+', 'x', '+', 'x', 'x', '+', '+', 'x', '+', '+', 'x', '+', '+', '+', 'x', 'x', '+', 'x', '+', '+', '+', '+', 'x', '+', 'x', '+', 'x', 'x', '+', '+', 'x', '+', '+', '+', '+', '+', 'x', 'x', 'x', 'x', 'x', 'x', '+', 'x', '+', 'x', '+', '+', 'x', 'x', 'x', 'x', '+', 'x', 'x', 'x', 'x', '+', '+', 'x', '+', 'x', 'x', 'x', '+', '+', '+', '+', '+', '+', 'x', '+', 'x', 'x', 'x', 'x', '+', 'x', '+', 'x', '+', 'x', 'x', 'x', 'x', '+', 'x', '+', '+', '+', 'x', '+', 'x', '+', 'x', 'x', '+', '+', 'x', '+', 'x', 'x', 'x', 'x', '+', 'x', '+', 'x', 'x', '+', '+', 'x', 'x', 'x', 'x', '+', 'x', '+', '+', '+', '+', '+', 'x', 'x', '+', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', '+', '+', '+', 'x', 'x', '+', '+', 'x', '+', '+', '+', 'x', '+', 'x', 'x', 'x', 'x', '+', 'x', '+', '+', '+', '+', '+', '+', 'x', 'x', '+', '+', '+', '+', '+', '+', 'x', '+', 'x', 'x', 'x', '+', '+', '+', 'x', '+', '+', 'x', '+', '+', '+', '+', 'x', '+', '+', 'x', '+', '+', '+', 'x', '+', '+', '+', 'x', 'x', 'x', '+', '+', '+', 'x', '+', '+', '+', '+', 'x', 'x', 'x', 'x', '+', '+', '+', 'x', '+', '+', '+', '+', 'x', 'x', 'x', '+', '+', 'x', '+', 'x', '+', '+', 'x', 'x', 'x', 'x', 'x', '+', 'x', '+', 'x', 'x', 'x', 'x', 'x', 'x', 'x', '+', 'x', '+', 'x', '+', 'x', '+', 'x', '+', '+', 'x', '+', '+', 'x', '+', '+', '+', '+', 'x', '+', 'x', '+', 'x', '+', '+', '+', 'x', 'x', '+', '+', '+', 'x', 'x', 'x', '+', '+', 'x', '+', 'x', '+', 'x', 'x', '+', 'x', '+', 'x', 'x', '+', 'x', 'x', 'x', '+', '+', 'x', '+', '+', '+', '+', '+', '+', 'x', 'x', 'x', 'x', 'x', 'x', 'x', '+', 'x', 'x', 'x', '+', '+', '+', '+', '+', '+', 'x', 'x', '+', 'x', '+', '+', 'x', 'x', '+', '+', '+', 'x', 'x', 'x', '+', '+', 'x', 'x', 'x', 'x', 'x', '+', '+', '+', '+', 'x', '+', 'x', 'x', '+', '+', 'x', '+', '+', 'x', '+', '+', 'x', '+', 'x', 'x', 'x', '+', '+', '+', '+', '+', 'x', 'x', '+', '+', '+', 'x', 'x', '+', '+', 'x', '+', 'x', '+', 'x', '+', 'x', 'x', 'x', '+', 'x', '+', '+', 'x', 'x', '+', '+', '+', '+', 'x', 'x', '+', '+', '+', 'x', '+', 'x', 'x', 'x', 'x', '+', '+', 'x', 'x', '+', '+', '+', '+', 'x', '+', '+', '+', 'x', '+', '+', '+', '+', 'x', '+', 'x', '+', '+', '+', '+', 'x', 'x', 'x', 'x', '+', 'x', '+', 'x', '+', 'x', 'x', 'x', 'x', '+', 'x', '+', '+', 'x', 'x', '+', 'x', '+', 'x', '+', '+', '+', '+', '+', '+', 'x', '+', '+', '+', '+', '+', '+', '+', 'x', 'x', '+', '+', 'x', '+', '+', 'x', 'x', 'x', 'x', 'x', '+', 'x', 'x', 'x', 'x', 'x', 'x', 'x', '+', '+', 'x', '+', '+', 'x', 'x', '+', 'x', 'x', 'x', '+', 'x', 'x', 'x', '+', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', '+', 'x', '+', '+', '+', 'x', '+', 'x', 'x', '+', '+', '+', 'x', '+', '+', 'x', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', 'x', '+', '+', 'x', 'x', 'x', 'x', 'x'] ciphertext_bin="111010111010010000000101100110100000101000101101100000011111110010101001100111101110010010001100100000101001110000100101111001011010000101001000110110010101000001000111101111001011001101110010000110100011110100110110010110011111000110000000101000000001001101110100001000010001001111001100010100101101100110111000010010110101101101111110011101110111110011010001" ############################請依照以上提示取出flag############################ key="" for i in range(0,qubit_num-1): if Alice_bases[i]==Bob_bases[i]: if (Bob_bases[i]=='+' and qubits[i]['real']==0 and qubits[i]['imag']==1) or (Bob_bases[i]=='x' and qubits[i]['real']==1/sqrt(2) and qubits[i]['imag']==1/sqrt(2)): key+=Alice_bit_string[i] if (Bob_bases[i]=='+' and qubits[i]['real']==1 and qubits[i]['imag']==0) or (Bob_bases[i]=='x' and qubits[i]['real']==1/sqrt(2) and qubits[i]['imag']==-1/sqrt(2)): key+=Alice_bit_string[i] print("key:") print(key) plaintext_bin='' print(len(ciphertext_bin),len(key)) for i in range(len(ciphertext_bin)): if key[i]==ciphertext_bin[i]: plaintext_bin+='0' else: plaintext_bin+='1' print("P:") print(plaintext_bin) flag=dec2utf8(plaintext_bin) print('flag = '+flag+"\n") ``` ![image](https://hackmd.io/_uploads/HkFdgbYKp.png) ### B92 ```python= import random from math import sqrt import requests import json #將binary string轉成十進制的function def BinaryToDecimal(binary): binary1 = binary decimal, i, n = 0, 0, 0 while(binary != 0): dec = binary % 10 decimal = decimal + dec * pow(2, i) binary = binary//10 i += 1 return (decimal) #將十進制明文轉成UTF8的函數 def dec2utf8(plaintext_bin): res="" for i in range(0, len(plaintext_bin), 8): temp_data = int(plaintext_bin[i:i + 8]) decimal_data = BinaryToDecimal(temp_data) res = res + chr(decimal_data) return res #傳送的qubit總量 key只會有將近1/4 qubit_num=2048 basis_vectors = { '0': [{'real': 0, 'imag': 1}], '1': [{'real': 1/sqrt(2), 'imag': -1/sqrt(2)}] } #1.Alice亂數產生位元字串 Alice_bit_string = [random.choice('01') for _ in range(qubit_num)] #2.Alice產生相對應的qubit序列 qubits = [random.choice(basis_vectors[basis]) for basis in Alice_bit_string] #3.將qubit序列和basis string傳給localhost server(Bob) #用post傳送json格式的資料到本機端的網頁,並取得json格式反饋 URL = 'http://localhost/index_b92.php' response = requests.post( URL, json={'qubits': qubits} ) #4.localhost server(Bob)回傳ciphertext和將留下的string #取得本機伺服器的反饋後解析其json資料 data=json.loads(response.text) #print(response.text) print("Reserved string from Bob:") print(data["reserved"]) print("") print("Ciphertext from Bob:") print(data["ciphertext"]+"\n") res ='' for i in range(0, len(data["ciphertext"]), 8): temp_data = int(data["ciphertext"][i:i + 8]) decimal_data = BinaryToDecimal(temp_data) res = res + chr(decimal_data) print("") print("Ciphertext to utf8:") print(res) print("") #5.自己比對留下來的string並還原出金鑰 key="" for i in range(0,qubit_num-1): ##寫出取出key的程式## if data["reserved"][i]=='1': key+=Alice_bit_string[i] print("Your key:") print(key+"\n") #6.解密從localhost server來的ciphertext #開始用KEY XOR來解密 ciphertext_bin=data["ciphertext"] plaintext_bin="" for i in range(len(ciphertext_bin)): if key[i]==ciphertext_bin[i]: #位元相同為0 plaintext_bin+='0' else: #位元相異為1 plaintext_bin+='1' #將解密完的binary string轉成utf8(8bits) flag ='' for i in range(0, len(plaintext_bin), 8): temp_data = int(plaintext_bin[i:i + 8]) decimal_data = BinaryToDecimal(temp_data) flag = flag + chr(decimal_data) print('flag2 = '+flag+"\n") ``` ![image](https://hackmd.io/_uploads/SyKSs-tt6.png)