Closed AzriDelta closed 5 years ago
Sorry, my fault, copy aes256.py
in the same folder with your testing.py
and use this code
from aes256 import aes256
# encryption
encrypted = aes256().encrypt('TEXT', 'PASSWORD')
print(encrypted)
# decryption
print(aes256().decrypt(encrypted, 'PASSWORD'))
The same error still happening, even after using the above code
Traceback (most recent call last):
File "<mydirectory>/Python Testing/testing.py", line 4, in <module>
encrypted = aes256().encrypt('TEXT', 'PASSWORD')
File "<mydirectory>\Python Testing\aes256.py", line 39, in encrypt
return base64.b64encode(b'Salted__' + salt + cipher.encrypt(self.__pkcs5_padding(raw)))
File "C:\Users\<myname>\AppData\Local\Programs\Python\Python36\lib\site-packages\Crypto\Cipher\_mode_cbc.py", line 160, in encrypt
expect_byte_string(plaintext)
File "C:\Users\<myname>\AppData\Local\Programs\Python\Python36\lib\site-packages\Crypto\Util\_raw_api.py", line 172, in expect_byte_string
raise TypeError("Only byte strings can be passed to C code")
TypeError: Only byte strings can be passed to C code
A little bit reading on _mode_cbc.py, it looks it requires byte string, but it's getting string returned from pkcs5_padding(raw).
UPDATE: I add the following lines of code at __pkcs5_padding() for 'aes256.py' to replace the return statement as below:
s = s + (self.BLOCK_SIZE - len(s) % self.BLOCK_SIZE) * chr(self.BLOCK_SIZE - len(s) % self.BLOCK_SIZE)
b = bytes(s, 'utf-8')
return b
and it works.
By following the sample code, it should return the statement as below
b'U2FsdGVkX18DbtQ7JRk3E+QZnxckBINY9cZVaMZ3Ghk='
b'TEXT'
Attached is the modified aes256.py and testing.py for your testing. aes256 testing.zip
from aes256 import aes256
# encryption
encrypted = aes256().encrypt('TEXT', b'PASSWORD')
print(encrypted)
# decryption
print(aes256().decrypt(encrypted, b'PASSWORD'))
Also try to add encoding in your testing.py
at the beginning of the file
#!/usr/bin/env python
# -*- coding: utf-8 -*-
Using the given sample code, this error pops out
Traceback (most recent call last):
File "d:/Google Drive/UM/UM Sem 7/#IPPI/#Sem 7/Coding and Testing/Python Testing/testing.py", line 7, in <module>
encrypted = aes256().encrypt('TEXT', b'PASSWORD')
File "d:\Google Drive\UM\UM Sem 7\#IPPI\#Sem 7\Coding and Testing\Python Testing\aes256.py", line 37, in encrypt
key, iv = self.__derive_key_and_iv(passphrase, salt)
File "d:\Google Drive\UM\UM Sem 7\#IPPI\#Sem 7\Coding and Testing\Python Testing\aes256.py", line 93, in __derive_key_and_iv
d_i = md5(d_i + password.encode('utf-8') + salt).digest()
AttributeError: 'bytes' object has no attribute 'encode'
So, if I changed
d_i = md5(d_i + password.encode('utf-8') + salt).digest()
to
d_i = md5(d_i + password + salt).digest()
now this error happens:
Traceback (most recent call last):
File "<mydirectory>/Python Testing/testing.py", line 7, in <module>
encrypted = aes256().encrypt('TEXT', b'PASSWORD')
File "<mydirectory>\Python Testing\aes256.py", line 39, in encrypt
return base64.b64encode(b'Salted__' + salt + cipher.encrypt(self.__pkcs5_padding(raw)))
File "C:\Users\<myname>\AppData\Local\Programs\Python\Python36\lib\site-packages\Crypto\Cipher\_mode_cbc.py", line 160, in encrypt
expect_byte_string(plaintext)
File "C:\Users\<myname>\AppData\Local\Programs\Python\Python36\lib\site-packages\Crypto\Util\_raw_api.py", line 172, in expect_byte_string
raise TypeError("Only byte strings can be passed to C code")
TypeError: Only byte strings can be passed to C code
It works.
b'U2FsdGVkX18RTb+muaFK+NXQuNpZa6EFDCOVT6vd4ng='
b'TEXT'
and second attempt produced this:
b'U2FsdGVkX1/eGk5cX+Bh0ZIezVVZXQgui3S+txk2Z1k='
b'TEXT'
These are correct right? New encrypted value for each attempt even though the plaintext is the same?
Yes, it's correct, it uses random salt for security reason
Sorry for raising another issue, it may look trivial, but I have no idea to solve it.
I tried to implement the Python version by downloading aes256.py and creating a testing.py (both files are in the same folder).
For testing.py, I use the same code just like in the README.md
but it raises the error
with the '^' symbol pointing to the letter 'm' of 'from'.
What I did was to modify the code to this:
but now the error that appeared was
I use VSCode and Python 3.6.3