0

我一直在研究一个加密程序,它只是将一个字符串加密为十进制值,并将编写另一个解密它的程序。截至目前,它对十进制值什么也不做,但是当我尝试对用于加密的二进制文件进行异或运算时,它会返回错误

    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

4

1 回答 1

1
for i in text2:

这不会给出列表的索引,而是给出字符串的实际字符作为字符串。您可能想使用枚举

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
于 2013-11-02T17:27:10.167 回答