?? fuctions.cpp
字號:
#include "menu.h"
#include "fuctions.h"
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include "conio.h"
#include "io.h"
using namespace std;
void tean(long *k, long *v, long N) {
long y=v[0], z=v[1];
long limit,sum=0;
if(N>0) { /* ENCRYPT */
limit=DELTA*N;
while(sum!=limit) {
y+=((z<<4)^(z>>5)) + (z^sum) + k[sum&3];
sum+=DELTA;
z+=((y<<4)^(y>>5)) + (y^sum) + k[(sum>>11)&3];
}
} else { /* DECRYPT */
sum=DELTA*(-N);
while(sum) {
z-=((y<<4)^(y>>5)) + (y^sum) + k[(sum>>11)&3];
sum-=DELTA;
y-=((z<<4)^(z>>5)) + (z^sum) + k[sum&3];
}
}
v[0]=y; v[1]=z;
}
void cl_enc_block(long *k, long *v) {
tean(k,v,ROUNDS);
}
void cl_dec_block(long *k, long *v) {
tean(k,v,-ROUNDS);
}
void keychange(long *k, const char* code)
{
for (int i = 0; i < 4; i++) {
k[i] = (long)code[i] * (long)code[i] * (long)code[i+1] * (long)code[i+1];
}
}
bool EnCode(const char* file, const char* code)
{
if (isEncrypted(file)) {
cout<<"[此文件已經加密!]"<<endl;
return false;
}
long k[4];
long v[2];
long t[2];
long* lt;
char newfile[FILE_NAME_MAX_LEN];
char fix[LEN];
streampos freallen, fpos;
long filelen;
int flag;
char ch;
char remnant[LEN];
char outPath[_MAX_PATH];
keychange(k, code);
char *fp = strrchr(file, '\\') + 1;
int i = 0;
while (*fp) {
newfile[i++] = *fp++;
}
outPath[i] = '\0';
changeName(newfile, czOutFix);
getFix(file, fix);
fstream finout(file, ios::in|ios::out|ios::binary);
if (!finout.is_open()) {
cout<<"[無法打開文件!]"<<endl;
finout.close();
return false;
}
//cout<<"[成功打開文件!]"<<endl;
cout<<"[按任意鍵選擇加密文件存放目錄!]";
getch();
outputPath(outPath);
_chdir(outPath);
system("CLS");
cout<<"[文件加密中...]"<<endl;
freallen = finout.seekg(0, ios_base::end).tellg();
filelen = long(freallen);
if (filelen % 8 == 0) flag = 0;
else {
flag = filelen%8;
filelen = (filelen/8)*8;
}
finout.seekg(0, ios_base::beg);
ofstream fout(newfile, ios::out | ios::binary);
if (fout.is_open()) {
while (finout.read((char*)v, 8)) {
cl_enc_block(k, v);
fout.write((char*)v, 8);
fpos = finout.tellg();
}
}
finout.clear();
if (flag) {
int s = 0;
finout.seekg(fpos, ios_base::beg);
while (finout.read((char*)&ch, 1)) {
remnant[s++] = ch;
}
remnant[s++] = 1;
while (s < 8) remnant[s++] = 0;
memcpy(t, remnant, 8);
lt = (long*)t;
cl_enc_block(k, lt);
fout.write((char*)lt, 8);
}
memcpy(t, code, 8);
lt = (long*)t;
cl_enc_block(k, lt);
fout.write((char*)lt, 8);
memcpy(t, fix, 8);
lt = (long*)t;
cl_enc_block(k, lt);
fout.write((char*)lt, 8);
v[0] = 0x0L; v[1] = (long)flag;
cl_enc_block(k, v);
fout.write((char*)v, 8);
finout.clear();
cout<<"[加密成功!]"<<endl;
finout.close();
fout.close();
return true;
}
bool DeCode(const char* file, const char* code)
{
long k[4];
long v[2];
streampos fpos, pos = 0;
char newfile[FILE_NAME_MAX_LEN];
char fix[LEN];
char key[LEN];
char remnant[LEN];
char* pkey;
char* pfix;
char* prem;
char ch;
int flag;
char outPath[_MAX_PATH];
keychange(k, code);
char *fp = strrchr(file, '\\') + 1;
int i = 0;
while (*fp) {
newfile[i++] = *fp++;
}
outPath[i] = '\0';
ifstream fin(file, ios::in | ios::binary);
if (!fin.is_open()) {
cout <<"[無法打開文件!]"<<endl;
fin.close();
return false;
}
fin.seekg(-24, ios_base::end);
fin.read((char*)v, 8);
cl_dec_block(k, v);
memcpy(key, v, 8);
pkey = (char*)key;
pkey[8] = '\0';
if (strcmp(code, pkey)) {
cout<<"[密碼不正確!]"<<endl;
fin.close();
return false;
}
fin.read((char*)v, 8);
cl_dec_block(k, v);
memcpy(fix, v, 8);
pfix = (char*)fix;
pfix[8] = '\0';
fin.read((char*)v, 8);
cl_dec_block(k, v);
flag = (int)v[1];
changeName(newfile, pfix);
cout<<"[密碼正確!]";
cout<<"[按任意鍵選擇解密文件存放目錄!]";
getch();
outputPath(outPath);
_chdir(outPath);
system("CLS");
cout<<"[文件解密中...]"<<endl;
fpos = fin.seekg(0, ios_base::end).tellg();
ofstream fout(newfile, ios::out | ios::binary);
fin.seekg(0, ios_base::beg);
while (fin.tellg() < fpos-streampos(24+(flag?8:0))) {
if (fin.read((char*)v, 8)) {
cl_dec_block(k, v);
fout.write((char*)v, 8);
}
}
fin.read((char*)v, 8);
cl_dec_block(k, v);
memcpy(remnant, v, 8);
prem = (char*)remnant;
for (int s = 0; s < flag; s++) {
fout.write((char*)&prem[s], 1);
}
fin.clear();
cout<<"[解密成功!]"<<endl;
fin.close();
fout.close();
return true;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -