2017-04-09 118 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)按钮来提问。 –

相关问题