Still some problems with XOR crypt

Hi,
it hasn't been an easy ride trying to do this :frowning:
Well, my function is now crypting and decrypting without problems. The existing problem now is when i save crypted text back to a file and then decrypt it, it's not working again, only some part is being decrypted.

Heres the code:

char* key = "f0df7440-23e6-4cf6-9711-db29c7eb6f8d";


bool XOR_cryptDecryptChar(char * input, int length)
{
  int keysize = strlen(key);
  
  for(int x=0; x<=length; x++)
  {
    for(int i=0; i<=keysize;i++)
    {
      input[x]=(input[x]^key[i])^(x*i);
    }
  }

  return true;
}

void saveFile()
{
	
	 DynamicJsonDocument jDoc(1800);
   
	jDoc["fileVersion"] = String("1.0.0");		
	jDoc["brokerURL"] = server->arg("brokerURL");;
	jDoc["brokerPort"] = server->arg("brokerPort");;
	jDoc["brokerUsername"] = server->arg("brokerUsername");;
	jDoc["brokerPassword"] = server->arg("brokerPassword");;
	jDoc["brokerClientId"] = server->arg("brokerClientId");;
	jDoc["brokerGeneralTopic"] = server->arg("brokerGeneralTopic");;
	jDoc["brokerGlobalTopic"] = server->arg("brokerGlobalTopic");	;
	jDoc["useAuthentication"] = server->arg("useAuthentication");
	jDoc["publishQoS"] = server->arg("publishQoS");
	jDoc["willQoS"] = server->arg("willQoS");
	jDoc["willRetain"] = server->arg("willRetain");
	jDoc["willMessage"] = server->arg("willMessage");
	jDoc["willMessage1"] = "SDS345VAZSDSdfklsdlkfsdklfskdlflsdjkfksdjlfjskdlfkjlsfkjlsdflksdflkslkjrrtioertioeroitreuiot34458435983489589345VAZSDSdfklsdlkfsdklfskdlflsdjkfksdjlfjskdlfkjlsfkjlsdflksdflkslkjrrtioertioeroitreuiot34458435983489589345VAZSDSdfklsdlkfsdklfskdlflsdjkfksdjlfjskdlfkjlsfkjlsdflksdflkslkjrrtioertioeroitreuiot34458435983489589345VAZSDSdfklsdlkfsdklfskdlflsdjkfksdjlfjskdlfkjlsfkjlsdflksdflkslkjrrtioertioeroitreuiot34458435983489589345VAZSDSdfklsdlkfsdklfskdlflsdjkfksdjlfjskdlfkjlsfkjlsdflksdflkslkjrrtioertioeroitreuiot34458435983489589345VAZSDSdfklsdlkfsdklfskdlflsdjkfksdjlfjskdlfkjlsfkjlsdflksdflkslkjrrtioertioeroitreuiot34458435983489589345VAZSDSdfklsdlkfsdklfskdlflsdjkfksdjlfjskdlfkjlsfkjlsdflksdflkslkjrrtioertioeroitreuiot34458435983489589345VAZSDSdfklsdlkfsdklfskdlflsdjkfksdjlfjskdlfkjlsfkjlsdflksdflkslkjrrtioertioeroitreuiot34458435983489589345VAZSDSdfklsdlkfsdklfskdlflsdjkfksdjlfjskdlfkjlsfkjlsdflksdflkslkjrrtioertioeroitreuiot34458435983489589345VAZSDSdfklsdlkfsdklfskdlflsdjkfksdjlfjskdlfkjlsfkjlsdflksdflkslkjrrtioertioeroitreuiot34458435983489589345ZAV";
	
// The willMessage1 on the json file is only to test using more data!

	char cfile[1800];
	
	serializeJson(jDoc,cfile,1800);
	
	int gcsLen = strlen(cfile);
   
        XOR_cryptDecryptChar(cfile,gcsLen); // This will crypt the plaintext without any problems
	
	//XOR_cryptDecryptChar(cfile,gcsLen);
	//Serial.println(cfile); // If i uncomment the line above, text is decrypted without any problems
	
	File configFile = SPIFFS.open("/crypted.json", "w");
	if (!configFile)
		DEBUG_WM(DEBUG_DEV,F("failed to open global config file for writing."));
	configFile.write(cfile,gcsLen);
	configFile.close();

   readFileFromSpiffs(gcsLen);
	
}


void readFileFromSpiffs(int length)
{
	File configFile = SPIFFS.open("/crypted.json", "r");
 
  if (configFile)
  {
	std::unique_ptr<char[]> buf(new char[length]);
    configFile.readBytes(buf.get(), length);
	char file[length];
	strcpy(file,buf.get());
	
	XOR_cryptDecryptChar(file,length); 
	
	Serial.println(file); // This is where i have problems, only part of the file is decrypted
  }

When crypting and decrypting the cfile variable on the saveFile() with the variable on memory, it works perfect. However, when i save the encryption to a file, and then open it to decrypt, that's where only part of the text is decrypted.

Example:

Text without encryption:
{"fileVersion":"1.0.0","brokerURL":"home.pt","brokerPort":"1883","brokerUsername":"teste","brokerPassword":"teste","brokerClientId":"4871129","brokerGeneralTopic":"IOT/nme12e/in","brokerGlobalTopic":"IOT/global/in","useAuthentication":"1","publishQoS":"1","willQoS":"1","willRetain":"1","willMessage":"Disconnected!","willMessage1":"SDS345VAZSDSdfklsdlkfsdklfskdlflsdjkfksdjlfjskdlfkjlsfkjlsdflksdflkslkjrrtioertioeroitreuiot34458435983489589345VAZSDSdfklsdlkfsdklfskdlflsdjkfksdjlfjskdlfkjlsfkjlsdflksdflkslkjrrtioertioeroitreuiot34458435983489589345VAZSDSdfklsdlkfsdklfskdlflsdjkfksdjlfjskdlfkjlsfkjlsdflksdflkslkjrrtioertioeroitreuiot34458435983489589345VAZSDSdfklsdlkfsdklfskdlflsdjkfksdjlfjskdlfkjlsfkjlsdflksdflkslkjrrtioertioeroitreuiot34458435983489589345VAZSDSdfklsdlkfsdklfskdlflsdjkfksdjlfjskdlfkjlsfkjlsdflksdflkslkjrrtioertioeroitreuiot34458435983489589345VAZSDSdfklsdlkfsdklfskdlflsdjkfksdjlfjskdlfkjlsfkjlsdflksdflkslkjrrtioertioeroitreuiot34458435983489589345VAZSDSdfklsdlkfsdklfskdlflsdjkfksdjlfjskdlfkjlsfkjlsdflksdflkslkjrrtioertioeroitreuiot34458435983489589345VAZSDSdfklsdlkfsdklfskdlflsdjkfksdjlfjskdlfkjlsfkjlsdflksdflkslkjrrtioertioeroitreuiot34458435983489589345VAZSDSdfklsdlkfsdklfskdlflsdjkfksdjlfjskdlfkjlsfkjlsdflksdflkslkjrrtioertioeroitreuiot34458435983489589345VAZSDSdfklsdlkfsdklfskdlflsdjkfksdjlfjskdlfkjlsfkjlsdflksdflkslkjrrtioertioeroitreuiot34458435983489589345ZAV"}

Text after saving to file, open it, and decrypt it (readFileFromSpiffs void):
{"fileVersion":"1.0.0","brokerURL":"home.pt","brokerPort":"1883","brokerUsername":"teste","brokerPassword":"teste","brokerClientId":"4871129","brokerGeneralTopic":"IOT/nme12e/in","brokerGlobalTopic":"IOT/global/in","useAuthentication":"1","publishQoS":"1","willQoS":"1","willRetain":"1","willMessage":"Disconnected!","willMessage1":"SDS345VAZSDSdfklsdlkfsdklfskdlflsdjkfksdjlfjskdlfkjlsfkjlsdflksdflkslkjrrtioertioeroitreuiot34458435983489589345VAZSDSdfklsdlkfsdklfskdlflsdjkfksdjlfjskdlfkjlsfkjlsdflksdflkslkjrrtioertioeroitreuiot34458435983489589345VAZSDSdfklsdlkfsdklfskdlflsdjkfksdjlfjskdlfkjlsfkjlsdflksdflkslkjrrtioertioeroitreuiot34458435983489589345VAZSDSdfklsdlkfsdklfskdlflsdjkfksdjlfjskdlfkjlsfkjlsdflksdflkslkjrrtioertioeroitreuiot34458435983489589345VAZSDSdfklsdlkfsdklfskdlflsdjkfksdjlfjskdlfkjlsfkjlsdflksdflkslkjrrtioertioeroitreuiot34458435983489589345VAZ��%��Zz␟�
Z�kZ�d�␚�"␅␅�ó�[�Z��5m�J�kں��u2W��t�␒���␅␅Z~�
�/{␚Z~␒�␑l�␚:*M5�f{�+��␚�"����␒�ڴ�Z��␂�ʪ�Z:*MuՂ�Z�␡␂ڒb␅␅Zt��Z5kZ5␜sv�µ/z␔�
�/{␚i␐�)�

Any idea on what is causing this ?

strcpy(file,buf.get());

What happens, when one of the data bytes gets encrypted to 0x00, aka the zero terminator?
HINT: There is a function with a very similar name, that does what you want - strncpy.

bool XOR_cryptDecryptChar(char * input, int length)
{
<snip>
  return true;
}

What is the point of declaring that the function returns a bool, when it only ever returns true?

DynamicJsonDocument jDoc(1800);

On which Arduino can you reserve 1800 bytes of SRAM?

jDoc["fileVersion"] = String("1.0.0");

Just plain stupid. There is NO excuse for wrapping the string literal in a String instance.

char cfile[1800];

Now, you are using more than 2600 bytes of SRAM. How much do you have?

 int keysize = strlen(key);
 
  for(int x=0; x<=length; x++)
  {
    for(int i=0; i<=keysize;i++)

I don't think so. Think about how much data you really want to process, and what happens when you sh*t on memory you don't own.

PaulS:

bool XOR_cryptDecryptChar(char * input, int length)

{

  return true;
}



What is the point of declaring that the function returns a bool, when it only ever returns true?



DynamicJsonDocument jDoc(1800);



On which Arduino can you reserve 1800 bytes of SRAM?



jDoc["fileVersion"] = String("1.0.0");



Just plain stupid. There is NO excuse for wrapping the string literal in a String instance.



char cfile[1800];



Now, you are using more than 2600 bytes of SRAM. How much do you have?



int keysize = strlen(key);

for(int x=0; x<=length; x++)
  {
    for(int i=0; i<=keysize;i++)



I don't think so. Think about how much data you really want to process, and what happens when you sh*t on memory you don't own.

Its a NodeMCU esp8266

LightuC:

strcpy(file,buf.get());

What happens, when one of the data bytes gets encrypted to 0x00, aka the zero terminator?
HINT: There is a function with a very similar name, that does what you want - strncpy.

Better go with memcpy().

gfvalvo:

Better go with memcpy().

Done, but that's not resolving my problem :confused:

gfvalvo,
that seems to almost solved the problem, now i only have this after decrypted text which is now fully decrypted: q�?�␅ maybe is the way i am writting it to the file ?

gfvalvo:

Better go with memcpy().

gfvalvo:
[...]

Better go with memcpy().

Damn this memory of mine ...

vaz83:
[...] maybe is the way i am writting it to the file ?

No, its the way you use strlen.

http://www.cplusplus.com/reference/cstring/strlen/:
The length of a C string is determined by the terminating null-character: A C string is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself).

You are writing and reading the serialized json without the null-terminator. Serial.print uses the terminator to stop printing, so you need to append a '\0' at the end of your array

char file[length+1];

// ...

// Assuming the array is filled with length bytes, then append the terminator after that
file[length] = '\0';