Irayya Mathad / UDAES

Fork of UDAES by Irayya Mathad

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers udaes.cpp Source File

udaes.cpp

00001 #include "mbed.h"
00002 #include "udaes.h"
00003 #include "AES.h"
00004 #include<cstring>
00005 
00006 Serial aespc(USBTX,USBRX);             //USB serial pin to print in the console
00007 AES aes;                               //Object for AES
00008 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
00009 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
00010 char message1[16];     //Character array to store the decrypted message
00011 char encackmsg[16];    //Character array to store the encrypted message
00012 
00013 
00014 //Function to print the hexadecimal ASCII value of the data taking data and its length as parameters
00015 /*void printData(const void* data, size_t length)
00016 {
00017     const char* dataBytes = (const char*)data;   //Casting the data to the character pointers
00018     for (size_t i = 0; i < length; i++) {  
00019         if ((i % 8) == 0)
00020             printf("\n\t");
00021         printf("0x%02X, ", dataBytes[i]);       //Print the hexadecimal ASCII value of the respective character
00022     }
00023     printf("\n");
00024 }*/
00025 
00026 
00027 //Function for encryption of data taking char enc[] as a parameter
00028 char* encryptData(char enc[])
00029 {
00030       //printf("\nOriginal message: \"%s\"", enc);                 //Print the message to be encrypted i.e. enc[] 
00031   //   printData(enc, 16);                                         //Print the hexadecimal ASCII form of the data
00032      aes.setup(key, AES::KEY_128, AES::MODE_CBC, iv);            //Setup the configuration of AES in 128 bit and CBC mode
00033      aes.encrypt(enc, encackmsg, 16);                            //Encrypt the given message and store it in encackmsg
00034      aes.clear();                                                //clear the AES
00035      printf("\nEncrypted message: \"%s\"\n", encackmsg);         //Print the encrypted data stored in encackmsg
00036   //   printData(encackmsg, sizeof(encackmsg));                    //Print the hexadecimal ASCII form of the encackmsg
00037      char* encstr=encackmsg;                                     //Store the value of encackmsg in encstr
00038      return encstr;                                              //Return encstr
00039 }
00040 
00041 
00042 //Function for decryption of data taking char dec[] as a parameter
00043 char* decryptData(char dec[])
00044 {
00045      //aespc.printf("\nThe received message is : %s",dec);         //Print the decrypted data to be encrypted i.e. dec[]
00046      aes.setup(key, AES::KEY_128, AES::MODE_CBC, iv);            //Setup the configuration of AES in 128 bit and CBC mode
00047      aes.decrypt(dec, message1, 16);                    //Decrypt the given encrypted data and store it in message1
00048      aes.clear();                                                //clear the AES
00049      printf("\nDecrypted message: \"%s\"", message1);            //Print the Decrypted data stored in message1
00050     // printData(message1, 16);                      //Print the hexadecimal ASCII form of the message1
00051      char* decstr=message1;                                      //Store the value of message1 in decstr
00052      return decstr;                                              //Return decstr
00053 
00054 }