Skip to content

Instantly share code, notes, and snippets.

@paulocoutinhox
Last active July 19, 2017 19:37
Show Gist options
  • Save paulocoutinhox/1bdaf36530c12d29c0a063ad3dee042e to your computer and use it in GitHub Desktop.
Save paulocoutinhox/1bdaf36530c12d29c0a063ad3dee042e to your computer and use it in GitHub Desktop.
Encrypted to:
31FFFFFFC013FFFFFF8C774FFFFFFF86FFFFFF9D206C545E2669FFFFFFACFFFFFFC37300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
OK!
Tag:
000000000000000031FFFFFFC013FFFFFF8C774FFFFFFF86FFFFFF9D206C545E2669FFFFFFACFFFFFFC3730000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
OK!
Decrypted to:
{id: "12345678"}}? ����r���R����:T�c�1V_����5D0ɇQ�S�J��"�U���ڈ�0i��t,�]Tb[�^�w����J8��$`ې�X8��p@!�'�iB$:4�A�9�֌q������B���Bj�g8�\��ڏD�Y����7c���m�R�P����V��)�JW6�q_(\v.��!�I��,;���oO"�o��͓��4������T�e�i�ҫ �uF�fB�F�"���PE��<E�h
OK!
Payload:
{id: "12345678"}1231212312345678012345678901234567890123456789019���*. '$6  ��Y>& 85� A � ; m2�� �
Encrypted to:OK!Tag:Decrypted to:Payload:{id: "12345678"}}? ����r���R����:T�c��1V_��ɥ5D0��Q�S�J��"�U���ڈ�0i�
OK!
#include "Crypto.h"
#include "ChaChaPoly.h"
#include <string.h>
#if (ESP8266)
#include <pgmspace.h>
#else
#include <avr/pgmspace.h>
#endif
#define MAX_PLAINTEXT_LEN 265
byte cipherBufferE[MAX_PLAINTEXT_LEN];
byte cipherBufferD[MAX_PLAINTEXT_LEN];
ChaChaPoly *cipher = new ChaChaPoly();
const uint8_t cipherKey[] = "01234567890123456789012345678901";
const uint8_t cipherIV[] = "12345678";
const uint8_t cipherAuth[] = "12312123";
uint8_t cipherTag[16];
const uint8_t payload[] = "{id: \"12345678\"}";
void setup() {
Serial.begin(9600);
cipher->clear();
cipher->setKey(cipherKey, sizeof(cipherKey));
cipher->setIV(cipherIV, sizeof(cipherIV));
cipher->addAuthData(cipherAuth, sizeof(cipherAuth));
cipher->encrypt(cipherBufferE, payload, sizeof(payload));
Serial.println("Encrypted to:");
printArray(cipherBufferE);
Serial.println("OK!");
Serial.println("Tag:");
cipher->computeTag(cipherTag, sizeof(cipherTag));
printArray(cipherTag);
Serial.println("OK!");
cipher->clear();
cipher->setKey(cipherKey, sizeof(cipherKey));
cipher->setIV(cipherIV, sizeof(cipherIV));
cipher->addAuthData(cipherAuth, sizeof(cipherAuth));
cipher->decrypt(cipherBufferD, cipherBufferE, sizeof(cipherBufferE));
Serial.println("Decrypted to:");
printArray2(cipherBufferD);
Serial.println("OK!");
Serial.println("Payload:");
printArray2(payload);
Serial.println("OK!");
}
void loop() {
// put your main code here, to run repeatedly:
}
void printArray(const uint8_t data[]) {
int sizeOfData = MAX_PLAINTEXT_LEN; //(sizeof(data)/sizeof(*data));
/*
Serial.println("Size:");
Serial.println(sizeOfData);
delay(100);
*/
for (int i = 0; i < sizeOfData; i++) {
Serial.print((char)data[i], HEX);
}
Serial.println(" ");
}
void printArray2(const uint8_t data[]) {
int sizeOfData = MAX_PLAINTEXT_LEN; //(sizeof(data)/sizeof(*data));
/*
Serial.println("Size:");
Serial.println(sizeOfData);
delay(100);
*/
for (int i = 0; i < sizeOfData; i++) {
Serial.print((char)data[i]);
}
Serial.println(" ");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment