gpt4 book ai didi

c - 解码文件 C 按位​​运算

转载 作者:太空宇宙 更新时间:2023-11-04 06:17:18 26 4
gpt4 key购买 nike

我有一个文件 encode.c,它进行一些按位操作来对第一个 .txt 文件中的文本进行编码。它会将编码后的文本放入 .txt 文件 2。并且有一个关键字来编码 .txt 文件...

ENCODE.c 文件如下:

/* C 语言 - ascii 文本文件的 UNIX 风格加密程序 - (c) Eike Falk Anderson, 2013*/

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

#define PUSAGE fprintf(stderr,"USAGE: %s [source file] [destination file] [key]\n\n",argv[0]) /* predefined error/usage message */


/* compile with clang encode.c -o encode

when you run this program it takes three inputs/parameters:
1. the name of your sourse text file (the ASCII text file you want to encode)
2. the name of your destination text file (the file that will have the encoded text in it)
3. the keyword (the "password" for the encoded file)

example:
./encode source.txt dest.txt secret

*/


int main(int argc,char *argv[]) /* argument vector 0 - program-name, argv 1 - source string, argv 2 - dest string, argv 3 - key string */
{
int counter; /* a counter variable for counting through the characters of the password */
char letter,lo,hi,rev,inv,enc; /* variables used for the letters of the text during the coding process */
FILE *sfp=NULL; /* source file pointer */
FILE *dfp=NULL; /* destination file pointer */

if((sfp=fopen(argv[1],"r"))==NULL) /* open the text file (sfp) named in argument vector 1 for reading - print error message if this operation fails */
{
fprintf(stderr,"UNABLE TO OPEN FILE\n");
PUSAGE;
return 1; /* end program with error */
}
if((dfp=fopen(argv[2],"w"))==NULL) /* open the text file (dfp) named in argument vector 2 for writing - print error message if this operation fails */
{
fprintf(stderr,"UNABLE TO OPEN FILE\n");
PUSAGE;
return 1; /* end program with error */
}

counter=0; /* set the keyword character counter to 0 */
while((letter=fgetc(sfp))!=EOF) /* process the source file by reading in the next letter (as 1 character/byte each) until the file ends, i.e. the EOF (end of file) character is read in */
{
if(counter==strlen(argv[3])) counter=0; /* reset if the keyword overflows, i.e. if its end (string length) is reached */

/* do something to the read in characters/bytes */
lo=letter>>4;
hi=letter<<4;
rev=hi|lo;
inv=~rev;
enc=inv^argv[3][counter];

counter++; /* increase the counter, i.e. step to the next character of the keyword */

fputc(enc,dfp); /* write the encoded character to the destination file */
}

fclose(sfp); /* close file sfp */
fclose(dfp); /* close file dfp */

return 0; /* end with success */

#

我明白一切,我明白单位运算的作用......但现在我需要解密文件,但我必须明白我应该使用哪些操作来反转这些操作:

    lo=letter>>4;
hi=letter<<4;
rev=hi|lo;
inv=~rev;
enc=inv^argv[3][counter];
#

我想到了这一点,但我不明白其他几行:

lo=letter<<4;
hi=letter>>4;
rev=??;
inv=~rev;
enc=??;

最佳答案

首先,您应该了解维吉尼亚密码。它与您的编码程序所做的非常相似。

假设您在 letter 中存储了一个字节,这是它的二进制表示形式:letter = 0b0100 1000

现在让我们看看加密过程中发生了什么:

                     // letter = 0100 1000
lo = letter >> 4; // lo = 0000 0100
hi = letter << 4; // hi = 1000 0000
rev = hi | lo; // rev = 1000 0100
inv = ~rev; // inv = 0111 1011

argv[3] 在此上下文中是一个加密 key ,但出于示例目的,我们假设 argv[3][counter] = 1010 1010

enc = inv ^ argv[3][counter]; // enc = 1101 0001

当您想要解密消息时,您需要按不同的顺序应用步骤。

dec = enc ^ argv[3][counter]; // dec = 0111 1011
dec = ~dec // dec = 1000 0100
hi = dec << 4; // hi = 0100 0000
lo = dec >> 4; // lo = 0000 1000
dec = hi | lo; // dec = 0100 1000

关于c - 解码文件 C 按位​​运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42836495/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com