如何使用加密逐位XOR一个文本文件? [英] How to encrypt a text file using bit-wise XOR?

查看:223
本文介绍了如何使用加密逐位XOR一个文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过从另一个文件(keys.txt)上有两个特定按键的左边和右边的字符逐位XOR运算加密从文本文件的消息,但我收到不可读code在原来的文本文件的前面(没有什么变化),这是不对的。我使用两个文本文件:

1)INPUT.TXT - 包含要被加密的消息

2)Keys.txt - 这包含两个字符做XOR运算每个字符在input.txt中(字符1键1和2的性格是关键2)

以下code在我的计划:

 海峡[I] = STR [I] ^ STR2 [2];
STR [++ i] = STR [I] ^ STR2 [1];
打破;

是假设要进行XOR运算code行

注意的我所需要的输出应类似于此:


  

米@#EMI(> 9S(@)H##FMN XGmmmmU,H!Gmr(DmI\"VmD,F(S!XmU%DmM\"C>U(S>,O)9I(9T?U!D>,M!,E;@#B(Gmu%D4,S(:@$U$O*\"OmU%DmR%H#F!D`V$M!4N8.N DM @#EMKH#9I(+ MMMM)@#B(F


有人能澄清,我遇到的问题?

用户需要输入:

  GCC myProgram.c
./a.outËinput.txt的keys.txt

(电子只是站在加密)

 #包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
INT主(整形变量,字符*的argc []){INT I;
INT LEN = 0;
焦炭海峡[1024];
焦炭STR2 [2];
FILE * finp;
FILE *密钥文件;如果(STRCMP(ARGC [1],E)== 0)
{
  如果((finp = FOPEN(ARGC [2],R))== NULL)
  {
    的printf(无法打开文件%s \\ n,ARGC [2]);
    出口(1);
  }  如果((KEYFILE = FOPEN(ARGC [3],R))== NULL)
  {
    的printf(无法打开文件%s \\ n,ARGC [3]);
    出口(1);
  }
  而((与fgets(STR,1024,finp)= NULL)及!(与fgets(str2,2,密钥文件)= NULL))
  {
    的printf(%C \\ N%C,STR2 [1],STR2 [2]);
    / * *** START code使用INPUT.TXT档案与KEYS.TXT *** * /
    LEN = strlen的(STR);
    对于(i = 0; I< LEN,我++)
    {
      海峡[I] = STR [I] ^ STR2 [2];
      STR [++ i] = STR [I] ^ STR2 [1];
      打破;
    }
  }  的printf(%S \\ n,STR);
  FCLOSE(finp);
  返回0;}
其他
{
  的printf(对不起!);
}


解决方案

C数组索引是从零开始的,那么就应该使用

  STR2 [0],STR2 [1]

而不是

  STR2 [1],STR2 [2]

在此片段

 为(i = 0; I< LEN,我++)
{
  海峡[I] = STR [I] ^ STR2 [2];
  STR [++ i] = STR [I] ^ STR2 [1];
  打破;
}

第一个循环之后break语句停止循环。你应该将其删除。然后你得到

 为(i = 0; I< LEN,我++)
{
  海峡[I] ^ = STR2 [1];
  STR [++ i] = ^ STR2 [0];
}

在行

 ,而((与fgets(STR,1024,finp)= NULL)及!(与fgets(str2,2,密钥文件)= NULL))

您需要逻辑和按位而不是

 ,而((与fgets(STR,1024,finp)= NULL)及!&安培;!(与fgets(str2,2,密钥文件)= NULL))

如果你的input.txt的文件中含有较多的1024字节,显示你需要移动printf的所有结果(%S \\ n,STR);进入while循环

 ,而((与fgets(STR,1024 finp)= NULL)及!&安培;!(与fgets(str2,2,密钥文件)= NULL))
{
    ...
    的printf(%S \\ n,STR);
}

I am trying to encrypt a message from a text file by using the bit-wise XOR operation on the left and right characters with two specific keys from another file(keys.txt), but I am receiving unreadable code in front of the original text file(nothing changed), which is not right. I am using two text files:

1) Input.txt - containing the message that is to be encrypted

2) Keys.txt - This contains two characters that do the XOR operation to each character in the input.txt (character 1 is key 1 and character 2 is key 2)

The following code in my program:

str[i]=str[i]^str2[2];
str[++i]=str[i]^str2[1];
break;

is the line of code that is suppose to be performing the XOR operation

Note My desired output should look similar to this:

m@#EmI(>9S(@)H#FmN# XGmmmmU,H!Gmr(DmI"VmD,F(S!XmU%DmM"C>U(S>,O)9I(9T?U!D>,M!,E;@#B(Gmu%D4,S(:@$U$O*"OmU%DmR%H#F!D`V$M!4N8.N Dm@#EmK"H#9I(+mmmm)@#B(f

Can someone clarify the issue that I am running into?

The user should enter:

gcc myProgram.c
./a.out e input.txt keys.txt

(The e just stands for encryption)

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


int main(int args, char *argc[]){

int i;
int len=0;
char str[1024];
char str2[2];
FILE *finp;
FILE *keyFile;

if ( strcmp(argc[1], "e") == 0 )
{
  if ( (finp = fopen(argc[2],"r")) == NULL )
  {
    printf("Could Not Open file %s\n", argc[2]);
    exit(1);
  }

  if ( (keyFile = fopen(argc[3],"r")) == NULL )
  {
    printf("Could Not Open file %s\n", argc[3]);
    exit(1);
  }


  while((fgets(str,1024,finp)!=NULL)&(fgets(str2,2,keyFile)!=NULL))
  {
    printf("%c\n %c",str2[1],str2[2]);


    /* *** START CODE THAT USES INPUT.TXT FILE & KEYS.TXT *** */
    len = strlen(str);
    for(i=0;i<len;i++)
    {
      str[i]=str[i]^str2[2];
      str[++i]=str[i]^str2[1];
      break;
    }
  }

  printf("%s\n", str);
  fclose(finp);
  return 0;

}
else
{
  printf("SORRY!");
}

解决方案

C arrays indexes are zero based, then you should use

str2[0],str2[1]

instead of

str2[1],str2[2]

In this fragment

for(i=0;i<len;i++)
{
  str[i]=str[i]^str2[2];
  str[++i]=str[i]^str2[1];
  break;
}

break statement stop loop after first iteration. You should remove it. Then you get

for(i=0;i<len;i++)
{
  str[i]^=str2[1];
  str[++i]^=str2[0];
}

In line

while((fgets(str,1024,finp)!=NULL)&(fgets(str2,2,keyFile)!=NULL))

you need logical AND instead of bitwise

while((fgets(str,1024,finp)!=NULL)&&(fgets(str2,2,keyFile)!=NULL))

And if your input.txt file contain more the 1024 byte to show all results you need to move printf("%s\n", str); into the while loop

while((fgets(str,1024,finp)!=NULL)&&(fgets(str2,2,keyFile)!=NULL))
{
    ...
    printf("%s\n", str);
}

这篇关于如何使用加密逐位XOR一个文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆