M
M
MixA19952018-05-15 19:29:20
C++ / C#
MixA1995, 2018-05-15 19:29:20

Slow AES256 encryption, how to speed it up?

Good afternoon. The essence of the question is this: my program will have to store some of the user data in encrypted form. I found an implementation of the AES256 algorithm, wrote a function to encrypt a file. Everything would be fine, but encryption is very slow.

void MyEncryptFile() {
    aes256_context ctx;
    uint8_t BUFFER[16];
    uint8_t PASSWORD[32] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
      0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f };
    FILE *ff1 = fopen("E:\\picture.jpg", "rb");
    FILE *ff2 = fopen("E:\\picture.jpg.encrypt", "wb");
    aes256_init(&ctx, PASSWORD);
      while (!feof(ff1))
      {
        fread(BUFFER, sizeof(uint8_t), 16, ff1);
        aes256_encrypt_ecb(&ctx, BUFFER);
        fwrite(BUFFER, sizeof(uint8_t), 16, ff2);
      }
    fclose(ff1);
    fclose(ff2);
    aes256_done(&ctx);
  }

A 1.23 MB file encrypts for about a minute. I implemented this crookedly, or is the implementation of AES itself like this? Or is this normal and the way it should be? But how then, for example, does TrueCrypt encrypt on the fly?
AES implementation: https://github.com/asu2010/crypt_AES-256/blob/mast...
Help me understand

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
forspamonly2, 2018-05-16
@forspamonly2

use some openssl with hardware accelerated aes.

S
Sumor, 2018-05-15
@Sumor

Well, if the encryption result is decrypted and matches the data encrypted by other programs, then everything is correct.
The problem is that algorithms often implemented head-on do a lot of repetitive work that can be optimized. At a minimum, processors have 8-byte registers, and most of the operations in the algorithm are single-byte.
For optimization, you can and should use a profiler that will show bottlenecks that you can try to optimize.
When compiling, you need to specify the parameters for the greatest acceleration.
For implementation, you can use the special instructions of the AES-NI processor , which are used in truecrypt.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question