Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Greeting,

I want to codes which create Encryption in Arduino-C and can be Decrypted into c#.

is it possible ?

Using algorithm AES or any other an algorthim

anyone help me, can provide codes



AJ

What I have tried:

C++
#include "AES.h"
#include "base64.h"

AES aes;

void setup() {
  
  Serial.begin(9600);
  Serial.println("\nBooting...");

  char b64data[200];
  byte cipher[1000];
  byte iv [N_BLOCK] ;

  Serial.println("Let's encrypt:");
  // Our AES key. Note that is the same that is used on the Node-Js side but as hex bytes.
  byte key[] = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 };

  // The unitialized Initialization vector
  //byte my_iv[N_BLOCK] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  byte my_iv[N_BLOCK] = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 };

  // Our message to encrypt. Static for this example.
  //String msg = "{\"data\":{\"value\":300}, \"SEQN\":700 , \"msg\":\"IT WORKS!!\" }";
  String msg = "ABC";


aes.iv_inc();
  aes.set_key( key , sizeof(key));  // Get the globally defined key


  // Print the IV
  base64_encode( b64data, (char *)my_iv, N_BLOCK);
  Serial.println(" IV b64: " + String(b64data));

  Serial.println("Message: " + msg );

  int b64len = base64_encode(b64data, (char *)msg.c_str(), msg.length());
  Serial.println (" Message in B64: " + String(b64data) );
  Serial.println (" The lenght is:  " + String(b64len) );
  
  // For sanity check purpose
  //base64_decode( decoded , b64data , b64len );
  //Serial.println("Decoded: " + String(decoded));

  // Encrypt! With AES128, our key and IV, CBC and pkcs7 padding
  aes.do_aes_encrypt((byte *)b64data, b64len , cipher, key, 128, my_iv);

  Serial.println("Encryption done!");

  Serial.println("Cipher size: " + String(aes.get_size()));

  b64len = base64_encode(b64data, (char *)cipher, aes.get_size() );
  Serial.println ("Encrypted data in base64: " + String(b64data) );

  Serial.println("Done...");
  String t="vVnzJGbo0IDNBywnkVxGXw==";
 //  b64len = base64_encode(b64data, (char *)t.c_str(), t.length());

  byte plain[1000];
  b64len = base64_decode(b64data, b64data, b64len);
 // aes.do_aes_decrypt((byte *)b64data, b64len, plain, key, 128, my_iv);
  test(t,key,my_iv,b64len);
  base64_decode(b64data, (char *)plain, aes.get_size());
  Serial.println (" Plain in B64: " + String((char *)plain) );
  Serial.println(" Plain size: " + String(aes.get_size()));
  Serial.println (" Decrypted data in base64: " + String(b64data) );
//  Serial.println ("Decrypted data: " + plain );
}
 /* void adecrypt(String msg, byte key1[], String my_iv) {
  
  aes.set_key(key1, sizeof(key1));
  char data_decoded[200];
  char iv_decoded[200];
  byte out[200];
  char temp[200];
  msg.toCharArray(temp, 200);
  int b64len = base64_decode(data_decoded, temp, msg.length());
  my_iv.toCharArray(temp, 200);
  base64_decode(iv_decoded, temp, my_iv.length());
  aes.do_aes_decrypt((byte *)data_decoded, b64len, out, key1, 128, (byte *)iv_decoded);
  
  char message[msg.length()];
  base64_decode(message, (char *)out, msg.length());
 // return String(message);
} */
void test(String msg, byte key1[], byte my_iv[],int b64len1)
{
 // aes.set_key(key1, sizeof(key1));
  char data_decoded[200];
  char iv_decoded[200];
  byte out[200];
  char temp[200];
  msg.toCharArray(temp, 200);
  int b64len = base64_decode(data_decoded, temp, msg.length());
//  my_iv.toCharArray(temp, 200);
  base64_decode(iv_decoded, temp, b64len1);
  aes.do_aes_decrypt((byte *)data_decoded, b64len, out, key1, 128, (byte *)iv_decoded);
  
  char message[msg.length()];
  base64_decode(message, (char *)out, msg.length());
   Serial.println (" Oyair aram data in base64: " + String(message) );
  }
void loop() {
  // put your main code here, to run repeatedly:
}
Posted
Updated 10-Dec-18 22:15pm
v2
Comments
Kornfeld Eliyahu Peter 14-Oct-18 15:29pm    
Unclear...
Are you asking if it is possible to decrypt in C# something that was encrypted in C? Yes, it is!
Or you are asking what's wrong with your code? How we could know? You tell us and we may able to tell the why!
alisolution 15-Oct-18 5:32am    
i have done encrypted mention code in c-Arduino and want to decrypted it into c#
but when we try to decrypt it in c# receiving different error

any code or helping URL.

regards
AJ


Quote:
i have done encrypted mention code in c-Arduino and want to decrypted it into c#
but when we try to decrypt it in c# receiving different error

We can't help you directly - there are too many variables, and we can't run both sides of your code under the same circumstances.
So the first thing you need to do is find out where the problem is.
Start by encrypting a known binary data file (make it something simple, like just teh letters of the alphabet perhaps) on your PC using a known working AES algorithm and a specific known key. Repeat the encryption using the same datafile and key on your Arduino.
Compare the output files. Are they identical? If they are, the problem is likely to be in your C# code, and you need to try feeding the known-good-output into that and see what you get. If they aren't, the problem is at the Arduino end, and you need to look at exactly what you are doing there.

We can't do that for you!
 
Share this answer
 
I have just published an article exactly answered this question. I know my post it's a little late to the party, but anyway, here's my article. cheers

AES Encrypted Data Transmission between Arduino (ESP32) and C# (ASP.NET)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900