You are on page 1of 6

// Elektroenergetski softverski inzenjering

// Primenjene racunarske mreze u namenskim sistemima 2


// Vezba 11 - Enkripcija

// We do not want the warnings about the old deprecated and unsecure CRT functions
since these examples can be compiled under *nix as well
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

// Include libraries
#include <stdlib.h>
#include <stdio.h>
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include "conio.h"
#include "pcap.h"
#include "protocol_headers.h"

// Function declarations
void packet_handler(unsigned char *param, const struct pcap_pkthdr *packet_header,
const unsigned char *packet_data); // Callback function invoked by WinPcap for
every incoming packet
unsigned char* encrypt_data(const unsigned char* packet_data, unsigned char*
app_data, int app_length); // Returns a copy of packet
with encrypted application data
void print_message_as_table(unsigned char* data, int rows_max, int columns_max);
// Prints application data
using table format
int find_key(int key, int * keys, int keys_size);
// Finds
specific key in array of keys and returns its index

#define ETHERNET_FRAME_MAX 1518 // Maximal length of ethernet frame

// Main function captures packets from the file


int main()
{
pcap_t* device_handle;
char error_buffer[PCAP_ERRBUF_SIZE];

// Open the capture file


if ((device_handle = pcap_open_offline("original_packets.pcap", // Name of
the device
error_buffer // Error buffer
)) == NULL)
{
printf("\n Unable to open the file %s.\n", "example.pcap");
return -1;
}

// Open the dump file


pcap_dumper_t* file_dumper = pcap_dump_open(device_handle,
"encrypted_packets.pcap");

if (file_dumper == NULL)
{
printf("\n Error opening output file\n");
return -1;
}

// Check the link layer. We support only Ethernet for simplicity.


if(pcap_datalink(device_handle) != DLT_EN10MB)
{
printf("\nThis program works only on Ethernet networks.\n");
return -1;
}

struct bpf_program fcode;

// Compile the filter


if (pcap_compile(device_handle, &fcode, "ip and udp", 1, 0xffffff) < 0)
{
printf("\n Unable to compile the packet filter. Check the syntax.\n");
return -1;
}

// Set the filter


if (pcap_setfilter(device_handle, &fcode) < 0)
{
printf("\n Error setting the filter.\n");
return -1;
}

// Read and dispatch packets until EOF is reached


pcap_loop(device_handle, 0, packet_handler, (unsigned char*) file_dumper);

// Close the file associated with device_handle and deallocates resources


pcap_close(device_handle);

return 0;
}

// Callback function invoked by WinPcap for every incoming packet


void packet_handler(unsigned char* file_dumper, const struct pcap_pkthdr*
packet_header, const unsigned char* packet_data)
{
/* DATA LINK LAYER - Ethernet */

// Retrive the position of the ethernet header


ethernet_header * eh = (ethernet_header *)packet_data;

/* NETWORK LAYER - IPv4 */

// Retrieve the position of the ip header


ip_header* ih = (ip_header*) (packet_data + sizeof(ethernet_header));

// TRANSPORT LAYER - UDP


if(ih->next_protocol != 17)
{
return;
}

// Retrieve the position of udp header


udp_header* uh = (udp_header*) ((unsigned char*)ih + ih->header_length * 4);
// Retrieve the position of application data
unsigned char* app_data = (unsigned char *)uh + sizeof(udp_header);

// Total length of application data


int app_length = ntohs(uh->datagram_length) - sizeof(udp_header);

// Encrypt data using tranposition of rows and columns


unsigned char * encrypted_packet = encrypt_data(packet_data, app_data,
app_length);

// Dump encrypted packets


pcap_dump((unsigned char*) file_dumper, packet_header, encrypted_packet);
}

void matrix_element_generator(char key_word[], char matrix_element[], int length)


{
char letters[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S','T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; //
without 'J'

for (int i = 0; i < length; i++)


{
matrix_element[i] = key_word[i];
}

int brojac = 0;
for (int i = 0; i < 25; i++)
{
bool exist = false;
char letter = letters[i];
for (int j = 0; j < length; j++)
{
if (matrix_element[j] == letter)
{
exist = true;
}
}

if (exist == false)
{
matrix_element[length + brojac] = letter;
brojac++;
}
}
}

// Returns a copy of packet with encrypted application data


unsigned char* encrypt_data(const unsigned char* packet_data, unsigned char*
app_data, int app_length)
{
// Reserve memory for copy of the packet
unsigned char encrypted_packet[ETHERNET_FRAME_MAX];

memcpy(encrypted_packet, packet_data, (app_data - packet_data));


memset(encrypted_packet + (app_data - packet_data), 0, app_length);

char key_word[] = { 'F', 'T', 'N' };


char matrix_elements[25];
char key_matrix[5][5];
matrix_element_generator(key_word, matrix_elements, 3);

for (int i = 0; i < 5; i++)


{
for (int j = 0; j < 5; j++)
{
key_matrix[i][j] = matrix_elements[i * 5 + j];
}
}

for (int i = 0; i < 5; i++)


{
for (int j = 0; j < 5; j++)
{
printf("%c ", key_matrix[i][j]);
}

printf("%\n");
}

for (int i = 0; i < app_length; i++)


{
char prvi = app_data[i];
char drugi;
bool tacno = false;

if (i == app_length - 1)
{
tacno = true;
}

if (tacno == true)
{
if (app_length % 2 == 0)
{
drugi = app_data[i + 1];
}
else
{
drugi = 'X';
}
}
else
{
drugi = app_data[i + 1];
}

printf("Prvi: %c \n", prvi);


printf("Drugi: %c \n", drugi);

int prvi_red = -1;


int drugi_red = -1;
int prvi_kolona = -1;
int drugi_kolona = -1;

for (int a = 0; a < 5; a++)


{
for (int b = 0; b < 5; b++)
{
if (prvi == key_matrix[a][b])
{
prvi_red = a;
prvi_kolona = b;
}

if (drugi == key_matrix[a][b])
{
drugi_red = a;
drugi_kolona = b;
}
}
}

printf("Prvi red: %d\n", prvi_red);


printf("Prvi kolona: %d\n", prvi_kolona);
printf("Drugi red: %d\n", drugi_red);
printf("Drugi kolona: %d\n", drugi_kolona);

getchar();

if (prvi_kolona == drugi_kolona)
{
if (prvi_red + 1 == 5)
{
encrypted_packet[app_data - packet_data + i] =
key_matrix[0][prvi_kolona];
}
else
{
encrypted_packet[app_data - packet_data + i] =
key_matrix[prvi_red + 1][prvi_kolona];
}

if (drugi_red + 1 == 5)
{
encrypted_packet[app_data - packet_data + i + 1] =
key_matrix[0][drugi_kolona];
}
else
{
encrypted_packet[app_data - packet_data + i + 1] =
key_matrix[drugi_red + 1][drugi_kolona];
}
}
else if (prvi_red == drugi_red)
{
if (prvi_kolona + 1 == 5)
{
encrypted_packet[app_data - packet_data + i] =
key_matrix[prvi_red][0];
}
else
{
encrypted_packet[app_data - packet_data + i] =
key_matrix[prvi_red][prvi_kolona + 1];
}
if (drugi_kolona + 1 == 5)
{
encrypted_packet[app_data - packet_data + i + 1] =
key_matrix[drugi_red][0];
}
else
{
encrypted_packet[app_data - packet_data + i + 1] =
key_matrix[drugi_red][drugi_kolona + 1];
}
}
else if (prvi_red != drugi_red && prvi_kolona != drugi_kolona)
{
encrypted_packet[app_data - packet_data + i] =
key_matrix[prvi_red][drugi_kolona];
encrypted_packet[app_data - packet_data + i + 1] =
key_matrix[drugi_red][prvi_kolona];
}
i++;
}

return encrypted_packet;
}

You might also like