Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
478 views
in Technique[技术] by (71.8m points)

node.js - Arduino AES ESP8266

i am using a library to code a message to AES CBC library : https://github.com/suculent/thinx-aes-lib version 2.2.0 from nodeJS i have :

CLEAR DATA: TEST
IV: 3b698860538401cdd146504278aeecc9
KEY: df4f66809bafe58fe16aba7f4cb3e8bc
ENCRYPTED DATA : 560d5ecae14404f4d143931d6d4e4705

i want to encode the message but i cant , the cipher i am getting is diffrent than the cipher i got from NodeJS

tried many ways but all faild

#include "AESLib.h"
// TEST MUST BE OUTPUT

//byte KEY[]         = {0xdf , 0x4f , 0x66 , 0x80 , 0x9b , 0xaf , 0xe5 , 0x8f , 0xe1 , 0x6a , 0xba , 0x7f , 0x4c , 0xb3 , 0xe8 , 0xbc};
//byte IV[N_BLOCK]  = {0x3b , 0x69 , 0x88 , 0x60 , 0x53 , 0x84 , 0x01 , 0xcd , 0xd1 , 0x46 , 0x50 , 0x42 , 0x78 , 0xae , 0xec , 0xc9};
//byte DATA[]       ={0x56 , 0x0d , 0x5e , 0xca , 0xe1 , 0x44 , 0x04 , 0xf4 , 0xd1 , 0x43 , 0x93 , 0x1d , 0x6d , 0x4e , 0x47 , 0x05 };

    #define BAUD 115200
    
    AESLib aesLib;

#define INPUT_BUFFER_LIMIT (128 + 1) // designed for Arduino UNO, not stress-tested anymore (this works with readBuffer[129])

unsigned char cleartext[INPUT_BUFFER_LIMIT] = {0}; // THIS IS INPUT BUFFER (FOR TEXT)
unsigned char ciphertext[2*INPUT_BUFFER_LIMIT] = {0}; // THIS IS OUTPUT BUFFER (FOR BASE64-ENCODED ENCRYPTED DATA)

unsigned char readBuffer[] = "TEST";

// AES Encryption Key (same as in node-js example)
byte aes_key[]       = {0xdf , 0x4f , 0x66 , 0x80 , 0x9b , 0xaf , 0xe5 , 0x8f , 0xe1 , 0x6a , 0xba , 0x7f , 0x4c , 0xb3 , 0xe8 , 0xbc};

// General initialization vector (same as in node-js example) (you must use your own IV's in production for full security!!!)
byte aes_iv[N_BLOCK] = {0x3b , 0x69 , 0x88 , 0x60 , 0x53 , 0x84 , 0x01 , 0xcd , 0xd1 , 0x46 , 0x50 , 0x42 , 0x78 , 0xae , 0xec , 0xc9};

// Generate IV (once)
void aes_init() {
  aesLib.set_paddingmode(paddingMode::CMS);
}

uint16_t encrypt_to_ciphertext(char * msg, uint16_t msgLen, byte iv[]) {
  Serial.println("Calling encrypt (string)...");
  // aesLib.get_cipher64_length(msgLen);
  int cipherlength = aesLib.encrypt((byte*)msg, msgLen, (char*)ciphertext, aes_key, sizeof(aes_key), iv);
  
        //// HERE IS WHERE I READ THE CIPHER            
    Serial.println();
    for(int i=0;i < cipherlength ;i++)
    {
      Serial.printf("%02X",ciphertext[i]);
    }
    Serial.println();
    
  return cipherlength;
}

uint16_t decrypt_to_cleartext(byte msg[], uint16_t msgLen, byte iv[]) {
  Serial.print("Calling decrypt...; ");
  uint16_t dec_bytes = aesLib.decrypt(msg, msgLen, (char*)cleartext, aes_key, sizeof(aes_key), iv);
  Serial.print("Decrypted bytes: "); Serial.println(dec_bytes);
  return dec_bytes;
}

void setup() {
  Serial.begin(BAUD);
  Serial.setTimeout(60000);
  delay(2000);

  aes_init(); 

}

/* non-blocking wait function */
void wait(unsigned long milliseconds) {
  unsigned long timeout = millis() + milliseconds;
  while (millis() < timeout) {
    yield();
  }
}

unsigned long loopcount = 0;


byte enc_iv[N_BLOCK] =      {0x3b , 0x69 , 0x88 , 0x60 , 0x53 , 0x84 , 0x01 , 0xcd , 0xd1 , 0x46 , 0x50 , 0x42 , 0x78 , 0xae , 0xec , 0xc9};
byte enc_iv_to[N_BLOCK]   = {0x3b , 0x69 , 0x88 , 0x60 , 0x53 , 0x84 , 0x01 , 0xcd , 0xd1 , 0x46 , 0x50 , 0x42 , 0x78 , 0xae , 0xec , 0xc9};
byte enc_iv_from[N_BLOCK] = {0x3b , 0x69 , 0x88 , 0x60 , 0x53 , 0x84 , 0x01 , 0xcd , 0xd1 , 0x46 , 0x50 , 0x42 , 0x78 , 0xae , 0xec , 0xc9};

void loop() {

  Serial.print("readBuffer length: "); Serial.println(sizeof(readBuffer));

   // must not exceed INPUT_BUFFER_LIMIT bytes; may contain a newline
  sprintf((char*)cleartext, "%s", readBuffer);

  // Encrypt
  // iv_block gets written to, provide own fresh copy... so each iteration of encryption will be the same.
  uint16_t msgLen = sizeof(readBuffer);
  memcpy(enc_iv, enc_iv_to, sizeof(enc_iv_to));
  uint16_t encLen = encrypt_to_ciphertext((char*)cleartext, msgLen, enc_iv);
  Serial.print("Encrypted length = "); Serial.println(encLen );

//  Serial.println("Encrypted. Decrypting..."); Serial.println(encLen ); Serial.flush();
//  memcpy(enc_iv, enc_iv_from, sizeof(enc_iv_from));
//  uint16_t decLen = decrypt_to_cleartext(ciphertext, encLen , enc_iv);
//  Serial.print("Decrypted cleartext of length: "); Serial.println(decLen);
//  Serial.print("Decrypted cleartext:
"); Serial.println((char*)cleartext);
//
//  if (strcmp((char*)readBuffer, (char*)cleartext) == 0) {
//    Serial.println("Decrypted correctly.");
//  } else {
//    Serial.println("Decryption test failed.");
//  }

  Serial.println("---");
  delay(5000);
}

dose any one know why i am getting the wrong output ? my output is : 7673424862437068714A722B30466C625146346B47773D3D which should be 560d5ecae14404f4d143931d6d4e4705

question from:https://stackoverflow.com/questions/65896668/arduino-aes-esp8266

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...