?? wencrypt.h
字號:
/***********************************************************
* *
* WEncrypt class for file encryption *
* *
* ### Wael Tahon ### *
* *
***********************************************************
* usage: *
********* *
* *
* WEncryption *obj; *
* obj = new WEncryption("srcfile","newfile","password"); *
* obj->EncryptFile(); *
* *
* Or *
* *
* obj->DecryptFile(); *
* delete obj; *
* *
***********************************************************
* Note:- *
********* *
* *
* Do not try to edit any encrypted files. *
* *
* if(you_lose_one_byte) *
* your_file = destroyed; *
* *
***********************************************************
* *
* Please send me your remarks about this class. *
* *
* E-Mail: wael_tahon@yahoo.com *
* Web page: wael_tahon.tripod.com *
* Phone: +2064323402 *
* *
***********************************************************/
#ifndef _WENCRYPT_H_
#define _WENCRYPT_H_
/*************************************************/
/* Return values */
/*************************************************/
#define NO_ERRORS_DONE 0 //No errors.
#define ERROR_SRC_FILE 1 //Can't open the source file.
#define ERROR_DST_FILE 2 //Can't create the new file.
#define ERROR_CANT_ENC 3 //Can't encrypt the file (must be normal file).
#define ERROR_CANT_DEC 4 //Can't decrypt the file (must be encrypted file).
/*************************************************/
#define ENCRYPTED_FILE 5 //The file is an encrypted file.
#define NORMAL_FILE 6 //The file is a normal file.
/*************************************************/
#include<stdio.h>
#include<string.h>
/*************************************************/
class WEncryption{ //Declaring the class.
/***********/
private: //Private members.
/***********/
char *SrcFile; //The source file name.
char *OutFile; //The new file name.
char password[64]; //The encryption key 64 bytes max.
/*************************************************/
/* Check if encrypted */
/*************************************************/
int CheckIfEncrypted(char *file)
{
int m[19];
FILE *h = fopen(file, "rb");
for(int d=0; d<19; d++)
m[d] = fgetc(h);
fclose(h);
if(m[0]==3&&m[1]==2&&m[2]==3&&m[3]==4&&m[4]==0&&m[5]==2&&
m[6]==3&&m[7]==2&&m[8]==3&&m[9]==4&&m[10]==0&&m[11]==2&&
m[12]==3&&m[13]==5&&m[14]==7&&m[15]==0&&m[16]==9&&m[17]==5&&m[18]==0)
return ENCRYPTED_FILE;
else
return NORMAL_FILE;
};
/*************************************************/
/* Mark the file by 19 bytes mark */
/*************************************************/
int MarkTheFile(FILE *f)
{
int m[]={3,2,3,4,0,2,3,2,3,4,0,2,3,5,7,0,9,5,0}; // My phone numbers !!
for(int d=0; d<19; d++)
fputc(m[d], f);
return 0;
};
/*************************************************/
/* Get the file size */
/*************************************************/
int GetFileSize(char *ff)
{
FILE *z = fopen(ff, "r");
long int cur;
fseek(z, 0L, SEEK_END);
cur = ftell(z);
fclose(z);
FileSize = cur;
return 0;
};
/*************************************************/
/**********/
public: //Public members.
/**********/
long int FileSize; //The file size.
long int CurPosition; //The current position inside the file.
/*************************************************/
/* Constructor */
/*************************************************/
WEncryption(char *src,char *dst,char pass[64])
{
FileSize=CurPosition=0;
SrcFile=src;
OutFile=dst;
strcpy(password,pass);
};
/*************************************************/
/* Destructor */
/*************************************************/
~WEncryption()
{
};
/*************************************************/
/* File encryption */
/*************************************************/
int EncryptFile()
{
FILE *r, *w;
int blah=0;
int g=0;
unsigned int ch;
GetFileSize(SrcFile);
if(CheckIfEncrypted(SrcFile) != NORMAL_FILE)
{
return ERROR_CANT_ENC;
}
if((r = fopen(SrcFile, "rb")) == NULL)
{
return ERROR_SRC_FILE;
}
if((w = fopen(OutFile, "wb")) == NULL)
{
return ERROR_DST_FILE;
}
MarkTheFile(w); // Mark the file.
CurPosition=0;
/***************************************/
/* Encryption algorithm */
/***************************************/
for(long int v=0;;v++)
{
for(g=0; g<64; g++)
{
CurPosition++;
if((ch=fgetc(r))==EOF)
{
blah = 1;
break;
}
if((ch%2)==0)
{
ch+=1;
}
else
{
ch+=3;
}
ch =(~ch^(int)password[g])^(v^~g);
fputc(ch, w);
}
if(blah == 1)
{
break;
}
}
/***************************************/
fclose(r);
fclose(w);
return NO_ERRORS_DONE;
};
/*************************************************/
/* File decryption */
/*************************************************/
int DecryptFile()
{
FILE *r, *w;
int blah = 0;
int g = 0;
unsigned int ch;
GetFileSize(SrcFile);
if( CheckIfEncrypted(SrcFile) != ENCRYPTED_FILE )
{
return ERROR_CANT_DEC;
}
if((r = fopen( SrcFile, "rb")) == NULL)
{
return ERROR_SRC_FILE;
}
if((w = fopen( OutFile, "wb")) == NULL)
{
return ERROR_DST_FILE;
}
char *zzz = "";
fread(zzz, 19, 1, r); // Skip 19 bytes from the file.
CurPosition=0;
/***************************************/
/* Decryption algorithm */
/***************************************/
for(long int v=0;;v++)
{
for(g=0; g<64; g++)
{
CurPosition++;
if((ch = fgetc(r)) == EOF)
{
blah = 1;
break;
}
ch = ~((ch^(v^~g))^(int)password[g]);
if((ch%2)==0)
{
ch-=3;
}
else
{
ch-=1;
}
fputc(ch, w);
}
if(blah == 1)
{
break;
}
}
/***************************************/
fclose(r);
fclose(w);
return NO_ERRORS_DONE;
};
/*************************************************/
};
#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -