kokke/tiny-AES-c

不支持Android?

Closed this issue · 5 comments

AES/ECB/PKCS5Padding

in Java output 32 bit array:
input: abcdefghabcdefgh
output: 0xff 0x4e 0xb3 0xad 0x54 0xa5 0xe1 0x4a 0xec 0xb2 0x10 0x8b 0x0e 0x0a 0x65 0x80 0x57 0xb7 0xf1 0xc9 0x0e 0x56 0x6c 0xa3 0xf0 0xd4 0xd7 0xc1 0xd8 0x52 0x7e 0x5d

use tiny-AES-c follow image :
input : abcdefghabcdefgh
output:0xff 0x4e 0xb3 0xad 0x54 0xa5 0xe1 0x4a 0xec 0xb2 0x10 0x8b 0x0e 0x0a 0x65 0x80

Lost part of result ...

how resolve it

image

It sound like a duplicate of #137 . Author's reply can be found here: #137 (comment)

This library doesn't include padding implementation, you have to add it manually (different applications use different padding).

kokke commented

Hi @zhangxy123666 and thank you for your interest in this project :)

When using ECB mode, you can only encrypt/decrypt 16 bytes at a time.

It looks like you are trying to pass 32 bytes to AES_ECB_encrypt(), but it will only encrypt 16 bytes at a time. That might also be the reason you are missing half the output?

I don't know what the string2bytes-function does, but if data can hold (at least) 32 bytes, you can do something like this:

AES_init_ctx(&ctx, key);
AES_ECB_encrypt(&ctx, data);
AES_ECB_encrypt(&ctx, &data[16]);
int i;
for (i = 0; i < 32; ++i)
{
  LOGD("out --- 0x%.2x", data[i]);
}

By the way, thanks for participating in the discussion @Nable80 :)

kokke commented

This issue basically asks the same question: #140

Oops, I didn't notice the length >_<

thanks kokke, I resolve it with :
AES/ECB/PKCS5Padding updateTO AES/ECB/NoPadding
If less than 16 bytes are added to 16 bytes, tiny-AES support NoPadding, not ECB/PKCS5Padding

in java
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");//"算法/模式/补码方式"

It works well