key="" for i inrange(0,qubit_num-1): if Alice_bases[i]==Bob_bases[i]: if (Bob_bases[i]=='+'and qubits[i]['real']==0and 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']==1and 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 inrange(len(ciphertext_bin)): if key[i]==ciphertext_bin[i]: plaintext_bin+='0' else: plaintext_bin+='1'
#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 inrange(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 inrange(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 inrange(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 inrange(0, len(plaintext_bin), 8): temp_data = int(plaintext_bin[i:i + 8]) decimal_data = BinaryToDecimal(temp_data) flag = flag + chr(decimal_data)