UDAES
Fork of UDAES by
udaes.cpp@1:470744ec67c0, 2017-06-13 (annotated)
- Committer:
- irayya
- Date:
- Tue Jun 13 03:33:09 2017 +0000
- Revision:
- 1:470744ec67c0
- Parent:
- 0:14f07fd5037f
- Child:
- 2:852dbf21ccf6
proo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
irayya | 0:14f07fd5037f | 1 | #include "mbed.h" |
irayya | 0:14f07fd5037f | 2 | #include "udaes.h" |
irayya | 0:14f07fd5037f | 3 | #include "AES.h" |
irayya | 0:14f07fd5037f | 4 | #include<cstring> |
irayya | 0:14f07fd5037f | 5 | |
irayya | 1:470744ec67c0 | 6 | Serial aespc(USBTX,USBRX); //USB serial pin to print in the console |
irayya | 1:470744ec67c0 | 7 | AES aes; //Object for AES |
irayya | 0:14f07fd5037f | 8 | 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 |
irayya | 0:14f07fd5037f | 9 | 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 |
irayya | 1:470744ec67c0 | 10 | char message1[16]; //Character array to store the decrypted message |
irayya | 1:470744ec67c0 | 11 | char encackmsg[16]; //Character array to store the encrypted message |
irayya | 0:14f07fd5037f | 12 | |
irayya | 1:470744ec67c0 | 13 | |
irayya | 1:470744ec67c0 | 14 | //Function to print the hexadecimal ASCII value of the data taking data and its length as parameters |
irayya | 0:14f07fd5037f | 15 | void printData(const void* data, size_t length) |
irayya | 0:14f07fd5037f | 16 | { |
irayya | 1:470744ec67c0 | 17 | const char* dataBytes = (const char*)data; //Casting the data to the character pointers |
irayya | 1:470744ec67c0 | 18 | for (size_t i = 0; i < length; i++) { |
irayya | 0:14f07fd5037f | 19 | if ((i % 8) == 0) |
irayya | 0:14f07fd5037f | 20 | printf("\n\t"); |
irayya | 1:470744ec67c0 | 21 | printf("0x%02X, ", dataBytes[i]); //Print the hexadecimal ASCII value of the respective character |
irayya | 0:14f07fd5037f | 22 | } |
irayya | 0:14f07fd5037f | 23 | printf("\n"); |
irayya | 0:14f07fd5037f | 24 | } |
irayya | 0:14f07fd5037f | 25 | |
irayya | 0:14f07fd5037f | 26 | |
irayya | 1:470744ec67c0 | 27 | //Function for encryption of data taking char enc[] as a parameter |
irayya | 0:14f07fd5037f | 28 | char* encryptData(char enc[]) |
irayya | 0:14f07fd5037f | 29 | { |
irayya | 1:470744ec67c0 | 30 | printf("\nOriginal message: \"%s\"", enc); //Print the message to be encrypted i.e. enc[] |
irayya | 1:470744ec67c0 | 31 | printData(enc, 16); //Print the hexadecimal ASCII form of the data |
irayya | 0:14f07fd5037f | 32 | aes.setup(key, AES::KEY_128, AES::MODE_CBC, iv); //Setup the configuration of AES in 128 bit and CBC mode |
irayya | 1:470744ec67c0 | 33 | aes.encrypt(enc, encackmsg, 16); //Encrypt the given message and store it in encackmsg |
irayya | 0:14f07fd5037f | 34 | aes.clear(); //clear the AES |
irayya | 1:470744ec67c0 | 35 | printf("\nEncrypted message: \"%s\"\n", encackmsg); //Print the encrypted data stored in encackmsg |
irayya | 1:470744ec67c0 | 36 | printData(encackmsg, sizeof(encackmsg)); //Print the hexadecimal ASCII form of the encackmsg |
irayya | 1:470744ec67c0 | 37 | char* encstr=encackmsg; //Store the value of encackmsg in encstr |
irayya | 1:470744ec67c0 | 38 | return encstr; //Return encstr |
irayya | 0:14f07fd5037f | 39 | } |
irayya | 0:14f07fd5037f | 40 | |
irayya | 0:14f07fd5037f | 41 | |
irayya | 1:470744ec67c0 | 42 | //Function for decryption of data taking char dec[] as a parameter |
irayya | 0:14f07fd5037f | 43 | char* decryptData(char dec[]) |
irayya | 0:14f07fd5037f | 44 | { |
irayya | 1:470744ec67c0 | 45 | //aespc.printf("\nThe received message is : %s",dec); //Print the decrypted data to be encrypted i.e. dec[] |
irayya | 0:14f07fd5037f | 46 | aes.setup(key, AES::KEY_128, AES::MODE_CBC, iv); //Setup the configuration of AES in 128 bit and CBC mode |
irayya | 1:470744ec67c0 | 47 | aes.decrypt(dec, message1, 16); //Decrypt the given encrypted data and store it in message1 |
irayya | 0:14f07fd5037f | 48 | aes.clear(); //clear the AES |
irayya | 1:470744ec67c0 | 49 | printf("\nDecrypted message: \"%s\"", message1); //Print the Decrypted data stored in message1 |
irayya | 1:470744ec67c0 | 50 | printData(message1, 16); //Print the hexadecimal ASCII form of the message1 |
irayya | 1:470744ec67c0 | 51 | char* decstr=message1; //Store the value of message1 in decstr |
irayya | 1:470744ec67c0 | 52 | return decstr; //Return decstr |
irayya | 0:14f07fd5037f | 53 | |
irayya | 0:14f07fd5037f | 54 | } |