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.

Recommended Answers

All 6 Replies

You cannot write to a string literal. When you do char* data = "Top Secret Message.";, the pointer data points to a section of read-only data. You cannot write to it, and that's why you get a access violation (or segmentation fault) in the decrypt function when you try to write to it. In reality, that statement should be const char* data = "Top Secret Message.";, and you should use a different (writable) buffer for storing the result of the decryption.

I'm not sure about the other problem you mentioned. You must first solve this access violation issue first.

Thanks. I solved the access violation issue. Well my encrypted_data variable gets the value Top Secret Message. instead of encrypted data. So somewhere something is wrong with this variable assignment.

Oh also mike_2000_17 (or any moderator), can you just please edit my post and change the char *password to something like "password" please. I forgot to remove my personal password from the code before posting it. Thanks.

I changed the password... be careful in the future with that kind of stuff. You can't depend on others to protect your personal information.

Yes thanks I know. A little big oops there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.