百度空间 | 百度首页 
 
查看文章
 
xor encryption
2009-08-13 12:48
/**************************************************************
* Basic xor encryption program by spade89 :) *
* this code is for everyhuman no copyrights *
* feel free to modify or duplicate as much as you want. *
* *
***************************************************************
*/
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
int encrypt(char *,char *);
int decrypt(char *,char *);
long filesize(FILE *);
int main(){
begining:
unsigned char key[128],filename[64];
textcolor(LIGHTGREEN);
//startup:
cprintf("BASIC XOR ENCRYPTION BY SPADE %c") ;cout<<endl;
for(int i=0;i<80;i++)cprintf("%c",6);
cout<<endl;
// synchronize i/o
cin.tie(&cout);
cout<<"Enter filename:\n";
//get filename
cin>>filename;
cout<<"Enter key:\n";
//get encryption decryption key
cin>>key;

char op;
cout<<"Enter option<Encrypt/Decrypt>:\n";
//see what the user wants us to do
op=getch();

switch(op){

case 'd':
case 'D':
decrypt(filename,key);
break;
case 'e':
case 'E':
encrypt(filename,key);
default:
break;
}
op=getch();
//see of tje user still wants to continue, 0x1b is escape key
if(op==0x1b)goto end;
goto begining;
end:
return 0;
}
/*
function filesize
arguments FILE *stream
a file stream object
returns size of the file as long
*/
long filesize(FILE *stream)
{
long curpos, length;

curpos = ftell(stream);
fseek(stream, 0L, SEEK_END);
length = ftell(stream);
fseek(stream, curpos, SEEK_SET);
return length;

}
/*
function encrypt:
arguments char *fname,char *key
fname is filename to be encrypted,key is the encryption key
returns -1 if file fails to open, 0 if all goes well


*/
int encrypt(char *fname,char *key){

FILE *out;
unsigned char *buf;
//open file and check if it exists
if((out=fopen(fname,"rb+"))==NULL){textcolor(LIGHTRED);
cprintf("error opening file!!! :(");cout<<endl; return -1;}
// find the size of our file and convert the result to int
int size=(int) filesize(out);
//make sure we are at begining of file
fseek(out, 0L, SEEK_SET);
//make a buffer for file data
buf=new char[size];
//read all the data of the file into buf
for(int i=0;i<size;i++){
buf[i]=fgetc(out);



}
//close our file stream
fclose(out);
//remove our file from the system
remove(fname);
//make a new file using our oringinal file name
if((out=fopen(fname,"ab+"))==NULL){textcolor(LIGHTRED);
cprintf("error opening file!!! :(");cout<<endl; return -1;}

for(int i=0,j=0;i<size;i++,j++){
//when j equals length of the key make j=0
if(j==strlen(key)-1)j=0;
//xor each value of buf with each value of key
buf[i]=buf[i]^key[j];
//write the encrypted char's
fputc(buf[i],out);
}
textcolor(YELLOW);
cprintf("File encrypted :)");cout<<endl;
return 1;
}
/*
function decrypt:
arguments char *fname,char *key
fname is filename to be decrypted,key is the decryption key
returns -1 if file fails to open, 0 if all goes well
this function is basically a duplicate of encrypt

*/
int decrypt(char *fname,char *key){
FILE *out;
unsigned char *buf;
//open file and check if it exists
if((out=fopen(fname,"rb+"))==NULL){textcolor(LIGHTRED);cprintf("error opening file!!! :(");cout<<endl; return -1;}
// find the size of our file and convert the result to int
int size=(int) filesize(out);
//make sure we are at begining of file
fseek(out, 0L, SEEK_SET);
//make a buffer for file data
buf=new char[size];
//read all the data of the file into buf
for(int i=0;i<size;i++){
buf[i]=fgetc(out);



}
//close our file stream
fclose(out);
//remove our file from the system
remove(fname);
//make a new file using our oringinal file name
if((out=fopen(fname,"ab+"))==NULL){textcolor(LIGHTRED);
cprintf("error opening file!!! :(");
cout<<endl;
return -1;}

for(int i=0,j=0;i<size;i++,j++){
//when j equals length of the key make j=0
if(j==strlen(key)-1)j=0;
//xor each value of buf with each value of key
buf[i]=buf[i]^key[j];
//write the decrypted char's
fputc(buf[i],out);
}
textcolor(YELLOW);
cprintf("File decrypted :)",0x38); cout<<endl;
return 1;
}


类别:c语言实践 | 添加到搜藏 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu