boppreh / aes Goto Github PK
View Code? Open in Web Editor NEWThis project forked from bozhu/aes-python
A pure Python implementation of AES, with optional CBC, PCBC, CFB, OFB and CTR cipher modes.
License: MIT License
This project forked from bozhu/aes-python
A pure Python implementation of AES, with optional CBC, PCBC, CFB, OFB and CTR cipher modes.
License: MIT License
Using master (with Python 3.5, 3.7 or 3.8) I get:
$ python tests.py
........FFFF.FFFF..........FFFF......
======================================================================
FAIL: test_different_iv (__main__.TestCfb)
Different IVs should generate different ciphertexts.
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests.py", line 223, in test_different_iv
plaintext1 = self.aes.decrypt_cfb(ciphertext1, self.iv)
File "/Users/ilan/tmp/aes/aes.py", line 379, in decrypt_cfb
return unpad(b''.join(blocks))
File "/Users/ilan/tmp/aes/aes.py", line 166, in unpad
assert all(p == padding_len for p in padding)
AssertionError
======================================================================
FAIL: test_long_message (__main__.TestCfb)
CBC should allow for messages longer than a single block.
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests.py", line 239, in test_long_message
self.assertEqual(self.aes.decrypt_cfb(ciphertext, self.iv), long_message)
File "/Users/ilan/tmp/aes/aes.py", line 375, in decrypt_cfb
plaintext_block = xor_bytes(ciphertext_block, self.decrypt_block(prev_ciphertext))
File "/Users/ilan/tmp/aes/aes.py", line 252, in decrypt_block
assert len(ciphertext) == 16
AssertionError
======================================================================
FAIL: test_single_block (__main__.TestCfb)
Should be able to encrypt and decrypt single block messages.
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests.py", line 196, in test_single_block
self.assertEqual(self.aes.decrypt_cfb(ciphertext, self.iv), self.message)
File "/Users/ilan/tmp/aes/aes.py", line 379, in decrypt_cfb
return unpad(b''.join(blocks))
File "/Users/ilan/tmp/aes/aes.py", line 166, in unpad
assert all(p == padding_len for p in padding)
AssertionError
======================================================================
FAIL: test_whole_block_padding (__main__.TestCfb)
When len(message) == block size, padding will add a block.
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests.py", line 233, in test_whole_block_padding
self.assertEqual(self.aes.decrypt_cfb(ciphertext, self.iv), block_message)
File "/Users/ilan/tmp/aes/aes.py", line 375, in decrypt_cfb
plaintext_block = xor_bytes(ciphertext_block, self.decrypt_block(prev_ciphertext))
File "/Users/ilan/tmp/aes/aes.py", line 252, in decrypt_block
assert len(ciphertext) == 16
AssertionError
======================================================================
FAIL: test_different_iv (__main__.TestCtr)
Different IVs should generate different ciphertexts.
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests.py", line 337, in test_different_iv
plaintext1 = self.aes.decrypt_ctr(ciphertext1, self.iv)
File "/Users/ilan/tmp/aes/aes.py", line 453, in decrypt_ctr
return unpad(b''.join(blocks))
File "/Users/ilan/tmp/aes/aes.py", line 166, in unpad
assert all(p == padding_len for p in padding)
AssertionError
======================================================================
FAIL: test_long_message (__main__.TestCtr)
CBC should allow for messages longer than a single block.
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests.py", line 353, in test_long_message
self.assertEqual(self.aes.decrypt_ctr(ciphertext, self.iv), long_message)
File "/Users/ilan/tmp/aes/aes.py", line 453, in decrypt_ctr
return unpad(b''.join(blocks))
File "/Users/ilan/tmp/aes/aes.py", line 166, in unpad
assert all(p == padding_len for p in padding)
AssertionError
======================================================================
FAIL: test_single_block (__main__.TestCtr)
Should be able to encrypt and decrypt single block messages.
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests.py", line 310, in test_single_block
self.assertEqual(self.aes.decrypt_ctr(ciphertext, self.iv), self.message)
File "/Users/ilan/tmp/aes/aes.py", line 453, in decrypt_ctr
return unpad(b''.join(blocks))
File "/Users/ilan/tmp/aes/aes.py", line 166, in unpad
assert all(p == padding_len for p in padding)
AssertionError
======================================================================
FAIL: test_whole_block_padding (__main__.TestCtr)
When len(message) == block size, padding will add a block.
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests.py", line 347, in test_whole_block_padding
self.assertEqual(self.aes.decrypt_ctr(ciphertext, self.iv), block_message)
File "/Users/ilan/tmp/aes/aes.py", line 453, in decrypt_ctr
return unpad(b''.join(blocks))
File "/Users/ilan/tmp/aes/aes.py", line 166, in unpad
assert all(p == padding_len for p in padding)
AssertionError
======================================================================
FAIL: test_different_iv (__main__.TestOfb)
Different IVs should generate different ciphertexts.
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests.py", line 280, in test_different_iv
plaintext1 = self.aes.decrypt_ofb(ciphertext1, self.iv)
File "/Users/ilan/tmp/aes/aes.py", line 417, in decrypt_ofb
return unpad(b''.join(blocks))
File "/Users/ilan/tmp/aes/aes.py", line 166, in unpad
assert all(p == padding_len for p in padding)
AssertionError
======================================================================
FAIL: test_long_message (__main__.TestOfb)
CBC should allow for messages longer than a single block.
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests.py", line 296, in test_long_message
self.assertEqual(self.aes.decrypt_ofb(ciphertext, self.iv), long_message)
File "/Users/ilan/tmp/aes/aes.py", line 417, in decrypt_ofb
return unpad(b''.join(blocks))
File "/Users/ilan/tmp/aes/aes.py", line 166, in unpad
assert all(p == padding_len for p in padding)
AssertionError
======================================================================
FAIL: test_single_block (__main__.TestOfb)
Should be able to encrypt and decrypt single block messages.
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests.py", line 253, in test_single_block
self.assertEqual(self.aes.decrypt_ofb(ciphertext, self.iv), self.message)
File "/Users/ilan/tmp/aes/aes.py", line 417, in decrypt_ofb
return unpad(b''.join(blocks))
File "/Users/ilan/tmp/aes/aes.py", line 166, in unpad
assert all(p == padding_len for p in padding)
AssertionError
======================================================================
FAIL: test_whole_block_padding (__main__.TestOfb)
When len(message) == block size, padding will add a block.
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests.py", line 290, in test_whole_block_padding
self.assertEqual(self.aes.decrypt_ofb(ciphertext, self.iv), block_message)
File "/Users/ilan/tmp/aes/aes.py", line 417, in decrypt_ofb
return unpad(b''.join(blocks))
File "/Users/ilan/tmp/aes/aes.py", line 166, in unpad
assert all(p == padding_len for p in padding)
AssertionError
----------------------------------------------------------------------
Ran 37 tests in 0.287s
FAILED (failures=12)
Awesome little project! I found a bug in the encrypt
and decrypt
methods though.
This code in the get_key_iv
method:
Lines 472 to 475 in bb81e5a
Should be one of these possible fixes, depending on what you intended:
stretched = pbkdf2_hmac('sha256', password, salt, workload, AES_KEY_SIZE + IV_SIZE + HMAC_KEY_SIZE)
aes_key, stretched = stretched[:AES_KEY_SIZE], stretched[AES_KEY_SIZE:]
hmac_key, stretched = stretched[:HMAC_KEY_SIZE], stretched[HMAC_KEY_SIZE:]
iv = stretched[:IV_SIZE]
rest = pbkdf2_hmac('sha256', password, salt, workload, AES_KEY_SIZE + IV_SIZE + HMAC_KEY_SIZE)
aes_key, rest = rest[:AES_KEY_SIZE], rest[AES_KEY_SIZE:]
hmac_key, rest = rest[:HMAC_KEY_SIZE], rest[HMAC_KEY_SIZE:]
iv = rest[:IV_SIZE]
stretched = pbkdf2_hmac('sha256', password, salt, workload, AES_KEY_SIZE + IV_SIZE + HMAC_KEY_SIZE)
aes_key, rest = stretched[:AES_KEY_SIZE], stretched[AES_KEY_SIZE:]
hmac_key, rest = rest[:HMAC_KEY_SIZE], rest[HMAC_KEY_SIZE:]
iv = rest[:IV_SIZE]
The __all__
variable in the code is written incorrectly (at least for more recent Python). It looks like this in the original code:
__all__ = [encrypt, decrypt, AES]
But it should be using quotes to work properly, as shown below:
__all__ = ["encrypt", "decrypt", "AES"]
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.