Hi,
it hasn't been an easy ride trying to do this
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 ?