user2415415
user2415415

Reputation: 1

TypeError: 'str' does not support the buffer interface when using PyCrypto.AES

I am trying to do some experimenting in encrypting and decrypting using PyCrypto.AES when I try to decrypt it gives me TypeError: 'str' does not support the buffer interface

I found some solutions where I have to encode or use string, but I couldn't figure how to use it.

AESModule.py

from Crypto.Cipher import AES
#base64 is used for encoding. dont confuse encoding with encryption#
#encryption is used for disguising data
#encoding is used for putting data in a specific format
import base64
# os is for urandom, which is an accepted producer of randomness that
# is suitable for cryptology.
import os

def encryption(privateInfo,secret,BLOCK_SIZE):
    #32 bytes = 256 bits
    #16 = 128 bits
    # the block size for cipher obj, can be 16 24 or 32. 16 matches 128 bit.
    # the character used for padding
    # used to ensure that your value is always a multiple of BLOCK_SIZE
    PADDING = '{'
    # function to pad the functions. Lambda
    # is used for abstraction of functions.
    # basically, its a function, and you define it, followed by the param
    # followed by a colon,
    # ex = lambda x: x+5
    pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
    # encrypt with AES, encode with base64
    EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
    # generate a randomized secret key with urandom
    #secret = os.urandom(BLOCK_SIZE)
    print('Encryption key:',secret)
    # creates the cipher obj using the key
    cipher = AES.new(secret)
    # encodes you private info!
    encoded = EncodeAES(cipher, privateInfo)
    print('Encrypted string:', encoded)
    return(encoded)

def decryption(encryptedString,secret):
    PADDING = '{'
    DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
    #Key is FROM the printout of 'secret' in encryption
    #below is the encryption.
    encryption = encryptedString
    cipher = AES.new(secret)
    decoded = DecodeAES(cipher, encryption)
    print(decoded)

test.py

import AESModule
import base64
import os

BLOCK_SIZE = 16
key = os.urandom(BLOCK_SIZE)
c = AESRun2.encryption('password',key,BLOCK_SIZE)
AESRun2.decryption(c,key)

Upvotes: 0

Views: 476

Answers (1)

Matti Virkkunen
Matti Virkkunen

Reputation: 65126

Strings (str) are text. Encryption does not deal in text, it deals in bytes (bytes).

In practice insert .encode and .decode calls where necessary to convert between the two. I recommend UTF-8 encoding.

In your case since you are already encoding and decoding the ciphertext as base-64 which is another bytes/text conversion, you only need to encode and decode your plaintext. Encode your string with .encode("utf-8") when passing it into the encryption function, and decode the final result with .decode("utf-8") when getting it out of the decryption function.

If you're reading examples or tutorials make sure they are for Python 3. In Python 2 str was a byte string and it was commonplace to use it for both text and bytes, which was very confusing. In Python 3 they fixed it.

Upvotes: 2

Related Questions