0

我已经从这个站点编译了一些 AES 实现代码,它应该执行 128 位密钥加密。我测试了可以一起工作的加密/解密程序。

但是,如果我使用上述代码加密任何内容,然后尝试通过 linux 内置的 openssl 工具对其进行解密,我就是无法解密它,它甚至会记录我错误的幻数错误。同样,如果我使用 openssl 加密任何内容并尝试使用代码解密将不起作用。我尝试了两个 cbc ecb。

如果他们都在实施 AES,它不应该以同样的方式工作吗?

4

1 回答 1

1

it looks like the C code is using ECB and does no padding. so try encrypting a message (a multiple) of 16 bytes followed by 16 bytes of value 16 (pkcs#7 padding). or use a message (a multiple) of 16 bytes and --nopad in openssl (more likely to work). also, use aes-128-ecb or whatever it's called.

a block cipher works on "chunks" of text - in this case, it's 16 characters long. so if you don't want to worry about padding you need to give an exact number of chunks.

also, ecb mode (doing each chunk in turn with no extra processing) isn't secure for many uses. see the wikipedia article (look at the penguin photos).

[edit:] [edit 2:]

> echo -n "abcdabcdabcdabcd" > msg
> wc msg
 0  1 16 msg
> openssl enc -aes-128-ecb -nopad -in msg -K 0 -S "" -iv ""
[noise]
> openssl enc -aes-128-ecb -nopad -in msg -K 0 -S "" -iv "" | wc
 0 1 16

try the above yourself and see if the other code decrypts it (edit 2 sets the key explicitly, and removes IV and salt - not sure what the latter two are for in this case).

[edit 3:]

as far as i can tell, the problem is related to the way that the password is converted to a key. openssl seems to be doing something extra that i can't get rid of unless i specify a key as hex (-K 0). and if i do that, the other program doesn't work (needs a password).

sorry, i'm out of ideas.

于 2013-07-08T00:48:10.990 回答