PointyCastle / pointycastle

Moved into the Bouncy Castle project: https://github.com/bcgit/pc-dart
MIT License
270 stars 76 forks source link

AES ECB encryption on a binary array #172

Closed ghost closed 5 years ago

ghost commented 5 years ago

I'm testing currently trying to figure out how to encrypt a binary array using AES ECB encryption. Both the AES key and the data I want to encrypt are presented as binary arrays, if I've understood everything correct I won't need to pad the array containing the data if it matches the key's length?

Here is the code I've got so far:

class Encr {
   static List<int> encrCmd(List<int> inputData, List<int> aesKey) {

    Uint8List keyList = Uint8List.fromList(aesKey);
    Uint8List dataList = Uint8List.fromList(inputData);

    CipherParameters cip = new PaddedBlockCipherParameters(new KeyParameter(keylist), null);
    BlockCipher cipherImpl = new BlockCipher("AES/ECB");
    cipherImpl.init(true, cip);
    Uint8List encrypted = cipherImpl.process(dataList);
    print("encrypted data: " + encrypted.toString());

This results in the following error message: I/flutter (55555): The following assertion was thrown while handling a gesture: I/flutter (55555): type 'PaddedBlockCipherParameters<KeyParameter, Null>' is not a subtype of type 'KeyParameter' of 'params'

Any ideas about what I'm missing or what I'm doing wrong? I've looked at one similar issue that seems to be solved, but it didn't resolve my problem. https://github.com/PointyCastle/pointycastle/issues/167

ghost commented 5 years ago

Thanks to a helpful person on Stack Overflow I was able to get this up and running. This is the way to do it if you have no need for padding:

BlockCipher cipher = ECBBlockCipher(AESFastEngine());
cipher.init(true,KeyParameter(key));
Uint8List encrypted = cipher.process(inputData);