• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C++使用key文件进行xor加解密

OC/C/C++ 水墨上仙 2511次浏览

C++使用key文件进行xor加解密

/*
    The function (en/de)crypts an input data file with an input key
    file using the XOR method.
 
    The use of a key file instead of a plain text key makes it much
    more flexible and secure. If the key is longer than the encrypted data,
    truly random (btw, compressed files like mp3 or jpg provide pretty
    good "almost-like-random" characters) and used only once, even though
    the metod is so simple, it is unbreakable even in theory.
*/
 
//libraries to include
#include <stdio.h> //for file handling and other IO
#include <stdlib.h> //for the exit function
 
//macros
#define BUF 512 //input filename buffer size
#define CNK 256 //IO buffer size
#define FILCHK(x) if(!x){printf("Cannot open file %s\n", x); exit(EXIT_FAILURE);} //file pointer checker
 
//xor (en/de)coding function
void xorenc(char *inname, char *outname, char *keyname)
{
    FILE *infile, *outfile, *keyfile; //file pointers
    char datachunk[CNK], keychunk[CNK]; //input data & key buffers
    long datapos, keypos, datalen, keylen, keymax; //data lengths
     
    //open files
    infile = fopen(inname, "rb"); //binary mode (b) is crucial
    FILCHK(infile); //check the file pointer
    outfile = fopen(outname, "wb");
    FILCHK(outfile);
    keyfile = fopen(keyname, "rb");
    FILCHK(keyfile);
     
    //get key length
    fseek(keyfile, 0L, SEEK_END); //seek the end of file
    keymax = ftell(keyfile); //tell what its position is
    rewind(keyfile); //go back to the beginning of the key file
     
    //get the first key chunk
    keypos = 0; //initialize the key buffer position
    keylen = fread(keychunk, 1, CNK, keyfile); //binary read a chunk of the key
     
    //an infinite loop
    while(1)
    {
        //binary read a chunk of data
        datalen = fread(datachunk, 1, CNK, infile);
         
        //for every char in the input buffer
        for(datapos = 0; datapos < datalen; ++datapos)
        {
            datachunk[datapos] ^= keychunk[keypos++]; //xor a data char with a key char
             
            //if the whole key chunk was used
            if(keypos == CNK)
            {
                keylen += fread(keychunk, 1, CNK, keyfile); //read a new key chunk
                keypos = 0; //set the key buffer position to 0
            }
             
            //otherwise, if the whole key file was used
            else if(keypos == keymax || keypos == keylen)
            {
                rewind(keyfile); //rewind it
                keylen = fread(keychunk, 1, CNK, keyfile); //read a key chunk
                keypos = 0; //set the key buffer position to 0
            }
        }
         
        //binary write the (en/de)crypted data
        fwrite(datachunk, 1, datalen, outfile);
         
        //if the whole data file was processed
        if(datalen < CNK)
            break; //end the loop
         
        //otherwise, if the whole key file was processed
        else if(keylen == keymax)
        {
            rewind(keyfile); //rewind it
            keylen = fread(keychunk, 1, CNK, keyfile); //read a key chunk
            keypos = 0; //set the key buffer position to 0
        }
    }
     
    //close the files
    fclose(infile);
    fclose(outfile);
    fclose(keyfile);
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C++使用key文件进行xor加解密
喜欢 (0)
加载中……