2017-04-09 7 views
1

私は初心者ですが、これは明白です。これが前に答えられたのであれば、本当にうまくいくものは見つけられません。binascii /: 'str'と 'int'のサポートされていないオペランドのタイプ

私は、テキストファイルを暗号化して解読するプログラムを作ろうとしています。

私の考えは、文字を数値形式に暗号化し、それらを暗号として乗算し、もちろん解読として分割することです。

暗号化の部分は完全に動作しますが、復号化コードを実行しているとき、私は私がfloatとint型を試してみた

unsupported operand type(s) for /: 'str' and '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) 

はまた、binasciiとしてPython 2.7で、これはあなたがいけないのpythonで私のために3

+1

'formatted2'は文字列であり、' 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)ボタンをクリックしてください。 –

関連する問題