c#文件加密程序
来源:互联网 发布:seo公司哪家专业 编辑:程序博客网 时间:2024/05/17 01:41
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 1024
#define Success 1
#define Failed 0
#define LEN 300
typedef int state;
typedef struct
{
char dirPath[LEN];
char fileName[LEN];
char new_file[LEN];
} fileInfo;
int count = 0;
//获取父目录、文件名
void getInfo(char *str, fileInfo ** info, int option)
{
fileInfo * INFO = (fileInfo *)malloc(sizeof(fileInfo));
memset(INFO, 0, sizeof(*INFO));
int i;
for(i=strlen(str); i>=0; i--)
{
if( str[i] == '\\')
{
strncpy(INFO->dirPath, str, i+1);
strcpy(INFO->fileName, str+i+1);
break;
}
}
(option)?( sprintf(INFO->new_file, "%sybk-cyp_%s",INFO->dirPath,INFO->fileName))
:( sprintf(INFO->new_file, "%sybk-dcy_%s",INFO->dirPath,INFO->fileName));
*info = INFO;
}
//加密
state cyp(char *str, fileInfo * info)
{
FILE *stream_org;
FILE *stream_new;
char buf[N] = {0};
if (((stream_org = fopen(str, "rb")) == NULL)
|| ((stream_new = fopen(info->new_file, "wb")) == NULL))
{
printf("Cannot read or write file...\n");
puts(str);
return Failed;
}
fseek(stream_org, 0, SEEK_END);
int len = ftell(stream_org);
printf("%d\n",len);
fseek(stream_org, 0, SEEK_SET);
int record = - 1;
while(len > 0)
{
record = fread(buf, 1,sizeof(buf),stream_org);
// if(record == 0)
// {
// if(strlen(buf) < N)
// {
// // count += strlen(buf);
// fwrite(buf, strlen(buf), 1, stream_new);
// // printf("pos1 count: %d\n",count);
// fflush(stream_new);
// }
// break;
// }
if(record >0)
{
int i;
for(i=0; i<record; i++)
{
buf[i] ^= 0x88;
}
//count += sizeof(buf);
fwrite(buf, 1, strlen(buf), stream_new);
//printf("pos2 count: %d\n",count);
fflush(stream_new);
memset(buf, 0, sizeof(buf));
len -= record;
printf("%d\n",len);
}
else if(record ==0)
{
break;
}
else
{
printf("fread() failed.\n");
return Failed;
}
}
printf("%d\n",len);
fclose(stream_new);
fclose(stream_org);
return Success;
}
int main(int argc, char *argv[])
{
if(argc < 3)
{
printf("Usage:%s [-c|-d] filename\n",argv[0]);
return 1;
}
else
{
char str[LEN] = {0};
strcpy(str, argv[2]);
puts(str);
fileInfo *info;
switch(argv[1][1])
{
case 'c':
{
getInfo(str, &info, 1);
if ( cyp(argv[2], info) == Success )
{
printf("Encrypted Successfully!\n");
return 0;
}
else
{
printf("Encrypted Failed!\n");
return 0;
}
}
break;
case 'd':
{
getInfo(str, &info, 0);
if ( cyp(argv[2], info) == Success )
{
printf("Decrypted Successfully!\n");
return 0;
}
else
{
printf("Decrypted Failed!\n");
return 0;
}
}
break;
default:
printf("Unknow Args!\n");
return 0;
}
}
}
#include <stdlib.h>
#include <string.h>
#define N 1024
#define Success 1
#define Failed 0
#define LEN 300
typedef int state;
typedef struct
{
char dirPath[LEN];
char fileName[LEN];
char new_file[LEN];
} fileInfo;
int count = 0;
//获取父目录、文件名
void getInfo(char *str, fileInfo ** info, int option)
{
fileInfo * INFO = (fileInfo *)malloc(sizeof(fileInfo));
memset(INFO, 0, sizeof(*INFO));
int i;
for(i=strlen(str); i>=0; i--)
{
if( str[i] == '\\')
{
strncpy(INFO->dirPath, str, i+1);
strcpy(INFO->fileName, str+i+1);
break;
}
}
(option)?( sprintf(INFO->new_file, "%sybk-cyp_%s",INFO->dirPath,INFO->fileName))
:( sprintf(INFO->new_file, "%sybk-dcy_%s",INFO->dirPath,INFO->fileName));
*info = INFO;
}
//加密
state cyp(char *str, fileInfo * info)
{
FILE *stream_org;
FILE *stream_new;
char buf[N] = {0};
if (((stream_org = fopen(str, "rb")) == NULL)
|| ((stream_new = fopen(info->new_file, "wb")) == NULL))
{
printf("Cannot read or write file...\n");
puts(str);
return Failed;
}
fseek(stream_org, 0, SEEK_END);
int len = ftell(stream_org);
printf("%d\n",len);
fseek(stream_org, 0, SEEK_SET);
int record = - 1;
while(len > 0)
{
record = fread(buf, 1,sizeof(buf),stream_org);
// if(record == 0)
// {
// if(strlen(buf) < N)
// {
// // count += strlen(buf);
// fwrite(buf, strlen(buf), 1, stream_new);
// // printf("pos1 count: %d\n",count);
// fflush(stream_new);
// }
// break;
// }
if(record >0)
{
int i;
for(i=0; i<record; i++)
{
buf[i] ^= 0x88;
}
//count += sizeof(buf);
fwrite(buf, 1, strlen(buf), stream_new);
//printf("pos2 count: %d\n",count);
fflush(stream_new);
memset(buf, 0, sizeof(buf));
len -= record;
printf("%d\n",len);
}
else if(record ==0)
{
break;
}
else
{
printf("fread() failed.\n");
return Failed;
}
}
printf("%d\n",len);
fclose(stream_new);
fclose(stream_org);
return Success;
}
int main(int argc, char *argv[])
{
if(argc < 3)
{
printf("Usage:%s [-c|-d] filename\n",argv[0]);
return 1;
}
else
{
char str[LEN] = {0};
strcpy(str, argv[2]);
puts(str);
fileInfo *info;
switch(argv[1][1])
{
case 'c':
{
getInfo(str, &info, 1);
if ( cyp(argv[2], info) == Success )
{
printf("Encrypted Successfully!\n");
return 0;
}
else
{
printf("Encrypted Failed!\n");
return 0;
}
}
break;
case 'd':
{
getInfo(str, &info, 0);
if ( cyp(argv[2], info) == Success )
{
printf("Decrypted Successfully!\n");
return 0;
}
else
{
printf("Decrypted Failed!\n");
return 0;
}
}
break;
default:
printf("Unknow Args!\n");
return 0;
}
}
}
阅读全文
0 0
- c#文件加密程序
- C#加密狗程序
- c#加密解密文件
- c#加密解密文件
- c#加密解密文件
- 文件加密解密程序
- 文件加密小程序
- 文件加密程序
- C#非对称加密程序
- MD5加密的C#程序
- c#实现文件加密、解密及文件拖拽至程序图标直接打开
- C#加密與解密文件
- C# - Rijndael文件加密类
- C# - Rijndael文件加密类
- C# 加密文件 过程透析
- C# 文件加密解密 SHA256
- C#之文件简单加密
- C#强密匙加密文件.snk
- js中比较时间字串大小
- 在Eclipse中使用JUnit4进行单元测试(初级篇)
- 【LeetCode】Subsets 系列
- wds无限桥接技术:不接网线就可以让客厅和卧室有2个不同wifi
- jquery-validate插件之resetForm方法清除历史错误提示信息
- c#文件加密程序
- hdfs上传文件大小为零问题解决
- 恐怖的死亡艺术,稻川淳二为自己办了场VR葬礼
- js封装ajax方法
- Android触摸屏事件派发机制详解与源码分析一(View篇)
- QT+FFMPEG播放器——github代码
- Spring-dataJpa实例
- iOS 浮点数格式字符串比较大小 精度问题
- Java程序员们最常犯的10个错误(如数组的操作)