2016-10-23 2 views
2

이 pycryptodome 물론 3.4.2TypeError : 바이트를 str에 연결할 수 없습니다. 암호화

를 사용하여, 파이썬 3 AES 암호화 텍스트를 암호화/해독하려고 Pycrypto AES, 나는 '인터넷에서이 방법을 발견하고 내 요구에 대한 변경을 시도하고 모든 I 점점 오류가 발생합니다. 여기

코드입니다 :

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

텍스트 파일을 파이썬 3에서 bytes 개체를 반환하는 rb으로 읽는 중입니다.

이 함수에서는 str 개체를 채우므로 오류가 발생합니다. 파이썬 3은 파이썬 2가하지 않은 바이너리 데이터와 텍스트 데이터를 명확히 구분합니다. open('Aes/plaintext.txt', 'r')에 의해

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

변경 open('Aes/plaintext.txt', 'rb') 당신이 양쪽에 아스키를 얻을 수 있습니다, 그것은 작동합니다.

(또는 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로 전달 될 수 있습니다. 코드 ") TypeError : 바이트 문자열 만 C 코드 – Tukanoid

+0

에 전달할 수 있습니다. 대체 솔루션 : PADDING을 바이트 ('{', 인코딩 ="ascii ")로 변경하고 파일을" ""rb "'로 열어 두십시오. 그것이 당신을 위해 어떻게 작동하는지 말해줘. –

관련 문제