2016-04-30 20 views
1

我不確定你們是否熟悉加密模塊,但我試圖加密一個表示字符串的變量。如何用密碼模塊加密變量?

例如:

string = input('String here') 

他們給出的模塊頁面上的例子是:

from cryptography.fernet import Fernet 
key = Fernet.generate_key() 
cipher_suite = Fernet(key) 
cipher_text = cipher_suite.encrypt(b"A really secret message. Not for prying eyes.") 
plain_text = cipher_suite.decrypt(cipher_text) 

這是所有罰款和花花公子,但是當我試圖取代「真的祕密消息字符串如果它是在引號中,它只是打印變量的名字(duh)

如果它出這樣的報價:cipher_text = cipher_suite.encrypt(bstring),它說變量沒有定義(也咄)

但如果我只是把變量,它給了我一個錯誤:TypeError: data must be bytes.

任何想法?謝謝!

回答

3

根據Python的documentation

bytes and bytearray objects are sequences of integers (between 0 and 255), representing the ASCII value of single bytes

我覺得輸入需要像這樣

a = b"abc" 

(請注意, 「B」)。

+0

你將如何格式化?你的意思是這樣的嗎?:string = input('b'+'String_input') –

+0

我不用這些工作,但文檔(比較上面)說: _Bytes對象可以構造構造函數,字節()和文字;使用正常字符串語法的b前綴:b'xyzzy'._ – patrick

+0

我不想像這樣放置一行文本進行加密:b'text_not_a_variable'。我想要做的就是加密一個像這樣的變量:b'This_IS_a_variable'(參見上面嘗試過的語法的問題) –