Crypto library: AES256 help.

22 Aug 2014

I would like to encrypt/decrypt a string using AES256 using the Crypto library. I have no idea where to begin because I don't exactly understand how encryption works. I'll search wikipedia to get a better understand of AES encryption and the different encrypting methodologies. In the mean time, can anyone post an example on how to use AES encryption using the Crypto library? The Crypto library can be found here: The library looks useful, but lacks documentation.

22 Aug 2014

I posted in the wrong forum section. Can any moderator please move this thread to the correct forum section?

22 Aug 2014


you can create a question directly on the library page, which would be linked to that repository. This should also notify a owner of the repository.

23 Aug 2014

Hi Martin. I did as you recommended. Its been about 2 days without a response from the owner. I guess I need to be more patient. In the mean time I had a look into the AES.h file to see if I can figure out how to instanciate the class, however I am not sure where to begin.

25 Aug 2014

Posting on the lib author does not work actually, i'll reply you here

#include "mbed.h"
#include "Crypto.h"

Serial pc(USBTX, USBRX);
unsigned char myKEY[0x20]; //256bit key, all 00 for quick&dirt example
//unsigned char myIV[0x10]; // initial vector used in CBC. IV (and blocksize) is fixed to 128bit for AES, regardless on keylen
int main()
    pc.baud (115200);
    unsigned char* buff = (unsigned char*) malloc(0x100);
    memset(buff,0,0x100); // just create an empty buff
    for(char i=0; i<0x40; i++) pc.printf("%.2X",buff[i]);
    //AES myAES(AES_256, myKEY, myIV); // will use CBC mode
    AES myAES(AES_256, myKEY); // will use ECB mode
    myAES.encrypt(buff,buff,0x100); // in and out to the same buff for convenience
    for(char i=0; i<0x40; i++) pc.printf("%.2X",buff[i]);
    pc.printf("\r\nDecrypted again\r\n");
    myAES.decrypt(buff,buff,0x100); //CDB decrypt is broken here, cause IV is not reinit to myIV
    for(char i=0; i<0x40; i++) pc.printf("%.2X",buff[i]);

if you want to use CBC, this lib is not perfect, cause IV is only initialize once at begin (along with key), then gets updated every block you process. In case, you could create a new public void BlockCipher::resetIV(uint8_t *iv) and call it when needed

25 Aug 2014


27 Jan 2015

Team I am not finding RSA encryption on the Crypto.h from mbed.

However it available on

And some sample code (help) with RSA will be great help to0 please

I am guessing, using ECB mode on AES is fairly secure. Right?

Thanks a bunch.

27 Jan 2015 Look at the penguin, it quickly explains.

27 Jan 2015


And the RSA question please?

Appreciate you taking the time to respond

28 Jan 2015

I don't have any example for rsa, sorry.

28 Jan 2015

Geremia I finally went through the ECB. Thanks for the info. I can now (superficially) understand the problem of ECB

I hate to ask you for more help. Since I am a newbie can you kindly help me with the BlockCipher::resetIV(uint8_t *iv) function that you are talking about please?

28 Jan 2015

I took some time to fix it and send pull request to the author

In the meantime here is the fixed lib and an example

Import libraryCrypto

Fork of François Berder Crypto, fixed AES CBC and small rework

Import programAES_example

Example of using AES with ECB/CBC/PCBC modes

the IV will reset automatically every call to encryption/decryption functions. Take care about CBC, if for example you have an encrypted message of 0x100 (encrypted in one pass) and you want to decrypt in multiple steps of small chunks, the IV must be set manually every chunk decrypt(), for cbc decr of chunk_N, IV is last 16bytes of encrypted chunk_N-1.