RSA 01: RSA Encryption
This example calculates an encrypted message using the RSA algorithm. For encryption, only the public key is needed. The public key is determined by the modulus and the exponent. The "public key" is (n,e). Where 'n' is the modulus and 'e' is the exponent of the public key.

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
* ------ RSA 01 - Calculate encrypted message with RSA --------
3
*
4
* Explanation: This example calculates an encrypted message
5
* using the RSA algorithm. For encryption, only the public key is needed.
6
* The public key is determined by the modulus and the exponent.
7
* The "public key" is (n,e). Where 'n' is the modulus and 'e' is the
8
* exponent of the public key.
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 3 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 Gascon
28
* Implementation: Alvaro Gonzalez, Yuri Carmona
29
*/
30
31
32
#include "WaspRSA.h"
33
34
// 1. Declaration of Public key variables
35
char modulus[] =
36
"7ebd3e97454cc46ebcf758a5b0b1ddfc" \
37
"4775878048968cf3b2aaa0e34b8b0553" \
38
"15005c21a4e31404ebe82485ee114918" \
39
"8a5b96605c3f4437ef7deeff30a5eaa4" \
40
"af944c4405a1c3ac1f0d54453194f212" \
41
"ea50d6c04aee07b1c8c9a37661ad9126" \
42
"604f754f7270503f7b61fa7b72367cac" \
43
"7c871203caa31d77aa0616571ecf388b" ;
44
45
46
// define exponent for public key 'e'
47
// This key is defined as HEX format:
48
// 0x00010001 = 65537 which is a prime number
49
char public_exponent[] = "00010001";
50
51
// 2. Declaration message to encrypt. In the example,
52
// 'message' stands for the HEX representation of "Libelium"
53
char message[] = "4C6962656C69756D";
54
55
// 3. variable to store the encrypted message
56
char enc_message[300];
57
58
59
void setup()
60
{
61
USB.ON();
62
USB.println(F("Example RSA_01\n"));
63
64
USB.print(F("message:"));
65
USB.println(message);
66
USB.println();
67
68
USB.print(F("public_exponent:"));
69
USB.println(public_exponent);
70
USB.println();
71
72
USB.println(F("public_modulus:"));
73
RSA.printMessage(modulus);
74
USB.println();
75
76
}
77
78
79
void loop()
80
{
81
82
// Calculating encrypted message
83
RSA.encrypt(message
84
, public_exponent
85
, modulus
86
, enc_message
87
, sizeof(enc_message));
88
89
USB.println(F("-------------------------"));
90
USB.println(F("Encrypted message:"));
91
USB.println(F("-------------------------"));
92
RSA.printMessage(enc_message);
93
USB.println(F("-------------------------"));
94
95
USB.println(F("-------------------------"));
96
USB.print(F("Encrypted length:"));
97
USB.println((int)strlen(enc_message));
98
USB.println(F("-------------------------"));
99
USB.println();
100
101
delay(10000);
102
103
}
104
Copied!

Output

1
H#
2
Example RSA_01
3
4
message:4C6962656C69756D
5
6
public_exponent:00010001
7
8
public_modulus:
9
7ebd3e97454cc46ebcf758a5b0b1ddfc
10
4775878048968cf3b2aaa0e34b8b0553
11
15005c21a4e31404ebe82485ee114918
12
8a5b96605c3f4437ef7deeff30a5eaa4
13
af944c4405a1c3ac1f0d54453194f212
14
ea50d6c04aee07b1c8c9a37661ad9126
15
604f754f7270503f7b61fa7b72367cac
16
7c871203caa31d77aa0616571ecf388b
17
18
-------------------------
19
Encrypted message:
20
-------------------------
21
275952BC689E5B62514F5A243B09C5D4
22
4931C070A86E2E8E7795EBE27AC7E63E
23
49332B50926B1F9CCE9991DE95AAE49C
24
C9A8CC359109FDD3EEB1C123F53861F0
25
D42AAFCFBC4EAB639AEF55BB56430317
26
689581CE4570A45E4470F40876B98E9F
27
FFD067A66FE75220DD12DD6CAA1CF89A
28
499762D4106368
Copied!
Last modified 1yr ago