简体   繁体   English

解码文件C按位运算

[英]Decode File C Bitwise Operations

I have a file encode.c that makes some bitwise operation to encode the text from a first .txt file. 我有一个文件encode.c,该文件进行一些按位操作以对第一个.txt文件中的文本进行编码。 It will put the encoded text in the .txt file2. 它将编码后的文本放入.txt文件2。 And there is a keyword to encode the .txt file... 还有一个关键字来编码.txt文件...

The ENCODE.c file is the following: ENCODE.c文件如下:

/* C language - UNIX style encryption program for ascii textfiles - (c) Eike Falk Anderson, 2013 */ / * 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 */

} }

#

I understand everything and I understand what the single bitwise operations do...but now i need to decrypt the file but i have to understand wich operations should I use to make the reverse of these operations: 我了解所有内容,也了解单个按位操作的功能...但是现在我需要解密文件,但我必须了解应该使用哪种操作来使这些操作相反:

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

I thought about this but I don't understand the other lines: 我考虑过这一点,但我不明白其他几行:

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

Firstly, you should have a look at Vigenere cipher. 首先,您应该看一下Vigenere密码。 It is very similar to what your encoding program does. 这与您的编码程序非常相似。

Imagine you have a byte stored in letter and this is its binary representation: letter = 0b0100 1000 . 假设您有一个字节存储在letter ,这是它的二进制表示形式: letter = 0b0100 1000

Now let's have a look what happens during encryption: 现在让我们看一下加密期间发生的情况:

                     // 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] is in this context a crypto key but for the example purposes lets assume argv[3][counter] = 1010 1010 argv[3]在本文中是一个加密密钥,但出于示例目的,我们假设argv[3][counter] = 1010 1010

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

When you want to decrypt the message you need to apply steps in different order. 当您想要解密消息时,您需要以不同的顺序应用步骤。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM