RSA 03: RSA Encryption Frame
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
#include "WaspFrame.h"
34
35
// 1. Declaration of Public key variables
36
char public_modulus[] =
37
"7ebd3e97454cc46ebcf758a5b0b1ddfc" \
38
"4775878048968cf3b2aaa0e34b8b0553" \
39
"15005c21a4e31404ebe82485ee114918" \
40
"8a5b96605c3f4437ef7deeff30a5eaa4" \
41
"af944c4405a1c3ac1f0d54453194f212" \
42
"ea50d6c04aee07b1c8c9a37661ad9126" \
43
"604f754f7270503f7b61fa7b72367cac" \
44
"7c871203caa31d77aa0616571ecf388b" ;
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
52
char private_exponent[] =
53
"4ea5689dbe27310df6bd16895ae844f4" \
54
"33f3beade05d6c021db0bc3dcfb6e90a" \
55
"f15153da9cd33cad012700e30b2436d3" \
56
"bfa7addd05e14c97d949b07132e30283" \
57
"663f39a32662d951d7d53ef92ef39d2e" \
58
"a791689065f656f5ffb5f60c92f91b98" \
59
"1f8127a90235a05d9b82c223d43bce92" \
60
"002b097e6634be3141f480d4e5333341" ;
61
62
char p[] =
63
"cada49cc750e4ff40bad216aa2ff3a69" \
64
"c5cbcc6d2a320dd81a098e5a995e30dc" \
65
"f40a0a775130471f3a4ebd364a003f6a" \
66
"65b2a02be98f7394258c51c324f1da03" ;
67
68
char q[] =
69
"9ff1e238eb0217b573239fd0b98fac6a" \
70
"97907f0534e2c59356e637e400e0b8a2" \
71
"497d7f53a614a29991dfb630e5d74b7f" \
72
"95e10c9663a34f67fc65009f51b724d9" ;
73
74
char dp[] =
75
"39924a6fa4a93337e83872cb790746e4" \
76
"ce2651168a6b3a52a2d1237dc3196074" \
77
"d52e245a48c892e6e1fd86e5e98ab874" \
78
"d1f8284d4e3450713356e7bda2b6a151" ;
79
80
char dq[] =
81
"6c08eafffd2525b4873819dbd76b074f" \
82
"dc5e5a9dbeb22a38326b40773e6c8be9" \
83
"fa6fcd50480f0a2166d9cfeb496459f7" \
84
"acda1d317bcdb4760d927f901d96f249" ;
85
86
char qp[] =
87
"513c04961d1c7c762b49f37d02b12538" \
88
"569fc1ad0a158f7ba8c2a1217158b9d9" \
89
"c4b9ca80a989466544d497c233c2dd43" \
90
"51ffe83b760fe8a6de4737ecef77b0ba" ;
91
92
93
// variable to store the encrypted message
94
char enc_message[300];
95
96
97
98
void setup()
99
{
100
USB.ON();
101
USB.println(F("Example RSA_03\n"));
102
103
USB.print(F("public_exponent:"));
104
USB.println(public_exponent);
105
USB.println();
106
107
USB.println(F("public_modulus:"));
108
RSA.printMessage(public_modulus);
109
USB.println();
110
111
}
112
113
114
void loop()
115
{
116
////////////////////////////////////////////////
117
// 1. Create a new Frame
118
////////////////////////////////////////////////
119
USB.println(F("1. Creating an ASCII frame"));
120
121
// Create new frame (ASCII)
122
frame.createFrame(ASCII,"WASP_encrypted");
123
124
// set frame fields (String - char*)
125
frame.addSensor(SENSOR_STR, (char*) "XBee frame");
126
// set frame fields (Battery sensor - uint8_t)
127
frame.addSensor(SENSOR_BAT, (uint8_t) PWR.getBatteryLevel());
128
// set frame fields (Temperature in Celsius sensor - float)
129
frame.addSensor(SENSOR_IN_TEMP, (float) RTC.getTemperature());
130
131
// Prints frame
132
frame.showFrame();
133
USB.println();
134
135
136
////////////////////////////////////////////////
137
// 2. Encrypt Frame
138
////////////////////////////////////////////////
139
USB.println(F("2. Encrypting Frame"));
140
141
// Calculating encrypted message
142
RSA.encrypt(frame.buffer
143
, frame.length
144
, public_exponent
145
, public_modulus
146
, enc_message
147
, sizeof(enc_message));
148
149
USB.println(F("-------------------------"));
150
USB.println(F("Encrypted message:"));
151
USB.println(F("-------------------------"));
152
RSA.printMessage(enc_message);
153
USB.println(F("-------------------------"));
154
155
USB.println(F("-------------------------"));
156
USB.print(F("Encrypted length:"));
157
USB.println((int)strlen(enc_message));
158
USB.println(F("-------------------------"));
159
USB.println();
160
161
162
delay(10000);
163
164
}
Copied!

Output

1
H#
2
Example RSA_03
3
4
public_exponent:00010001
5
6
public_modulus:
7
7ebd3e97454cc46ebcf758a5b0b1ddfc
8
4775878048968cf3b2aaa0e34b8b0553
9
15005c21a4e31404ebe82485ee114918
10
8a5b96605c3f4437ef7deeff30a5eaa4
11
af944c4405a1c3ac1f0d54453194f212
12
ea50d6c04aee07b1c8c9a37661ad9126
13
604f754f7270503f7b61fa7b72367cac
14
7c871203caa31d77aa0616571ecf388b
15
16
1. Creating an ASCII frame
17
===============================
18
Current ASCII Frame:
19
Length: 62
20
Frame Type: 134
21
frame (HEX): 3C3D3E8602233444334337303633443933373432463423574153505F656E63727970746564233023535452A058426565206672616D65234241543A393823
22
frame (STR): <=>�#4D3C7063D93742F4#WASP_encrypted#0#STR:XBee frame#BAT:98#
23
===============================
24
25
2. Encrypting Frame
26
-------------------------
27
Encrypted message:
28
-------------------------
29
322854D2FDDD9673787B1715FA50488A
30
CF81F09436A9C4A72245CB49253FC337
31
F0914E4202880D1036C1953E5C8A9593
32
EDB35784A31DF9932846C3C8592D4300
33
0E077C262CA2EBE58C284B187B870F81
34
FAB9772E78FCB9160EB3C53EAFA774C6
35
D106F8C36176CC396019420EEE1734DC
36
584CFD0A901506DCDC20F538EC64DACD
37
38
-------------------------
39
-------------------------
40
Encrypted length:256
41
-------------------------
Copied!
Last modified 1yr ago