AES 04: AES 128, CBC, ZEROS
This example shows how to encrypt plain text using: - AES algorithm - 128-bit key size - ZEROS 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_04 Encryption 128 CBC ZEROS --------
3
*
4
* This example shows how to encrypt plain text using:
5
* - AES algorithm
6
* - 128-bit key size
7
* - ZEROS 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: Alvaro Gonzalez, Yuri Carmona
29
*/
30
31
#include "WaspAES.h"
32
33
// Define a 16-Byte (AES-128) private key to encrypt message
34
char password[] = "libeliumlibelium";
35
36
// Define Initial Vector
37
uint8_t IV[16] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F};
38
39
// original message on which the algorithm will be applied
40
char message[] = "This_is_a_message";
41
42
// Variable for encrypted message's length
43
uint16_t encrypted_length;
44
45
// Declaration of variable encrypted message
46
uint8_t encrypted_message[300];
47
48
49
50
void setup()
51
{
52
// init USB port
53
USB.ON();
54
USB.println(F("AES_04 example:"));
55
USB.println(F("- 128-bit Key Size"));
56
USB.println(F("- CBC Cipher Mode"));
57
USB.println(F("- ZEROS padding"));
58
USB.println();
59
60
USB.print(F("\nOriginal message:"));
61
USB.println(message);
62
63
USB.print(F("Original length:"));
64
USB.println((int)strlen(message));
65
USB.println();
66
67
}
68
69
void loop()
70
{
71
////////////////////////////////////////////////////////////////
72
// 1. Encrypt message
73
////////////////////////////////////////////////////////////////
74
USB.println(F("1. Encrypt message"));
75
76
// 1.1. Calculate length in Bytes of the encrypted message
77
encrypted_length = AES.sizeOfBlocks(message);
78
79
// 1.2. Calculate encrypted message with CBC cipher mode and ZEROS padding.
80
AES.encrypt( AES_128
81
, password
82
, message
83
, encrypted_message
84
, CBC
85
, ZEROS
86
, IV);
87
88
89
// 1.3. Printing encrypted message
90
USB.print(F("AES Encrypted message:"));
91
AES.printMessage( encrypted_message, encrypted_length);
92
93
// 1.4. Printing encrypted message's length
94
USB.print(F("AES Encrypted length:"));
95
USB.println( (int)encrypted_length);
96
USB.println();
97
98
99
delay(5000);
100
USB.println(F("***********************"));
101
102
}
Copied!

Output

1
H#
2
AES_04 example:
3
- 128-bit Key Size
4
- CBC Cipher Mode
5
- ZEROS padding
6
7
Original message:This_is_a_message
8
Original length:17
9
10
1. Encrypt message
11
AES Encrypted message: "97E356A0EBECF14C08628989BAFCD1284676F43AA76AF55AAA1763D92C21A4E1"
12
AES Encrypted length:32
13
14
***********************
15
1. Encrypt message
16
AES Encrypted message: "97E356A0EBECF14C08628989BAFCD1284676F43AA76AF55AAA1763D92C21A4E1"
17
AES Encrypted length:32
Copied!
Last modified 6mo ago