AES 01: AES 128, ECB, PKCS
This example shows how to encrypt plain text using: - AES algorithm - 128-bit key size - PKCS Padding - ECB 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_01 Encryption 128 ECB PKCS --------
3
*
4
* This example shows how to encrypt plain text using:
5
* - AES algorithm
6
* - 128-bit key size
7
* - PKCS Padding
8
* - ECB 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
// original message on which the algorithm will be applied
37
char message[] = "This_is_a_message";
38
39
// Variable for encrypted message's length
40
uint16_t encrypted_length;
41
42
// Declaration of variable encrypted message
43
uint8_t encrypted_message[300];
44
45
46
47
void setup()
48
{
49
// init USB port
50
USB.ON();
51
USB.println(F("AES_01 example:"));
52
USB.println(F("- 128-bit Key Size"));
53
USB.println(F("- ECB Cipher Mode"));
54
USB.println(F("- PKCS5 padding"));
55
USB.println();
56
57
USB.print(F("\nOriginal message:"));
58
USB.println(message);
59
60
USB.print(F("Original length:"));
61
USB.println((int)strlen(message));
62
USB.println();
63
64
}
65
66
void loop()
67
{
68
////////////////////////////////////////////////////////////////
69
// 1. Encrypt message
70
////////////////////////////////////////////////////////////////
71
USB.println(F("1. Encrypt message"));
72
73
// 1.1. Calculate length in Bytes of the encrypted message
74
encrypted_length = AES.sizeOfBlocks(message);
75
76
// 1.2. Calculate encrypted message with ECB cipher mode and PKCS5 padding.
77
AES.encrypt( AES_128
78
, password
79
, message
80
, encrypted_message
81
, ECB
82
, PKCS5);
83
84
// 1.3. Printing encrypted message
85
USB.print(F("AES Encrypted message:"));
86
AES.printMessage( encrypted_message, encrypted_length);
87
88
// 1.4. Printing encrypted message's length
89
USB.print(F("AES Encrypted length:"));
90
USB.println( (int)encrypted_length);
91
USB.println();
92
93
94
delay(5000);
95
USB.println(F("***********************"));
96
97
}
Copied!

Output

1
H#
2
AES_01 example:
3
- 128-bit Key Size
4
- ECB Cipher Mode
5
- PKCS5 padding
6
7
Original message:This_is_a_message
8
Original length:17
9
10
1. Encrypt message
11
AES Encrypted message: "DEAB54C1B9DC182329B2F4D8F8DB3D6AAAF316444A355697E22E48B503DC8C0C"
12
AES Encrypted length:32
13
14
***********************
15
1. Encrypt message
16
AES Encrypted message: "DEAB54C1B9DC182329B2F4D8F8DB3D6AAAF316444A355697E22E48B503DC8C0C"
17
AES Encrypted length:32
18
19
***********************
20
1. Encrypt message
21
AES Encrypted message: "DEAB54C1B9DC182329B2F4D8F8DB3D6AAAF316444A355697E22E48B503DC8C0C"
Copied!
Last modified 1yr ago