1

Over the last few weeks I've been meddling with encryption in C. I've been using a simple substitution cipher but I've encountered problems with the following code. Though the program runs smoothly, the contents of the text file "Message" always change to the same piece of text : C=Øžû†. I was hoping to change every character of the string in the file to a random letter.

#define _CRT_SECURE_NO_WARNINGS
#include  <stdio.h>
#include  <stdlib.h>
#include  <Windows.h>
#include  <type.h>
#include <string.h>

const int MAXSIZE = 50;

void Encrypt(FILE *File, char file[MAXSIZE], int i, int j)
{
    File = fopen("message.txt", "r+");
    for (i = 0; i < 6; i++)
    {
        file[i] = rand() + 26;
        fputc(file[i], File);
    }

    printf("%s", file);

    fclose(File);
    return;
}

int main()
{
    int i = 0;
    int j = 0;
    char file[MAXSIZE];
    FILE *File = 0;

    Encrypt(File, file, i, j);

    system("pause");
    return 0;
}
Mathieu
  • 8,840
  • 7
  • 32
  • 45
  • 2
    Need [srand](http://en.cppreference.com/w/c/numeric/random/srand) ? – BLUEPIXY Jul 25 '17 at 10:53
  • 1
    There are a few things you need to know: The first is that you don't have to use arguments for variables that should be local inside a function. The second thing is that without [*seeding*](http://en.cppreference.com/w/c/numeric/random/srand) You will always get the same "random" numbers. The third is that you don't substitute anything in the file, you simply *overwrite*, unconditionally, the contents of the file. Lastly, using "random" numbers makes it *impossible* to decrypt the file. How would you know what value to subtract to get back the original character? – Some programmer dude Jul 25 '17 at 10:53
  • 1
    All in all, I think you need to take a few steps back, [get a couple of good beginners books](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and start over. – Some programmer dude Jul 25 '17 at 10:53
  • 1
    Oh one last thing, [ASCII](http://en.cppreference.com/w/c/language/ascii) (the most common character encoding scheme) is using only seven bits, which means the max value is 127. The [`rand`](http://en.cppreference.com/w/c/numeric/random/rand) function returns a number between `0` and [`RAND_MAX`](http://en.cppreference.com/w/c/numeric/random/RAND_MAX) (guaranteed to be at least `32767`). That range is *way* out of bounds for a valid ASCII character. What character would e.g. the *integer* value `5623` represent? – Some programmer dude Jul 25 '17 at 11:00

1 Answers1

0

There are quite a few problem with the code shared, they are:

  1. How will you decrypt the encrypted text if you are using rand()/srand() functions.
  2. file[i] = rand() + 26; Here you are just dumping any character value and than dumping into the file, which doesn't make any sense, you need to encrypt some text.

I am attaching a simple sample code with Ceaser cipher[type of substitution ciper] for your reference. whose output will be as follow:

testtest 123123 TESTTEST
tbwxatzd 164904 AOWPTBWX
testtest 123123 TESTTEST

#include <stdio.h>
#include <string.h>

static char key[] = "helloworld";

int Encrypt(char *aText,char *aKey)
{
    int lTextlen    = strlen(aText);
    int lKeylen     = strlen(aKey);

    int lCount,lShift;
    int lCount1 = 0;

    for(lCount = 0; lCount < lTextlen;lCount++)
    {
        if(aText[lCount] >= 'a' && aText[lCount] <= 'z')
        {
            lShift = aKey[lCount1] % 26;
            aText[lCount] = ((aText[lCount] + lShift - 97) % 26) + 97;
        }
        else if(aText[lCount] >= 'A' && aText[lCount] <= 'Z')
        {
            lShift = aKey[lCount1] % 26;
            aText[lCount] = ((aText[lCount] + lShift - 65) % 26) + 65;
        }
        else if(aText[lCount] >= '0' && aText[lCount] <= '9')
        {
            lShift = aKey[lCount1] % 10;
            aText[lCount] = ((aText[lCount] + lShift - 48) % 10) + 48;
        }
        else
        {
        }
        lCount1 = (lCount1 + 1) % lKeylen;
    }
}

int Decrypt(char *aText,char *aKey)
{
    int lTextlen    = strlen(aText);
    int lKeylen     = strlen(aKey);

    int lCount,lShift;
    int lCount1 = 0;

    for(lCount = 0; lCount < lTextlen;lCount++)
    {
        if(aText[lCount] >= 'a' && aText[lCount] <= 'z')
        {
            lShift = 26 - (aKey[lCount1] % 26);
            aText[lCount] = ((aText[lCount] + lShift - 97) % 26) + 97;
        }
        else if(aText[lCount] >= 'A' && aText[lCount] <= 'Z')
        {
            lShift = 26 - (aKey[lCount1] % 26);
            aText[lCount] = ((aText[lCount] + lShift - 65) % 26) + 65;
        }
        else if(aText[lCount] >= '0' && aText[lCount] <= '9')
        {
            lShift = 10 - (aKey[lCount1] % 10);
            aText[lCount] = ((aText[lCount] + lShift - 48) % 10) + 48;
        }
        else
        {
        }
        lCount1 = (lCount1 + 1) % lKeylen;
    }
}
int main()
{
    char plaintext[] = "testtest 123123 TESTTEST";
    printf("%s\n",plaintext);
    Encrypt(plaintext,key);
    printf("%s\n",plaintext);
    Decrypt(plaintext,key);
    printf("%s\n",plaintext);
    return 0;
}
Rajat Kothari
  • 300
  • 1
  • 2
  • 12