我一直致力于加密程序,它只是将字符串加密成十进制值,并编写另一个将解密它的程序。截至目前,它对小数值没有任何作用,但是当我尝试对我用于加密的二进制文件进行异或时,它会返回错误
Traceback (most recent call last):
File "./enc.py", line 53, in <module>
encrypt()
File "./enc.py", line 35, in encrypt
val1 = text2[i] + decryptionKey[j]
TypeError: string indices must be integers, not str
这是代码
#!/usr/bin/env python2
import sys
def encrypt():
text1 = raw_input("Please input your information to encrypt: ")
text2 = []
#Convert text1 into binary (text2)
for char in text1:
text2.append(bin(ord(char))[2:])
text2 = ''.join(text2)
key = raw_input("Please input your key for decryption: ")
decryptionKey = []
#Convert key into binary (decryptionKey)
for char in key:
decryptionKey.append(bin(ord(char))[2:])
decryptionKey = ''.join(decryptionKey)
#Verification String
print "The text is '%s'" %text1
print "The key is '%s'" %key
userInput1 = raw_input("Is this information ok? y/n ")
if userInput1 == 'y':
print "I am encrypting your data, please hold on."
elif userInput1 == 'n':
print "Cancelled your operation."
else:
print "I didn't understand that. Please type y or n (I do not accept yes or no as an answer, and make sure you type it in as lowercase. We should be able to fix this bug soon.)"
finalString = []
if userInput1 == 'y':
for i in text2:
j = 0
k = 0
val1 = text2[i] + decryptionKey[j]
if val1 == 0:
finalString[k] = 0
elif val1 == 1:
finalString[k] = 1
elif val1 == 2:
finalString[k] = 0
j += 1
k += 1
print finalString
encrypt()
如果更容易,您还可以查看来源@ https://github.com/ryan516/XOREncrypt/blob/master/enc.py
答案 0 :(得分:1)
for i in text2:
这不会给列表的索引,而是字符串的实际字符作为字符串。您可能想要使用enumerate
for i, char in enumerate(text2):
例如,
text2 = "Welcome"
for i in text2:
print i,
将打印
W e l c o m e
但
text2 = "Welcome"
for i, char in enumerate(text2):
print i, char
将给出
0 W
1 e
2 l
3 c
4 o
5 m
6 e