2017-04-09 115 views
1

我是初學者,抱歉,這是顯而易見的或奇怪的。我也很抱歉,如果這之前已經回答,我只是找不到任何真正有用的東西。binascii不支持的操作數類型爲:'str'和'int'

我想製作一個程序來加密和解密文本文件。

我的想法背後是將字符加密爲數字形式,並將它們作爲加密進行加密,當然也可以將其作爲解密進行劃分。

的加密部分作品完美,但在運行解密代碼時,我不斷收到一個

unsupported operand type(s) for /: 'str' and 'int'.

我試過float和INT,但他們沒有工作。

加密代碼:

import binascii 

#ENCRYPTION ALGORITHM 
algorithm = 2 

#ASCII ----> NUMBERS 
raw = raw_input("Enter text to encrypt:") 
one = binascii.hexlify(raw) 
two = binascii.hexlify(one) 

#UNENCRYPTED HEX 
unencrypted = int(two) 

#ENCRYPT HEX 
encrypted = unencrypted * algorithm 

#PRINTS ENCRYPTED TEXT 
print "%.9f" % encrypted 

解密代碼:

import time 
import binascii 

incorrectpasswords = 0 
password=("mypass") 
originpassword = password 
x = 1 
algorithm = 2 

while x==1: 
    passwordattempt =raw_input("Enter Password:") 
    if passwordattempt == password: 
     print("Correct") 
     x = 2 

    if passwordattempt!= password: 
     print("Incorrect") 
     incorrectpasswords = incorrectpasswords + 1 
    if incorrectpasswords > 2: 
     if x == 1: 
      password = (generatedpassword) 
      print("Too many wrong attempts, please try again in one minute.") 
      time.sleep(60) 
      password=originpassword 


encrypted = float(input("Enter text to unencrypt:")) 
formatted = "%.9f" % encrypted 
formatted2 = formatted 
one = formatted2/algorithm 
print ("%.9f" % one) 
two = binascii.unhexlify(one) 
unencrypted = binascii.unhexlify(two) 
print(unencrypted) 

此外,這在Python 2.7作爲binascii不蟒蛇爲我工作3

+1

因爲'formatted2'是一個字符串,'算法'是一個整數。他們不能像那樣進行數學運算。也許你打算使用'encrypted/algorithm'? – zondo

+0

'formatted2 = formatted'似乎毫無意義...只需使用'formatted' –

回答

0

你不應該嘗試用一個數字來分割格式化的字符串。

algorithm = 2 

encrypted = float(input("Enter text to unencrypt:")) 
one = encrypted/algorithm 
print("%.9f" % one) 
+0

感謝您的快速回答!我有另一個問題,> TypeError:a2b_hex()參數1必須是字符串或緩衝區,而不是浮動。當我嘗試用str來修復它,並將str實現到其他變量時,我得到> TypeError:奇數字符串 我不確定這是什麼意思。 –

+0

由於這是一個不同的錯誤,如果您有其他問題,請通過單擊[提問](// stackoverflow.com/questions/ask)按鈕來提問。 –

相關問題