AES arduino加密代码解密成C# [英] AES arduino encrypted codes decrypted into C#

查看:95
本文介绍了AES arduino加密代码解密成C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,



我想要在Arduino-C中创建加密的代码,可以解密成c#。



有可能吗?



使用算法AES或任何其他algorthim



任何人帮助我,可以提供代码







AJ



我尝试过:



  #include     AES.h 
#include < span class =code-string> base64.h

AES aes;

void setup(){

Serial.begin( 9600 );
Serial.println( \ nnBooting ...);

char b64data [ 200 ];
字节密码[ 1000 ];
字节iv [N_BLOCK];

Serial.println( 让我们加密:);
// 我们的AES密钥。注意,它与Node-Js端使用的相同,但是作为十六进制字节。
byte key [] = {0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30 ,0x30,0x30,0x30,0x30,0x30,0x30,0x30};

// 酉初始化向量
// 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};

// 我们要加密的消息。此示例为静态。
// 字符串msg ={\data \ :{\value \:300},\SEQN\:700,\msg \:\IT WORKS !! \};
字符串 msg = ABC;


aes.iv_inc();
aes.set_key(key, sizeof (key)); // 获取全局定义的密钥


// 打印IV
base64_encode(b64data,( char *)my_iv,N_BLOCK);
Serial.println( IV b64: + 字符串(b64data));

Serial.println( 消息: + msg);

int b64len = base64_encode(b64data,( char *)msg.c_str (),msg.length());
Serial.println( B64中的消息: + String (b64data));
Serial.println( 长度为: + String (b64len));

// 用于完整性检查
// base64_decode(已解码,b64data,b64len);
/ / Serial.println(Decoded:+ String(decoding));

// 加密!使用AES128,我们的密钥和IV,CBC和pkcs7填充
aes.do_aes_encrypt((byte *)b64data,b64len,cipher,key, 128 ,my_iv);

Serial.println( 加密完成!);

Serial.println( 密码大小: + 字符串(aes.get_size()));

b64len = base64_encode(b64data,( char *)cipher,aes.get_size());
Serial.println( base64中的加密数据: + String (b64data));

Serial.println( 完成......);
字符串 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( B64中的普通: + 字符串(( char *)plain));
Serial.println( 普通尺寸: + 字符串(aes.get_size()));
Serial.println( 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());
//返回String(消息);
} * /

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 ];
字节输出[ 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( base64中的Oyair aram数据: + String (message));
}
void loop(){
// 把你的主要代码放在这里,重复运行:
}

解决方案

引用:

我已经在c-Arduino中完成了加密提及代码,并希望将其解密为c#

但是当我们尝试在c#中解密它接收不同的错误



我们无法直接帮助你 - 变量太多了,我们无法运行在相同的情况下你的代码的两面。

所以你需要做的第一件事是找出问题所在。

首先加密一个已知的二进制数据文件(使用已知的工作AES算法和特定的已知密钥,在PC上制作一些简单的东西,比如字母表中的字母)。使用Arduino上相同的数据文件和密钥重复加密。

比较输出文件。它们是一样的吗?如果是这样的话,问题很可能出在你的C#代码中,你需要尝试将已知良好输出提供给它,看看你得到了什么。如果不是,问题出在Arduino端,你需要仔细看看你在那里做了什么。



我们不能这样做你!

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:

#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:
}

解决方案

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!


这篇关于AES arduino加密代码解密成C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆