本文实例为大家分享了C语言实现文件加密解密功能的具体代码,供大家参考,具体内容如下
使用命令提示符,实现任何文件的加密和解密功能。
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
//#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdlib.h> #include<time.h> #include<sys/stat.h> #pragma warning(disable:4996) //加密 void Encryption( char *p, size_t n) { for ( int i = 0; i < n;++i) { *p += 7; } } //解密 void Decrypt( char *p, size_t n) { for ( int i = 0; i < n;++i) { *p -= 7; } } char *EnOrDe = {0}; int main( int argc, char *args[]) { clock_t c1 = clock (); //系统当前时间,毫秒为单位 char *FileNameSrc = ( char *) calloc (160, sizeof ( char )); //待操作文件 char *p2 = ( char *) calloc (200, sizeof ( char )); //操作后文件名 //从命令行获取文件名和要处理的操作 FileNameSrc = args[1]; //文件名,包含路径 char *p1 = args[1]; EnOrDe = args[2]; //en表示加密,de表示解密 /*FileNameSrc = "E:\\iPhone6-new.txt" ; char *p1 = FileNameSrc; EnOrDe = "de";*/ /**********处理生成新的文件名***********/ //char *p2 = { 0 }; /*FileNameSrc = p1;*/ //printf("%s\n", FileNameSrc); //printf("%s\n", EnOrDe); int index = 0; while (*p1) { if (*p1!= '.' ) { *p2 = *p1; p2++; p1++; index++; } else if (*p1 == '.' ) { *p2 = '_' ; p2++; *p2 = 'H' ; p2++; *p2 = '.' ; p2++; p1++; index+=2; } } printf ( "\n" ); printf ( "信息摘要:\n" ); printf ( "--------------------------------------\n" ); printf ( "原文件:%s\n" , FileNameSrc); printf ( "操作:%s (en——加密,de——解密)\n" , EnOrDe); printf ( "预计结果文件:%s\n" , p2 - index - 1); printf ( "--------------------------------------\n\n" ); printf ( "请稍后,玩命处理中......\n" ); char *FileNameDst = p2 - index - 1; FILE *pr = fopen (FileNameSrc, "rb" ); FILE *pw = fopen (FileNameDst, "wb" ); struct stat st = { 0 }; size_t fileSize = st.st_size; //以字节为单位 //char *buf = NULL; //if (fileSize<1024*1024)//小于1M //{ // buf = malloc(sizeof(char) * 1024 * 20);//分配20K //} //else //{ // buf = malloc(sizeof(char)*fileSize / 10); //} char *buf = calloc (1024 * 1024 * 25, sizeof ( int )); //分配100M /*************定义函数指针***************/ void (*pFunc)( char *, size_t ); pFunc = NULL; if ( strcmp (EnOrDe, "en" ) == 0) { pFunc = Encryption; } else if ( strcmp (EnOrDe, "de" ) == 0) { pFunc = Decrypt; } /*************定义函数指针***************/ while (! feof (pr)) { //memset(buf, 0, sizeof(buf));//calloc自动初始化为0 size_t res = fread (buf, sizeof ( char ), sizeof (buf), pr); pFunc(buf, res); fwrite (buf, sizeof ( char ), res, pw); } fclose (pr); fclose (pw); printf ( "\n" ); printf ( "--------------------------------------\n" ); printf ( "执行成功!\n所在目录:%s\n" , FileNameDst); clock_t c2 = clock (); //系统当前时间,毫秒为单位 printf ( "耗时:%u毫秒\n" , c2-c1); printf ( "--------------------------------------\n" ); return 0; } |
效果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/u010771437/article/details/49705917