UDAES

Fork of UDAES by Irayya Mathad

udaes.cpp

Committer:
irayya
Date:
2017-05-29
Revision:
0:14f07fd5037f
Child:
1:470744ec67c0

File content as of revision 0:14f07fd5037f:

#include "mbed.h"
#include "udaes.h"
#include "AES.h"
#include<cstring>

Serial aespc(USBTX,USBRX);
AES aes;
const char key[16] = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }; //Key used for AES encryption and decryption
const char iv[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };  //Format used for AES-CBC encryption and decryption
char message1[16];
char encackmsg[16]; 

void printData(const void* data, size_t length)
{
    const char* dataBytes = (const char*)data;
    for (size_t i = 0; i < length; i++) {
        if ((i % 8) == 0)
            printf("\n\t");
        printf("0x%02X, ", dataBytes[i]);
    }
    printf("\n");
}



char* encryptData(char enc[])
{
      printf("\nOriginal message: \"%s\"", enc);                  //Print the message to be encrypted
     printData(enc, 16);                                         //Print the hexadecimal form of the data
     aes.setup(key, AES::KEY_128, AES::MODE_CBC, iv);            //Setup the configuration of AES in 128 bit and CBC mode
     aes.encrypt(enc, encackmsg, 16);                            //Encrypt the given message
     aes.clear();                                                //clear the AES
     printf("\nEncrypted message: \"%s\"\n", encackmsg);         //Print the encrypted data
     printData(encackmsg, sizeof(encackmsg));                    //Print the hexadecimal form of the encrypted data
     return encackmsg;
}


char* decryptData(char dec[])
{
     aespc.printf("\nThe received message is : %s",dec);            //Print the decrypted data to be encrypted
     aes.setup(key, AES::KEY_128, AES::MODE_CBC, iv);            //Setup the configuration of AES in 128 bit and CBC mode
     aes.decrypt(dec, message1, 16);                    //Decrypt the given encrypted data
     aes.clear();                                                //clear the AES
     printf("\nDecrypted message: \"%s\"", message1);            //Print the Decrypted data
     printData(message1, 16);                      //Print the hexadecimal form of the Decrypted data
     char* decstr=message1;
     return decstr;

}