输入后程序变为空白终端(x或C加密)

时间:2015-10-09 05:40:33

标签: c encryption

这是一个具有一些支持功能的程序,可以进行简单的XOR加密/解密。

可能有很多混乱的语法 - 虽然它目前没有错误编译 - 我的问题是它没有通过程序,所以我无法真正测试它。

输入加密密钥后按Enter键,终端屏幕变为空白但程序未退出。

如果有人想要查看它,或者运行它并自己查看问题,我会留下下面的代码。这将非常有帮助 - 并提供信息,谢谢大家。

我的意见是代码在某些地方很难缩进 - 当我从编辑器复制和粘贴时格式不是很好,但我试图让它可读。

这是在Ubuntu shell中的VIM编辑器中用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));
}

1 个答案:

答案 0 :(得分:1)

您的问题是由错字引起的

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

您正在递增错误的变量。应该是

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

我建议您启用-Wall编译代码,您会发现有趣的内容,例如:

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++ ){
    ^

然而,您的代码存在很多错误:它无法执行所谓的操作。