SHARE
TWEET

Untitled

a guest Mar 21st, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include "RSA.h"
  3. #include "AES.h"
  4. #include "WiFi.h"
  5. #include "WiFiUdp.h"
  6. #define MAX_BUFFER_SIZE 255
  7. char packetBuffer[MAX_BUFFER_SIZE];   //Where we get the UDP data
  8.  
  9. // WiFi network name and password:
  10. const char * networkName = "ESP32SOFTAP";
  11. const char * networkPswd = "testpassword";
  12. BigNumber RsaKey = 0;
  13.  
  14. //THATS MY KONG FU
  15. int AES_KEY[16] = {0x54, 0x48, 0x41, 0x54, 0x53, 0x20, 0x4d, 0x59, 0x20, 0x4b, 0x55, 0x4e, 0x47, 0x20, 0x46, 0x55};
  16.  
  17. //IP address to send UDP data to:
  18. // either use the ip address of the server or
  19. // a network broadcast address
  20. const char * udpAddress = "192.168.4.1";
  21. const int udpPort = 2000;
  22.  
  23. //Are we currently connected?
  24. boolean connected = false;
  25.  
  26. //The udp library class
  27. WiFiUDP udp;
  28.  
  29.  
  30. // Methods below
  31. String readFromClient(){
  32.   String temp = "";
  33.   while (temp == ""){ // Skal løbe i while loop, da vi skal læse indtil der kommer noget andet end "".
  34.     udp.parsePacket();
  35.     while(udp.read(packetBuffer,MAX_BUFFER_SIZE)>0){
  36.       // We've received a UDP packet, send it to serial
  37.       udp.read(packetBuffer, MAX_BUFFER_SIZE); // read the packet into the buffer, we are reading only one byte
  38.       delay(20);
  39.     }
  40.     temp = packetBuffer;
  41.   }
  42.   return temp;
  43. }
  44.  
  45. void connectToWiFi(const char * ssid, const char * pwd){
  46.   Serial.println("Connecting to WiFi network: " + String(ssid));
  47.  
  48.   // delete old config
  49.   WiFi.disconnect(true);
  50.   //register event handler
  51.   WiFi.onEvent(WiFiEvent);
  52.  
  53.   //Initiate connection
  54.   WiFi.begin(ssid, pwd);
  55.  
  56.   Serial.println("Waiting for WIFI connection...");
  57. }
  58.  
  59.  
  60. //Wifi event handler
  61. void WiFiEvent(WiFiEvent_t event){
  62.     switch(event) {
  63.       case SYSTEM_EVENT_STA_GOT_IP:
  64.           //When connected set
  65.           Serial.print("WiFi connected! IP address: ");
  66.           Serial.println(WiFi.localIP());  
  67.           //initializes the UDP state
  68.           //This initializes the transfer buffer
  69.           udp.begin(WiFi.localIP(),udpPort);
  70.           connected = true;
  71.           break;
  72.       case SYSTEM_EVENT_STA_DISCONNECTED:
  73.           Serial.println("WiFi lost connection");
  74.           connected = false;
  75.           break;
  76.     }
  77. }
  78.  
  79. // Cast string to BigNumber
  80. BigNumber castToBignumber(String msg){
  81.   char temp[(msg.length()+1)];
  82.   msg.toCharArray(temp, (msg.length()+1));
  83.   return temp;
  84. }
  85.  
  86. void clearBuffer(){
  87.   for(int i = 0; i < MAX_BUFFER_SIZE; i++)
  88.     packetBuffer[i] = 0;
  89. }
  90.  
  91. void hexToCharArray(char *src){
  92.   int place = 0;
  93.   for (int i=0; i<16; i++)
  94.     place += sprintf(&src[place], "%d", AES_KEY[i]); //this is magic man.
  95. }
  96.  
  97. void sendStringPacket(string msg){
  98.   udp.beginPacket(ClientIP,UDPPort);
  99.   udp.print(msg);
  100.   udp.endPacket();
  101. }
  102.  
  103. void CompleteKeySetup(){
  104.   //STEP 1 RECEIVE RSA KEY//
  105.   String RsaKeyString = readFromClient();
  106.   RsaKey = castToBignumber(RsaKeyString);
  107.   Serial.println(RsaKey);
  108.  
  109.   // TODO Check om vi kan slette dette
  110.   udp.beginPacket(udpAddress,udpPort); //for whatever reason we need to begin an empty packet and end it??? why?
  111.   udp.endPacket();
  112.  
  113.   //Wait for 0.01 second
  114.   delay(10);
  115.  
  116.   sendStringPacket("RsaKey ACK");
  117.  
  118.   //STEP 2 ENCRYPT AES KEY WITH RSA PUBLIC KEY//
  119.   char holdkey[33];
  120.   hexToCharArray(holdkey);
  121.  
  122.   BigNumber AES_CONVERTED_KEY_CHAR = holdkey; //key is now converted from int array to char array and then varible set as bignumber
  123.   BigNumber AES_ENC = Encrypt(AES_CONVERTED_KEY_CHAR,RsaKey);
  124.  
  125.   udp.beginPacket(udpAddress,udpPort);
  126.   udp.print(AES_ENC);
  127.   udp.endPacket();
  128.  
  129.   Serial.println("SENT ENCRYPTED AES KEY TO RECEIVER");
  130.   Serial.println("AWAITING ACK FROM RECEIVER");
  131.   Serial.println();
  132.  
  133.   clearBuffer();
  134.  
  135.   Serial.print("I RECEIVED : ");
  136.   String ACK = readFromClient();
  137.   Serial.println(ACK);
  138. }
  139.  
  140. void setup(){
  141.   // Initilize hardware serial:
  142.   Serial.begin(115200);
  143.   BigNumber::begin (); //Dette *SKAL* MED!!!
  144.   BigNumber RSA_KEY_ENCRYPTED[16];
  145.  
  146.   // Connect to the WiFi network
  147.   connectToWiFi(networkName, networkPswd);
  148.  
  149.   // After WiFi connection established, make key setup.
  150.   if (connection){
  151.     CompleteKeySetup();
  152.   }
  153. }
  154.  
  155. void loop(){
  156.   //only send data when connected
  157.  
  158.  
  159. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top