2013-11-02 6 views
0

문자열을 십진수로 암호화하는 암호화 프로그램을 작성하고 해독 할 다른 프로그램을 작성합니다. 지금은 소수점 값으로 아무것도하지 않고있다,하지만 난 암호화를 위해 내가 사용 바이너리를 XOR하려고 할 때이 오류 여기암호화 프로그램이 작동하지 않습니다. TypeError : 문자열 인덱스는 str이 아닌 정수 여야합니다.

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

답변

1
for i in text2: 

이 @ 소스를 볼 수 있습니다.

text2 = "Welcome" 
for i, char in enumerate(text2): 
    print i, char 

,691을 줄 것이다

text2 = "Welcome" 
for i in text2: 
    print i, 

W e l c o m e 

하지만

를 인쇄합니다 당신은 예를 들어 enumerate

for i, char in enumerate(text2): 

을 사용할 수 있습니다

0 W 
1 e 
2 l 
3 c 
4 o 
5 m 
6 e 
+0

잠깐,이 배열을 Char 배열로 취급하고 있습니까? Decimal Array이기를 바랐습니다 ... Python Noob이되어서 미안하지만, 통역사가 그것을보고있는 것입니까? –

+0

@RyanSmith 문자열에 일반 for 루프를 사용하면 char 배열로 반복됩니다. 파이썬에는 char 데이터 타입이 없으므로 각각의 char은 파이썬에서 문자열로 취급됩니다. – thefourtheye

+0

하지만 이것은 문자/문자열 배열입니까? 나는 혼란 스럽다. (나는 Number Manipulation을위한 2 진 또는 10 진 배열이되기를 바랐다.) 무언가 잘못 되었습니까? –

관련 문제