[아두이노 arduino] 아두이노 json 암호화: Crypt json content

Hi everyone,
I am doing a software on a ESP8266 which stores json files in the flash memory. I wanted somehow to crypt some json properties inside the file so that if someone access it doesn't ready my data (example: a username / password of mqtt server). I have google a lot for the last few days but without any succes. Can someone point me to what library should i use in order to do this ? thanks!

 

This is sort of hard to do, because you have to store the key to decrypt it somewhere... so if they've dumped the flash, they have the information needed to decrypt it...

Though there is probably some merit to at least a passing attempt to obfuscate it, so it doesn't jump out at someone snooping around in the flash - but if they've got the flash contents, a dedicated adversary will be able to get the sensitive data out of it... I mean, they could just write it to their own board, and see what requests it sends, and get the data that way if they wanted to.

 
 

The idea would be storing the key in the code itself, but leaving some json files on the flash memory encrypted. I know there is always a way, but at least i am protecting my data at some sort of level. Can you point me in any direction ?

 
 
 

There are AES libraries for Arduino / ESP8266 which can do the job - but they introduce a performance and storage impact. You could use some very basic bit inversion instead:

 
 

void xor_crypt(char *buffer, int buf_len, char* key, int key_len)

{  

   int ki = 0;  

   for (int bi = 0; bi < buf_len; bi++)   {    

      buffer[bi] ^= key[ki++];    

      if (ki >= key_len) ki = 0;  

   }

 }

...

 

char* key = "secret";

char* str = "Say hello to my little friend!";

int str_len = strlen(str);  //Remember this value in order to ensure proper decryption!

      //Encrypt

xor_crypt(str, str_len, key, strlen(key));

    Serial.println(str);

 

//This will be "garbage" in the serial monitor

//Reverse

xor_crypt(str, str_len, key, strlen(key));

      //Do NO rely on "strlen" for "str" here!

Serial.println(str); //This will be the original string again

Even though this form of encryption may seem crude, it may be just as hard to break as more advanced forms of encryption - but the performance impact is negligible.

EDIT: Bug in code fixed.

 
 
 

vaz83:
The idea would be storing the key in the code itself, but leaving some json files on the flash memory encrypted. I know there is always a way, but at least i am protecting my data at some sort of level. Can you point me in any direction ?

Then on the other hand this is just obfuscation as DrAzzy said. You could store the key on an external EEPROM chip or an sd card which you insert before operation and remove after operation (much like online banking dongles work).

 
 
 

Hi Danois, thanks! This is the direction i wanna take.
I was trying the code you sent, which works fine, but what i want to crypt is something i will read and send to the function. I have made this:

 

char* key = "secret";

void xor_crypt(char *buffer, int buf_len, char* key, int key_len)

{  

   int ki = 0;  

   for (int bi = 0; bi < buf_len; bi++)   {    

      buffer[bi] ^= key[ki++];    

      if (ki >= key_len) ki = 0;  

   }

}

 

void crypt(char * input) {  

      xor_crypt(input, strlen(input), key, strlen(key));  

      Serial.println(input); //Crypted  

      xor_crypt(input, strlen(input), key, strlen(key));  

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

      Serial.println(input); //Descripted  

     delay(5000);

}

What happens now is that when i pass the text i want to crypt, when i then decrypt it, it doesn't show all the text :frowning:

Danois90:
There are AES libraries for Arduino / ESP8266 which can do the job - but they introduce a performance and storage impact. You could use some very basic bit inversion instead:

void xor_crypt(char *buffer, int buf_len, char* key, int key_len)

{
  int ki = 0;
  for (int bi = 0; bi < buf_len; bi++)
  {
    buffer[bi] ^= key[ki++];
    if (ki >= key_len) ki = 0;
  }
}

...

char* key = "secret";
char* str = "Say hello to my little friend!";

//Encrypt
xor_crypt(str, strlen(str), key, strlen(key));
Serial.println(str); //This will be "garbage" in the serial monitor

//Reverse
xor_crypt(str, strlen(str), key, strlen(key));
Serial.println(str); //This will be the original string again




Even though this form of encryption may seem crude, it may be just as hard to break as more advanced forms of encryption - but the performance impact is negligible.
 
 
 

What do you mean by "doesn't allow all the text"? The encrypted buffer is binary and null-characters may therefore occur. Since "strlen" searches for the first null-character, some of the string may be lost. The sollution is to store / remember how long the original string was instead of relying on strlen (which I wrongly did, code in #4 updated).

 
 
 

Danois90:
What do you mean by "doesn't allow all the text"? The encrypted buffer is binary and null-characters may therefore occur. Since "strlen" searches for the first null-character, some of the string may be lost. The sollution is to store / remember how long the original string was instead of relying on strlen (which I wrongly did, code in #4 updated).

Got it! it's working now, but facing another problem: I want to save the result into a json file using arduinoJson, save goes ok, but then, when i try to open the file, says invalid input. Maybe i have to convert the crypt result to hex first to save, and then back to how it was to decrypt? Can you help me?

 

[출처] https://forum.arduino.cc/t/crypt-json-content/592375/7

 

 

[아두이노 arduino] 아두이노 json 암호화: Crypt json content

Hi everyone,
I am doing a software on a ESP8266 which stores json files in the flash memory. I wanted somehow to crypt some json properties inside the file so that if someone access it doesn't ready my data (example: a username / password of mqtt server). I have google a lot for the last few days but without any succes. Can someone point me to what library should i use in order to do this ? thanks!

 

This is sort of hard to do, because you have to store the key to decrypt it somewhere... so if they've dumped the flash, they have the information needed to decrypt it...

Though there is probably some merit to at least a passing attempt to obfuscate it, so it doesn't jump out at someone snooping around in the flash - but if they've got the flash contents, a dedicated adversary will be able to get the sensitive data out of it... I mean, they could just write it to their own board, and see what requests it sends, and get the data that way if they wanted to.

 
 
 

The idea would be storing the key in the code itself, but leaving some json files on the flash memory encrypted. I know there is always a way, but at least i am protecting my data at some sort of level. Can you point me in any direction ?

 
 
 

There are AES libraries for Arduino / ESP8266 which can do the job - but they introduce a performance and storage impact. You could use some very basic bit inversion instead:

 
void xor_crypt(char *buffer, int buf_len, char* key, int key_len) {   int ki = 0;   for (int bi = 0; bi < buf_len; bi++)   {     buffer[bi] ^= key[ki++];     if (ki >= key_len) ki = 0;   } } ... char* key = "secret"char* str = "Say hello to my little friend!"int str_len = strlen(str); //Remember this value in order to ensure proper decryption! //Encrypt xor_crypt(str, str_len, key, strlen(key)); Serial.println(str); //This will be "garbage" in the serial monitor //Reverse xor_crypt(str, str_len, key, strlen(key)); //Do NO rely on "strlen" for "str" here! Serial.println(str); //This will be the original string again

Even though this form of encryption may seem crude, it may be just as hard to break as more advanced forms of encryption - but the performance impact is negligible.

EDIT: Bug in code fixed.

 
 
 

vaz83:
The idea would be storing the key in the code itself, but leaving some json files on the flash memory encrypted. I know there is always a way, but at least i am protecting my data at some sort of level. Can you point me in any direction ?

Then on the other hand this is just obfuscation as DrAzzy said. You could store the key on an external EEPROM chip or an sd card which you insert before operation and remove after operation (much like online banking dongles work).

 
 
 

Hi Danois, thanks! This is the direction i wanna take.
I was trying the code you sent, which works fine, but what i want to crypt is something i will read and send to the function. I have made this:

 
char* key = "secret"void xor_crypt(char *buffer, int buf_len, char* key, int key_len) {   int ki = 0;   for (int bi = 0; bi < buf_len; bi++)   {     buffer[bi] ^= key[ki++];     if (ki >= key_len) ki = 0;   } } void crypt(char * input) {   xor_crypt(input, strlen(input), key, strlen(key));   Serial.println(input); //Crypted   xor_crypt(input, strlen(input), key, strlen(key));   Serial.println(input); //Descripted   delay(5000); }

What happens now is that when i pass the text i want to crypt, when i then decrypt it, it doesn't show all the text :frowning:

Danois90:
There are AES libraries for Arduino / ESP8266 which can do the job - but they introduce a performance and storage impact. You could use some very basic bit inversion instead:

void xor_crypt(char *buffer, int buf_len, char* key, int key_len)

{
  int ki = 0;
  for (int bi = 0; bi < buf_len; bi++)
  {
    buffer[bi] ^= key[ki++];
    if (ki >= key_len) ki = 0;
  }
}

...

char* key = "secret";
char* str = "Say hello to my little friend!";

//Encrypt
xor_crypt(str, strlen(str), key, strlen(key));
Serial.println(str); //This will be "garbage" in the serial monitor

//Reverse
xor_crypt(str, strlen(str), key, strlen(key));
Serial.println(str); //This will be the original string again




Even though this form of encryption may seem crude, it may be just as hard to break as more advanced forms of encryption - but the performance impact is negligible.
 
 
 

What do you mean by "doesn't allow all the text"? The encrypted buffer is binary and null-characters may therefore occur. Since "strlen" searches for the first null-character, some of the string may be lost. The sollution is to store / remember how long the original string was instead of relying on strlen (which I wrongly did, code in #4 updated).

 
 
 

Danois90:
What do you mean by "doesn't allow all the text"? The encrypted buffer is binary and null-characters may therefore occur. Since "strlen" searches for the first null-character, some of the string may be lost. The sollution is to store / remember how long the original string was instead of relying on strlen (which I wrongly did, code in #4 updated).

Got it! it's working now, but facing another problem: I want to save the result into a json file using arduinoJson, save goes ok, but then, when i try to open the file, says invalid input. Maybe i have to convert the crypt result to hex first to save, and then back to how it was to decrypt? Can you help me?

 

[출처] https://forum.arduino.cc/t/crypt-json-content/592375/7

 

 

 

 

 

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
23 [아두이노 arduino] Run a Virtual Factory with Arduino! : 아두이노로 가상 공장을 운영해보세요! file 졸리운_곰 2024.12.28 116
22 [아두이노 arduino] Arduino – base64 encode and decode file 졸리운_곰 2024.03.11 207
21 [아두이노 arduino] Simple encryption using XOR operator 졸리운_곰 2024.03.11 214
» [아두이노 arduino] 아두이노 json 암호화: Crypt json content file 졸리운_곰 2024.03.11 218
19 C#으로 아두이노의 LED 제어하기 - C# 프로그래밍 file 졸리운_곰 2024.02.25 269
18 Top 20 Arduino-based Health Projects file 졸리운_곰 2020.06.07 252
17 Get started with machine learning on Arduino file 졸리운_곰 2019.11.12 270
16 아두이노 운영체제 : 4 Operating Systems for the Arduino file 졸리운_곰 2019.11.04 315
15 아두이노 프로토쉴드(ProtoShield) 조립 가이드 file 졸리운_곰 2019.10.31 307
14 아두이노 우노 를 이용한 부트로더 굽기 ATMEGA328 file 졸리운_곰 2018.01.04 475
13 아두이노 부트로더 굽기(Arduino boorloader burning) file 졸리운_곰 2018.01.04 526
12 아두이노를 이용한 미니 웹 서버 만들기 - LED 제어 file 졸리운_곰 2017.08.03 494
11 아두이노를 이용한 미니 웹 서버 만들기 - 예제 분석 file 졸리운_곰 2017.08.03 596
10 아두이노를 이용한 미니 웹 서버 만들기 - 시작하기 file 졸리운_곰 2017.08.03 431
9 CSEduino : 아두이노 호환 : PCB 주문하여 납땜 조립가능 소형 보드 file 졸리운_곰 2017.08.02 441
8 회로 시뮬레이션 및 아두이노 시뮬레이터 Autodesk의 123D Circuits file 졸리운_곰 2017.06.10 794
7 브레드보드 위에 아두이노 만들기- Arduino Breadboard DIY file 졸리운_곰 2016.11.20 671
6 아두이노 UNO 회로도, Arduino UNO Schemetic file 졸리운_곰 2015.07.04 2222
5 Intel® Edison Boards, rcad* Schematic Design File file 졸리운_곰 2015.07.04 312
4 아두이노 부트로더 굽기 2 file 졸리운_곰 2015.07.04 670
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED