Hello! I am trying to encrypt a message on the Arduino using ChaCha20 encryption algorithm but when i am decrypting the received message on the computer using Python it does not resemble the same message. For some reason they are both different when encrypting and decrypting the same plaintext. The same thing applies when using AES. Is this a know issue or does anyone know a workaround to it ?
/* ----------- ENCRYPT DATA ----------- */
void encrypt(ChaChaPoly *cipher, byte key[], byte iv[], byte plaintext[], byte ciphertext[], byte tag[]) {
cipher->clear();
cipher->setKey(key, sizeof(key));
cipher->setIV(iv, sizeof(iv));
cipher->encrypt(ciphertext, plaintext, sizeof(plaintext));
cipher->computeTag(tag, sizeof(tag));
}
/* ----------- PRINT UNDERSTANDABLE CODE FOR PYTHON ----------- */
void printFinal(byte data1[], int len1, byte data2[], int len2, byte data3[]){
Serial.print("[");
printHex(data1, len1);
Serial.print("|");
printHex(data2, len2);
Serial.print("|");
printHex(data3, sizeof(data3));
Serial.print("]");
}
unsigned long uid = getID();
unsigned long timenow = now();
String ToSend = uid + "|" + timenow;
byte text[32];
for(int i=0; i< 32; i++)
text[i] = ToSend[i];
byte ciphertext[sizeof(text)];
encrypt(&cha, key, iv, text, ciphertext, tag);
printFinal(tag, 16, iv, 12, ciphertext);