【发布时间】:2020-07-08 14:30:11
【问题描述】:
我正在尝试在 ECB 模式下使用 LPC18S37 的 AES 引擎来加密 16 字节的明文。我获得了一个密文,然后我通过解密过程来验证我得到了相同的原始明文,我确实这样做了。
但是,作为一项安全措施,我还希望使用外部资源进行验证,因此在 ECB 模式下使用 Python API (Crypto.Cipher.AES) 加密了相同的明文,但我没有得到相同的结果来自MCU的密文。问题是当我解密Python版本的密文时,结果是相同的原始明文。
我验证了 lib 的算法实现,并将其与 FIPS-197 标准进行了比较:它是正确实现的。
因此,我认为问题很可能来自我在 MCU 中的代码实现。
我当然会注意验证密钥和明文在两种情况下是否完全相同。
如果有人有建议或知道如何解决问题,我很乐意欢迎任何想法。
以下是单片机程序实现的相关部分:
Board_Init();
Chip_AES_Init();
//Set buffers :
uint8_t RX_plaintext[16]; / /Contains a 16-bytes plaintext
uint8_t TX_plaintext[16]; // WILL contain a 16-bytes plaintext after decryption
uint8_t RX_key[16]; // Contains a 16-bytes key
uint8_t TX_ciphertext[16]; // WILL contain the 16-bytes ciphertext after encryption
...
Chip_AES_LoadKeySW((uint8_t *) RX_key); // I like using verbose casts but this works the same way even without them
Chip_AES_SetMode(CHIP_AES_API_CMD_ENCODE_ECB);
Chip_AES_Operate((uint8_t *)TX_ciphertext, (uint8_t *)RX_plaintext, 1);
Chip_AES_SetMode(CHIP_AES_API_CMD_DECODE_ECB);
Chip_AES_Operate((uint8_t *)TX_plaintext, (uint8_t *)TX_ciphertext, 1);
还有 Python 部分(版本 3.8.2):
from Crypto.Cipher import AES
import numpy as np
...
t_plaintext = np.random.randint(0, 255, size = 16, dtype = np.uint8)
t_key = np.random.randint(0, 255, size = 16, dtype = np.uint8)
...
o_cryptoEngine = AES.new(bytes(t_key), AES.MODE_ECB)
t_AES_by_Python = np.frombuffer(o_cryptoEngine.encrypt(bytes(t_plaintext)),dtype=np.uint8)
t_decryption_by_Python = np.frombuffer(o_cryptoEngine.decrypt(bytes (t_AES_by_Python)),dtype=np.uint8)
由于密钥和明文是随机生成的,因此输出每次都在变化,但在 MCU 和 Python 之间永远不会完全相同。不过,解密又回到了相同的原始明文。
提前谢谢你。
【问题讨论】:
-
你也可以发布等效的python代码吗?请告诉你正在使用的python版本。两者的输出也会有所帮助。还请提供密钥,以便重现。
-
你的数据类型已经是
uint8_t *,不需要在你的c代码中转换任何东西。 -
Python 代码和版本已在 :) 中编辑过
标签: python c encryption aes lpc