0

I use https://create.withcode.uk

So I found a AES (Advanced Encryption Standard) implementation in Python, but just the encoding and the decoding part:

def encrypt(string, password):
    pad = lambda s : s + str.encode(chr(16 - len(s) % 16) * (16 - len(s) % 16))

    password = str.encode(password)
    string = str.encode(string)

    salt = os.urandom(8) # Unpredictable enough for cryptographic use
    salted = b''
    dx = b''

    while len(salted) < 48:
        dx = md5_encode(dx + password + salt, True)
        salted += dx

    key = salted[0:32]
    iv = salted[32:48]
    
    cipher = AES.new(key, AES.MODE_CBC, iv)
    encrypted_64 = base64.b64encode(cipher.encrypt(pad(string))).decode('ascii')
    
    json_data = {
        'ct': encrypted_64,
        'iv': iv.hex(),
        's': salt.hex()
    }

    return json.dumps(json_data, separators = (',', ':'))

Which works perfectly btw. Then after I did some "sentence = input('blah blah blah') and some password = input('blah blah'), after that, i did encrypt(sentence, password). But I'm really not that sure I did it correctly because it says : AttributeError: 'str' object has no attribute 'encode' on line 4

I'm new to Python.

2
  • What's your python version? Commented Feb 25, 2021 at 2:23
  • Python 3 I think. I'm not sure because I use create.withcode.uk wich is online and doesnt show which version it is. Commented Feb 25, 2021 at 2:42

1 Answer 1

1

str should be replaced by the string you want to encode, and the encoding you want to apply goes in the brackets.

password = password.encode(encoding='UTF-8')
string = string.encode(encoding='UTF-8')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.