I am working on a little project of mine. Nothing major, just messing around with encryption etc...
I have problem thoug. My methods do not change the values of my data. Meaning that the pointer that I pass to hold my encrypted data, doesn't get the data assigned to its memory.
There are 3 files, but only these 2 are needed:
`EncryptDecrypt.h
/*
Encrypts and decrypts the data.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int encrypt(char *data, char *encrypted_data, int **key);
int decrypt(char *data, char *decrypted_data, int **key);
// Data changing functions.
void shift_data_left(char *data, char *encrypted_data, int **key, int block_size);
void shift_data_right(char *data, char *encrypted_data, int **key, int block_size);
void and_data(char *data, char *encrypted_data, int **key, int block_size);
int encrypt(char *data, char *encrypted_data, int **key)
{
int block_size = strlen(data);
int block_spacer = 0;
int i;
int j;
for (i = 0; i < 13; i++)
{
for (j = 0; j < 13; j++)
{
// Encrypts the data.
shift_data_left(data, encrypted_data, key, block_size);
and_data(encrypted_data, encrypted_data, key, block_size);
shift_data_right(encrypted_data, encrypted_data, key, block_size);
}
}
return 0;
}
int decrypt(char *data, char *decrypted_data, int **key)
{
int block_size = strlen(data);
int block_spacer = 0;
int i;
int j;
for (i = 0; i < 13; i++)
{
for (j = 0; j < 13; j++)
{
// Decrypts the data.
shift_data_right(data, decrypted_data, key, block_size);
and_data(decrypted_data, decrypted_data, key, block_size);
shift_data_left(decrypted_data, decrypted_data, key, block_size);
}
}
return 0;
}
void shift_data_left(char *data, char *encrypted_data, int **key, int block_size)
{
int i;
int j;
int data_spacer = 0;
for (data_spacer = 0; data_spacer < block_size; data_spacer++)
{
for (i = 0; i < 13; i++)
{
for (j = 0; j < 13; j++)
{
encrypted_data[data_spacer] = (int)data[data_spacer] << key[i][j];
}
}
}
}
void shift_data_right(char *data, char *encrypted_data, int **key, int block_size)
{
int i;
int j;
int data_spacer = 0;
for (data_spacer = 0; data_spacer < block_size; data_spacer++)
{
for (i = 0; i < 13; i++)
{
for (j = 0; j < 13; j++)
{
encrypted_data[data_spacer] = (int)data[data_spacer] << key[i][j];
}
}
}
}
void and_data(char *data, char *encrypted_data, int **key, int block_size)
{
int i;
int j;
int data_spacer = 0;
for (data_spacer = 0; data_spacer < block_size; data_spacer++)
{
for (i = 0; i < 13; i++)
{
for (j = 0; j < 13; j++)
{
encrypted_data[data_spacer] = (int)data[data_spacer] << key[i][j];
}
}
}
}
`Main.c
#include "KeyGeneration.h"
#include "EncryptDecrypt.h"
#include <stdio.h>
int main()
{
char *passphrase = "password";
int **key;
char *data = "Top Secret Message.";
char *encrypted_data = (char*)malloc(sizeof(char) * strlen(data));
key = (int**)malloc(sizeof(int) * 13);
for (int i = 0; i < 13; i++)
key[i] = (int*)malloc(sizeof(int) * 13);
generate_key(passphrase, key);
printf("Key:\n");
for (int i = 0; i < 13; i++)
{
for (int j = 0; j < 13; j++)
{
printf("%i ", key[i][j]);
}
}
printf("\n");
printf("Data: %s\n", data);
encrypt(data, encrypted_data, key);
printf("Encrypted data: %s\n", encrypted_data);
decrypt(encrypted_data, data, key);
printf("Decrypted data: %s\n", data);
for (int i = 0; i < 13; i++)
free(key[i]);
free(key);
free(encrypted_data);
getc(stdin);
return 0;
}
I also get an error at encrypted_data[data_spacer] = (int)data[data_spacer] << key[i][j];
something about access violation. This error only occurs when I call decrypt.