AES 08: AES 128, CBC, PKCS with Frame
This example shows how to encrypt a binary message: - AES algorithm - 128-bit key size - PKCS Padding - CBC Cipher Mode

Required Materials

1 x Waspmote 1 x battery

Notes

- The battery has to be connected. - This example can be executed in Waspmote v12 and Waspmote v15

Code

1
/*
2
* ------Waspmote AES_08 Encryption 128 ECB PKCS --------
3
*
4
* This example shows how to encrypt a binary message:
5
* - AES algorithm
6
* - 128-bit key size
7
* - PKCS Padding
8
* - CBC Cipher Mode
9
*
10
* Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L.
11
* http://www.libelium.com
12
*
13
* This program is free software: you can redistribute it and/or modify
14
* it under the terms of the GNU General Public License as published by
15
* the Free Software Foundation, either version 2 of the License, or
16
* (at your option) any later version.
17
*
18
* This program is distributed in the hope that it will be useful,
19
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
* GNU General Public License for more details.
22
*
23
* You should have received a copy of the GNU General Public License
24
* along with this program. If not, see <http://www.gnu.org/licenses/>.
25
*
26
* Version: 3.0
27
* Design: David Gascón
28
* Implementation: Yuri Carmona
29
*/
30
31
#include "WaspAES.h"
32
#include "WaspFrame.h"
33
34
// Define private key to encrypt message
35
char nodeID[] = "node_001";
36
37
// Define a 16-Byte (AES-128) private key to encrypt message
38
char password[] = "libeliumlibelium";
39
40
// Define Initial Vector
41
uint8_t IV[16] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F};
42
43
// Variable for encrypted message's length
44
uint16_t encrypted_length;
45
46
// 2. Declaration of variable encrypted message with enough memory space
47
uint8_t encrypted_message[300];
48
49
50
51
void setup()
52
{
53
// init USB port
54
USB.ON();
55
USB.println(F("AES_08 example:"));
56
USB.println(F("- 128-bit Key Size"));
57
USB.println(F("- ECB Cipher Mode"));
58
USB.println(F("- PKCS5 padding"));
59
USB.println();
60
61
// set the node ID
62
frame.setID(nodeID);
63
}
64
65
66
void loop()
67
{
68
////////////////////////////////////////////////
69
// 1. Create a new Frame
70
////////////////////////////////////////////////
71
USB.println(F("1. Creating an ASCII frame"));
72
RTC.ON();
73
74
// Create new frame (ASCII)
75
frame.createFrame(ASCII);
76
77
// set frame fields (String - char*)
78
frame.addSensor(SENSOR_STR, (char*) "this_is_a_message");
79
// set frame fields (Battery sensor - uint8_t)
80
frame.addSensor(SENSOR_BAT, (uint8_t) PWR.getBatteryLevel());
81
// set frame fields (Temperature in Celsius sensor - float)
82
frame.addSensor(SENSOR_IN_TEMP, (float) RTC.getTemperature());
83
84
// Prints frame
85
frame.showFrame();
86
USB.println();
87
88
89
////////////////////////////////////////////////
90
// 2. Encrypt Frame
91
////////////////////////////////////////////////
92
USB.println(F("2. Encrypting Frame"));
93
94
95
// 2.1. Calculate length in Bytes of the encrypted message
96
encrypted_length = AES.sizeOfBlocks(frame.length);
97
98
// 2.2. Calculate encrypted message with ECB cipher mode and PKCS5 padding.
99
AES.encrypt( 128
100
, password
101
, frame.buffer
102
, frame.length
103
, encrypted_message
104
, CBC
105
, PKCS5
106
, IV);
107
108
// 2.3. Printing encrypted message
109
USB.print(F("AES Encrypted message:"));
110
AES.printMessage( encrypted_message, encrypted_length);
111
112
// 2.4. Printing encrypted message's length
113
USB.print(F("AES Encrypted length:"));
114
USB.println( (int)encrypted_length);
115
USB.println();
116
117
118
delay(10000);
119
USB.println(F("***********************"));
120
}
Copied!

Output

1
H#
2
AES_08 example:
3
- 128-bit Key Size
4
- ECB Cipher Mode
5
- PKCS5 padding
6
7
1. Creating an ASCII frame
8
===============================
9
Current ASCII Frame:
10
Length: 70
11
Frame Type: 128
12
frame (HEX): 3C3D3E800323333837323634353339236E6F64655F3030312330235354523A746869735F69735F615F6D657373616765234241543A363423494E5F54454D503A32302E373523
13
frame (STR): <=>€#387264539#node_001#0#STR:this_is_a_message#BAT:64#IN_TEMP:20.75#
14
===============================
15
16
2. Encrypting Frame
17
AES Encrypted message: "5EE419EF3F35475204A7B99FC883B1C755F22DE3F5CEDF0FA4A55F15012E89940B4EC4510E34E63B5F5C2F458C629C9DAC1EF9AEEB1ECC69FF49BEB89B0C021F4190C51A48956ECD961409E2BA2275CA"
18
AES Encrypted length:80
19
20
***********************
21
1. Creating an ASCII frame
22
===============================
23
Current ASCII Frame:
24
Length: 70
25
Frame Type: 128
26
frame (HEX): 3C3D3E800323333837323634353339236E6F64655F3030312331235354523A746869735F69735F615F6D657373616765234241543A363523494E5F54454D503A32302E373523
27
frame (STR): <=>€#387264539#node_001#1#STR:this_is_a_message#BAT:65#IN_TEMP:20.75#
28
===============================
29
30
2. Encrypting Frame
31
AES Encrypted message: "5EE419EF3F35475204A7B99FC883B1C77B83C5B263862424E29CDBF75262BA1CF653E7277AC1D00189DDF025B7B00953DCE640F283B7E87C0ED9E18AB56772CCF25610CF90B6C9B05D60E736E321FBA0"
32
AES Encrypted length:80‌
Copied!
Last modified 1yr ago