Code Monkey home page Code Monkey logo

arduino-esp8266-aes-encryption-with-nodejs's Introduction

arduino-esp8266-aes-encryption-with-nodejs

This repo demostrates how to encypt your data using AES and decrypt it using NodeJS crypto-js library. If you are looking for PHP port see https://zeitwesentech.com/blog/?p=521 (I am not the author)

You can find the library version here https://github.com/kakopappa/arduino-esp8266-aes-lib

Complete article is here: (I am not the author) https://primalcortex.wordpress.com/2016/06/17/esp8266-logging-data-in-a-backend-aes-and-crypto-js/.

I have fixed few bugs related ESP8266 and combined it with AES and Base64

How to use this:

  1. Clone the github repo.
  2. open aes_encryption.ino in Arduino
  3. Flash

conole output

  1. Copy the iv, encrypted data and replace the esp8266_msg, esp8266_iv in the javascript code below.

Here is the NodeJS Code.

//npm install --save-dev crypto-js
var CryptoJS = require("crypto-js");
var esp8266_msg = 'IqszviDrXw5juapvVrQ2Eh/H3TqBsPkSOYY25hOQzJck+ZWIg2QsgBqYQv6lWHcdOclvVLOSOouk3PmGfIXv//cURM8UBJkKF83fPawwuxg=';
var esp8266_iv  = 'Cqkbb7OxPGoXhk70DjGYjw==';

// The AES encryption/decryption key to be used.
var AESKey = '2B7E151628AED2A6ABF7158809CF4F3C';

var plain_iv =  new Buffer( esp8266_iv , 'base64').toString('hex');
var iv = CryptoJS.enc.Hex.parse( plain_iv );
var key= CryptoJS.enc.Hex.parse( AESKey );

console.log("Let's ");

// Decrypt
var bytes  = CryptoJS.AES.decrypt( esp8266_msg, key , { iv: iv} );
var plaintext = bytes.toString(CryptoJS.enc.Base64);
var decoded_b64msg =  new Buffer(plaintext , 'base64').toString('ascii');
var decoded_msg =     new Buffer( decoded_b64msg , 'base64').toString('ascii');

console.log("Decryptedage: ", decoded_msg);

arduino-esp8266-aes-encryption-with-nodejs's People

Contributors

kakopappa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

arduino-esp8266-aes-encryption-with-nodejs's Issues

Too long message

@kakopappa
Hi, I want to send string message about 7000-8000 length. I do not send with this code. How can I convert this code to send long message ?

AES.cpp中的PKCS7填充出错

AES.cpp应该改成:

void AES::calc_size_n_pad(int p_size){
  int s_of_p = p_size;
  if ( s_of_p % N_BLOCK == 0){
      size = s_of_p + 16;
  }else{
    size = s_of_p +  (N_BLOCK-(s_of_p % N_BLOCK));
  }
  pad = size - s_of_p;
}

AES.h应该改成:

  byte arr_pad[16] = { 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10 };/**< holds the hexadecimal padding values */

Suggested #include in AESLib.cpp

Hello,

Thank you for putting together this library.

I've installed it and the code fails to compile with an error:
AESLib.cpp:63:48: error: 'base64_encode' was not declared in this scope

Adding the following include to AESLib.cpp resolved the issue, the example code now compiles and runs as expected:
#include "Base64.h"

Adding that include to my sketch did not work.

extra character in decrypting

Hello i am trying to decode a message but i get unwanted chacters on the end of my message
the message has been created by nodejs , this is good library i rather use it .
this is my code , its very simple :

#include "AES.h"
#include "base64.h"
#include "ArduinoJson.h"
AES aes;

void setup() {
    Serial.begin(115200);
    Serial.println("\nBooting...");  

byte IV[N_BLOCK]  = {0x28 , 0xc1 , 0x58 , 0xc3 , 0x0b , 0x35 , 0x16 , 0xd7 , 0x8a , 0xd0 , 0xfc , 0x61 , 0xc5 , 0x03 , 0x23 , 0x2e};
byte KEY[]        = {0xa6 , 0xf7 , 0xf8 , 0x72 , 0xc1 , 0x8e , 0x0b , 0xe2 , 0x0a , 0x77 , 0xa1 , 0x0e , 0xc5 , 0xe3 , 0x15 , 0x68};
byte DATA[]       = {0x23 , 0x13 , 0x4c , 0xda , 0xa1 , 0xf4 , 0x4d , 0xcf , 0x5d , 0xfb , 0x85 , 0x55 , 0x1a , 0x63 , 0x78 , 0xdc , 0x9b , 0x22 , 0x7a , 0xb3 , 0x11 , 0x57 , 0x25 , 0xd6 , 0xe1 , 0xb0 , 0xf6 , 0x6f , 0x1b , 0xcb , 0xa2 , 0x0d , 0x43 , 0x7f , 0x08 , 0x2f , 0x5f , 0xb4 , 0xf1 , 0xa5 , 0xec , 0xd2 , 0x5a , 0x8f , 0xee , 0x9d , 0x64 , 0x37};

   byte text[100]={0};
   char text2[100]={0};
    aes.do_aes_decrypt(DATA, sizeof(DATA) , text, KEY, 128, IV);

    Serial.println();
    for(int i=0;text[i];i++)
    {
      Serial.print((char)text[i]);
      text2[i]=(char)text[i];
    }
    Serial.println();
    String msge = String(text2);
    msge.trim();
    Serial.println(msge);

    Serial.println("Done...");
}

void loop() {
  // put your main code here, to run repeatedly:
}

this is the out put :
EXM1 : you should see number one end of line 1��
that two squares are more than what i need , i cant trim them , any idea why is this happening ?

Always same IV b64 whatever key I use.

I just tried your gift for an ESP32...

With your example I get:

11:50:38.671 -> Booting...
11:50:38.671 -> Let's encrypt:
11:50:38.671 -> IV b64: AAAAAAAAAAAAAAAAAAAAAA==
11:50:38.671 -> Message: {"data":{"value":300}, "SEQN":700 , "msg":"IT WORKS!!" }
11:50:38.671 -> Message in B64: eyJkYXRhIjp7InZhbHVlIjozMDB9LCAiU0VRTiI6NzAwICwgIm1zZyI6IklUIFdPUktTISEiIH0=
11:50:38.671 -> The lenght is: 76
11:50:38.671 -> Encryption done!
11:50:38.706 -> Cipher size: 80
11:50:38.706 -> Encrypted data in base64: 7bzIAnZUizSXRjIPTPysu9GXfiJohP8Ku2I9EUrviheRX9A+ecvsQaNvFveKtKGnTeAzOeCrK0WldZy8psT8NfLOsIjAXySlTjljf8PGAng=
11:50:38.706 -> Done...

Whatever key I use I always get:

IV b64: AAAAAAAAAAAAAAAAAAAAAA==

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.