Development

General / Frame:

» Frame 07: Encrypted Frames

This example shows how to encrypt a Waspmote Frame using the Encryption libraries. In this example AES-128 is used. But AES-192 and AES-256 are available too. Regarding the Block Cipher mode, only ECB is supported. And regarding the padding mode: only ZEROS padding is supported. The result for encryption function is stored in 'frame.buffer' and 'frame.length'

Required Materials

1 x Waspmote
1 x MiniUSB wire
1 x Battery

Notes

- Regarding the Block Cipher mode, only ECB is supported.
- Regarding the padding mode: only ZEROS padding is supported.
- This example can be executed in Waspmote v12 and Waspmote v15

Code

/*  
 *  ------ FRAME_07 - encrypt Frame -------- 
 *  
 *  Explanation: This example shows how to encrypt a 
 *  Waspmote Frame using the Encryption libraries. In this example
 *  AES-128 is used. But AES-192 and AES-256 are available too. 
 *  Regarding the Block Cipher mode, only ECB is supported. And 
 *  regarding the padding mode: only ZEROS padding is supported
 *  The result for encryption function is stored in 
 *  'frame.buffer' and 'frame.length'
 *
 *  Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details. 
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 *  
 *  Version:           3.0
 *  Design:            David Gascón 
 *  Implementation:    Yuri Carmona
 */

#include <WaspFrame.h>
#include <WaspAES.h>

// define the Waspmote ID 
char moteID[] = "node_01";

// Define private 16-Byte key to encrypt message  
char password[] = "libeliumlibelium"; 


  
void setup()
{
  // Init USB port & Accelerometer
  USB.ON();    
  USB.println(F("FRAME_07 example")); 
  
  // set RTC on
  RTC.ON();
  
  // set the Waspmote ID
  frame.setID(moteID);  
}


void loop()
{
  /////////////////////////////////////////////
  // 1. create a new Frame
  /////////////////////////////////////////////
  USB.println(F("1. Creating an ASCII frame"));
  
  // Create new frame (ASCII)
  frame.createFrame(ASCII);
  USB.println(F("new Waspmote Frame created"));
  
  // set frame fields (Battery sensor - uint8_t)
  frame.addSensor(SENSOR_BAT, (uint8_t) PWR.getBatteryLevel());
  USB.println(F("Battery Level added"));
  
  // set frame fields (Temperature in Celsius sensor - float)
  frame.addSensor(SENSOR_IN_TEMP, (float) RTC.getTemperature());
  USB.println(F("RTC temperature added"));

  // Prints frame
  frame.showFrame();
  USB.println();
  
  delay(2000);
    
    
  ////////////////////////////////////////////////
  // 2. Encrypt Waspmote Frame
  ////////////////////////////////////////////////  
  USB.println(F("2. Encrypting Frame"));   

  /* Calculate encrypted message with ECB cipher mode and ZEROS padding
   The Encryption options are:
   - AES_128
   - AES_192
   - AES_256
   */
  frame.encryptFrame( AES_128, password ); 

  // Show the Encrypted frame via USB port
  frame.showFrame();
  USB.println();  
      
  USB.println(F("*****************************"));
  delay(5000);

}

Output

H#
FRAME_07 example
1. Creating an ASCII frame
new Waspmote Frame created
Battery Level added
RTC temperature added
===============================
Current ASCII Frame:
Length: 47
Frame Type: 128
frame (HEX): 3C3D3E800223333837323634353339236E6F64655F30312330234241543A373823494E5F54454D503A32352E353023
frame (STR): <=>€#387264539#node_01#0#BAT:78#IN_TEMP:25.50#
===============================

2. Encrypting Frame
===============================
Current ENCRYPTED Frame:
Length: 65
Frame Type: 97
frame (HEX): 3C3D3E613C1B3015176E6F64655F30312362E6A8D022275CCDD35ED2E4A04E945025DAAAE1423AA4D89BA92DE9FE808DB1E418251A2B848757F1E2767E227C4907
frame (STR): <=>a<0node_01#bæ¨Ð"'\ÍÓ^Òä N”P%ÚªáB:¤Ø›©-éþ€±ä%+„‡Wñâv~"|I
===============================
...

Quick Publish: