2016-10-23 4 views
2

、私はインターネット上で、この方法を発見し、私のニーズと私は「はすべてのためにそれを変更しようとしましたエラーが発生しています。ここでTypeError:バイトをstrに連結できません。 Pycrypto AESはもちろんpycryptodome 3.4.2</p> <p>を使用して、Pythonの3のAES暗号化を使用してテキストを暗号化/復号化しようとすると、暗号化

コードです:

def aes(): 
    #aes 
    print('1.Шифруем') 
    print('2.Дешифруем') 
    c = input('Ваш выбор:') 
    if int(c) == 1: 
     #shifr 
     os.system('clear') 
     print('Шифруем значит') 
     print('Введите текст, который хотите зашифровать') 
     text = input() 
     with open('Aes/plaintext.txt', 'wb') as f: 
      f.write(text.encode('utf-8')) 
     BLOCK_SIZE = 16 
     PADDING = '{' 
     pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING 
     EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s))) 
     secret = os.urandom(BLOCK_SIZE) 
     with open('Aes/secret.bin', 'wb') as f: 
      f.write(secret) 
     cipher = AES.new(secret, AES.MODE_CFB) 
     with open('Aes/plaintext.txt', 'rb') as f: 
      text = f.read() 
     encoded = EncodeAES(cipher, text) 
     with open('Aes/ciphertext.bin', 'wb') as f: 
      f.write(encoded) 
     print (encoded) 
    if int(c) == 2: 
     os.system('clear') 
     print('Дешифруем значит') 
     PADDING = '{' 
     with open('Aes/ciphertext.bin', 'rb') as f: 
      encoded = f.read() 
     with open('Aes/secret.bin', 'rb') as keyfile: 
      secret = keyfile.read() 
     DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING) 
     cipher = AES.new(secret, AES.MODE_CFB) 
     decoded = DecodeAES(cipher, encoded) 
     with open('Aes/plaintext.txt', 'w') as f: 
      f.write(str(decoded)) 
     print(decoded)  

しかし、私はいくつかのテキストを解読しようとしている、私はこのエラーを取得する:

Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
File "/usr/lib/python3/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 699, in runfile 
execfile(filename, namespace) 
File "/usr/lib/python3/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 88, in execfile 
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace) 
File "/home/tukanoid/Desktop/Enc_dec2/Enc_dec.py", line 475, in <module> 
aes() 
File "/home/tukanoid/Desktop/Enc_dec2/Enc_dec.py", line 178, in aes 
encoded = EncodeAES(cipher, text) 
File "/home/tukanoid/Desktop/Enc_dec2/Enc_dec.py", line 166, in <lambda> 
EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s))) 
File "/home/tukanoid/Desktop/Enc_dec2/Enc_dec.py", line 162, in <lambda> 
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING 
TypeError: can't concat bytes to str 

実は、私は、ラムダは何をするのか知らないが、私はそれのためにこのエラーが発生すると思います。ありがとう。

+0

[TypeErrorの可能な重複:strにバイトを連結できず、python3を使用しようとしています](http://stackoverflow.com/questions/25042212/typeerror-cant-concat-bytes-to-str-trying-to -use-python3)、どこでもバイトを使う必要があります。 –

答えて

2

あなたはテキストファイルをrbと読んでいます。これはPython 3でbytesオブジェクトを返します。

この関数では、strオブジェクトが埋め込まれているため、エラーになります。 Python 3は、バイナリデータとテキストデータを明確に分離しています。これは、Python 2がしなかったことです。 open('Aes/plaintext.txt', 'r')によって

PADDING = '{' 
    pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING 

変更open('Aes/plaintext.txt', 'rb')とあなたが両側にASCIIを得るでしょう、それが動作します。

(または、PADDING~bytes('{',encoding="ascii")またはJamesとして記載されているのはb"{")。

+1

バイトに固執するのは、バイトベースの操作であるaes暗号に適合しているようです。 'PADDING = b '{''も動作するはずです。 –

+0

Tnxですが、このエラーが発生しました: EncodeAES = lambda c、s:base64.b64encode(c.encrypt(pad)) expect_byte_string(平文) raise TypeError( "バイト文字列のみをCに渡すことができますcode ") TypeError:バイトコードだけをCコード – Tukanoid

+0

に渡すことができます。代わりの解決方法:PADDINGをバイト( '{'、encoding =" ascii ")に変更し、ファイルを' 'rb" 'としてオープンします。それがあなたのためにどのように機能するか教えてください。 –