Python /:';str';和';int';
我是初学者,对不起,这是明显的还是奇怪的。我也很抱歉,如果以前有人回答过,我就是找不到任何真正有效的方法 我正在尝试制作一个程序来加密和解密文本文件 我的想法是将字符加密成数字形式,然后将它们相乘作为加密,当然,再除以作为解密 加密部分工作得很好,但是当运行解密代码时,我总是得到一个 /:'str'和'int'的操作数类型不受支持 我试过float和int,但它们不起作用 加密代码:Python /:';str';和';int';,python,encryption,typeerror,Python,Encryption,Typeerror,我是初学者,对不起,这是明显的还是奇怪的。我也很抱歉,如果以前有人回答过,我就是找不到任何真正有效的方法 我正在尝试制作一个程序来加密和解密文本文件 我的想法是将字符加密成数字形式,然后将它们相乘作为加密,当然,再除以作为解密 加密部分工作得很好,但是当运行解密代码时,我总是得到一个 /:'str'和'int'的操作数类型不受支持 我试过float和int,但它们不起作用 加密代码: import binascii #ENCRYPTION ALGORITHM algorithm = 2 #A
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)
另外,在Python2.7中,这是因为binascii在Python3中不适用于我。您不应该尝试将格式化字符串除以数字
algorithm = 2
encrypted = float(input("Enter text to unencrypt:"))
one = encrypted / algorithm
print("%.9f" % one)
您不应该尝试将格式化字符串除以数字
algorithm = 2
encrypted = float(input("Enter text to unencrypt:"))
one = encrypted / algorithm
print("%.9f" % one)
因为
formatted2
是一个字符串,algorithm
是一个整数。他们不能做那样的数学运算。也许您打算使用加密/算法
?formatted2=格式化
似乎毫无意义。。。只需使用formatted
,因为formatted2
是一个字符串,algorithm
是一个整数。他们不能做那样的数学运算。也许您打算使用加密/算法
?formatted2=格式化
似乎毫无意义。。。只需使用格式化
谢谢您的快速回答!不过,我还有一个问题,>TypeError:a2b_hex()参数1必须是字符串或缓冲区,而不是浮点。当我尝试用str修复它,并对其他变量实现str时,我得到了>TypeError:Odd length string,我不确定这是什么意思。因为这是一个不同的错误,如果您有其他问题,请单击按钮提问。谢谢您的快速回答!不过,我还有一个问题,>TypeError:a2b_hex()参数1必须是字符串或缓冲区,而不是浮点。当我尝试用str修复它,并对其他变量实现str时,我得到了>TypeError:Odd length string,我不确定这是什么意思。因为这是另一个错误,如果您有其他问题,请单击按钮询问。