GithubHelp home page GithubHelp logo

Comments (8)

mervick avatar mervick commented on June 1, 2024

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'))

from aes-everywhere.

AzriDelta avatar AzriDelta commented on June 1, 2024

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 aes-everywhere.

mervick avatar mervick commented on June 1, 2024
from aes256 import aes256

# encryption
encrypted = aes256().encrypt('TEXT', b'PASSWORD')
print(encrypted)

# decryption
print(aes256().decrypt(encrypted, b'PASSWORD'))

from aes-everywhere.

mervick avatar mervick commented on June 1, 2024

Also try to add encoding in your testing.py at the beginning of the file

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from aes-everywhere.

AzriDelta avatar AzriDelta commented on June 1, 2024

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

from aes-everywhere.

mervick avatar mervick commented on June 1, 2024

Okey, I updated aes256.py, try it now

from aes-everywhere.

AzriDelta avatar AzriDelta commented on June 1, 2024

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?

from aes-everywhere.

mervick avatar mervick commented on June 1, 2024

Yes, it's correct, it uses random salt for security reason

from aes-everywhere.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.