WolfKach
WolfKach

Reputation: 11

Program turns to blank terminal after inputs (xor encrypt in C)

This is a program with some supporting functions to make a simple XOR encryption/decryption.

There is probably a lot of messed up syntax - though it compiles currently without error - my issue is that it doesn't make it through the program, so I cannot really test it well.

After pressing enter upon entering the encryption key, the terminal screen goes blank but the program does not quit.

I'll leave the code below if anyone wants to try to look through it, or run it and see the problem for themselves. It would be very helpful - and informative, thank you all.

My appologies that the code is poorly indented in some places - the formatting wasn't great when I copy and pasted from my editor, but I tried to make it readable.

This was coded in VIM editor in Ubuntu shell, in C. (v14.3).

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

#define MAX_BUF  256

unsigned char getBit(unsigned char, int);
unsigned char setBit(unsigned char, int);
unsigned char clearBit(unsigned char, int);
unsigned char skipx(unsigned char, unsigned char);
unsigned char rotr(unsigned char);


int main()
{

    char str[8];
    unsigned  char c[MAX_BUF];
    unsigned  char key[MAX_BUF];

    int  choice;

    printf("\nYou may:\n");
    printf("  (1) Encrypt a message \n");
    printf("  (2) Decrypt a message \n");

    printf("\n  what is your selection: ");
    fgets(str, sizeof(str), stdin);
    sscanf(str, "%d", &choice);

    switch (choice)
    {
        case 1:
            printf("\nEnter plain text to be encrypted: ");
            fgets(c, sizeof(c),stdin);
            printf("Press enter to continue.");
            scanf("%c",c);

            //User interface: Ask user for encryption key
            printf("\nNow enter your encryption  key: ");
            fgets(key, sizeof(key),stdin);
            printf("Press enter to continue.");
            scanf("%c",key);

            int inputLen = strlen(c);

            int pos;

            for(pos = 0; pos < inputLen; pos++ )
            {
                skipx(c[pos], *key);
            }

            int pos2;

            for(pos2 = 0; pos2 < inputLen; pos++)
            {
                printf("%c", c[pos2]);
            }
            break;

        case 2://Decrypt message
            printf("\nEnter plain text to be decrypted: ");
            fgets(c, sizeof(c),stdin);
            printf("Press enter to continue.");
            scanf("%c",c);

            printf("\nNow enter your decryption  key: ");
            fgets(key, sizeof(key),stdin);
            printf("Press enter to continue.");
            scanf("%c",key);

            for(pos = 0; pos < inputLen; pos++ ){
            skipx(c[pos], *key);
            }

            //Prints the char array, after being XOR encrypted
            for(pos2 = 0; pos2 < inputLen; pos++){

            printf("%c", c[pos2]);

            }
            break;

        default:
            break;
    }

    return 0;
}

unsigned char skipx(unsigned char c, unsigned char key)
{
    int i; //for looping through the 8 positions of the bit

    for(i = 7; i > -1; i--) //Loops through byte, this will be the bit index
    {
        if(getBit(c,i) % 2 != 0) //only if the number isn't even. ->
        {
            setBit(getBit(c,i)^getBit(key,i-1),i);

            rotr(key);//rotate right to update key
        }
    }
}

unsigned char rotr(unsigned char c)
{
  unsigned char c1 = getBit(c,0) << 7;
  unsigned char c2 = c >> 1;

  return c1^c2;
}


unsigned char getBit(unsigned char c, int n)
{
  return (c & (1 << n)) >> n;
}


unsigned char setBit(unsigned char c, int n)
{
  return c | (1 << n);
}


unsigned char clearBit(unsigned char c, int n)
{
  return c & (~(1 << n));
}

Upvotes: 0

Views: 112

Answers (1)

LPs
LPs

Reputation: 16233

Your issue is caused by a typo

for(pos2 = 0; pos2 < inputLen; pos++)

You are incrementing the wrong variable. Should be

for(pos2 = 0; pos2 < inputLen; pos2++)

I suggest you to enable -Wall compiling your code, You'll find interesting things, like:

test.c: In function ‘main’:
test.c:125:4: warning: ‘inputLen’ may be used uninitialized in this function [-Wmaybe-uninitialized]
    for(pos = 0; pos < inputLen; pos++ ){
    ^

However your code has a lot of errors: it can't do what is supposted to do.

Upvotes: 1

Related Questions