我是初學者,抱歉,這是顯而易見的或奇怪的。我也很抱歉,如果這之前已經回答,我只是找不到任何真正有用的東西。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
因爲'formatted2'是一個字符串,'算法'是一個整數。他們不能像那樣進行數學運算。也許你打算使用'encrypted/algorithm'? – zondo
'formatted2 = formatted'似乎毫無意義...只需使用'formatted' –