AKushWarrior / steel_crypt

A collection of high-level API's exposing PointyCastle to perform hashing and encrypting in popular/secure algorithms.
https://pub.dev/packages/steel_crypt
Mozilla Public License 2.0
40 stars 10 forks source link

AES 128 ECB No Padding #6

Closed aytunch closed 4 years ago

aytunch commented 4 years ago

Hi @AKushWarrior , Thanks for this great package. I am trying to communicate with my MiBand using Bluetooth and the authorization requires to encrpyt a key with "AES/ECB/NoPadding". However in your wiki, it says:

5 paddings available for block modes:

PKCS7 Padding ('pkcs7') (Default) ISO7816-4 Padding ('iso7816-4') X9.23 Padding ('x9.23') TBC Padding ('tbc') ISO10126-2 Padding ('iso10126-2') Note: All block modes require padding, to ensure that input is the correct block size.

Note: Paddings do not work with stream modes. You can still enter the parameter, but it won't be used.

Does any of those 5 padding options correspond to "No Padding"? If not, Are you planning on supporting that mode? Or it says paddings don't work in stream modes. I am not sure what it exactly is. But if I use stream mode, does that mean it will be like no padding?

AKushWarrior commented 4 years ago

Ok, so you are probably going to have to go a bit more in-depth here.

The AES cipher is a block cipher, meaning that it can only encrypt strings that have lengths which are multiples of 16. The purpose of padding is to ensure that any irregular length string fills up to a length % 16 == 0; to do this, it attaches random characters to the end of the string.

Therefore, if your key/string you want to encrypt is a set length which is a multiple of 16, you actually don't use padding.:

var aesKey = CryptKey().genFortuna(32);

// This is an AES/ECB/noPadding
var crypt = AesCrypt(aesKey, 'ECB', 'pkcs7'); // Padding doesn't mean anything since length % 16 == 0

var keyToEncrypt = "XXXXXXXXXXXXXXXX"; // Length == multiple of 16
return crypt.encrypt(keyToEncrypt); // This actually has no padding