【C/C++学院】0801-重定向以及文件扫描/二进制加密解密/简单加密/按照密码加密/动态库与静态库
2015-09-10 13:19
501 查看
重定向以及文件扫描
#define _CRT_SECURE_NO_WARNINGS//关闭安全检查 #include<stdio.h> #include<stdlib.h> void main1() { char str[100] = { 0 }; scanf("%s", str); printf("str=%s\n", str); system(str); } void main2() { char str[100] = { 0 }; fscanf(stdin,"%s", str); fprintf(stdout,"str=%s\n", str); system(str); } void main3() { char *path = "C:\\Users\\wuyq\\Desktop\\newcmd.txt"; int num=0; char docmd[30] = { 0 }; scanf("%d%s", &num, docmd);//接受键盘输入 FILE *pf; pf = fopen(path, "w");//写入 if (pf == NULL) { printf("文件打开失败"); } else { fprintf(pf, "for /l %%i in (1,1,%d) do %s", num, docmd); fclose(pf); } system("pause"); } void main4() { char cmd[100] = { 0 }; int num = 0; char docmd[30] = { 0 }; char *path = "C:\\Users\\wuyq\\Desktop\\newcmd.txt"; FILE *pf = fopen(path, "r");//读取 if (pf == NULL) { printf("文件打开失败"); return; } else { fscanf(pf, "for /l %%i in (1,1,%d) do %s", &num, docmd); printf("num=%d,docmd=%s", num, docmd); } system("pause"); } void main() { int num ; scanf("num=%d", &num);//必须精确对应 printf("num=%d", num); system("pause"); }
二进制加密解密
#define _CRT_SECURE_NO_WARNINGS//关闭安全检查 #include<stdio.h> #include<stdlib.h> int getfilesize(char *path) { FILE *pf = fopen(path, "r"); if (pf == NULL) { return -1; } else { fseek(pf, 0, SEEK_END); int length = ftell(pf); return length;//获取文件大小 } } void copy(char *oldpath, char *newpath) { FILE *pfr, *pfw; pfr = fopen(oldpath, "rb"); pfw = fopen(newpath, "wb");//写入二进制模式 if (pfr == NULL || pfw == NULL) { fclose(pfr);//关闭文件 fclose(pfw); return; } else { int length = getfilesize(oldpath); char *p = (char *)malloc(length*sizeof(char));//分配内存,读取文件 fread(p, sizeof(char), length, pfr);//读取二进制到内存 fwrite(p, sizeof(char), length, pfw);//写入二进制到文件 fclose(pfr);//关闭文件 fclose(pfw); } } void jia(char *oldpath, char *newpath) { FILE *pfr, *pfw; pfr = fopen(oldpath, "rb"); pfw = fopen(newpath, "wb");//写入二进制模式 if (pfr == NULL || pfw == NULL) { fclose(pfr);//关闭文件 fclose(pfw); return; } else { int length = getfilesize(oldpath); char *p = (char *)malloc(length*sizeof(char));//分配内存,读取文件 fread(p, sizeof(char), length, pfr);//读取二进制到内存 for (int i = 0; i < length; i++) { p[i] ^= 'A';//抑或 } fwrite(p, sizeof(char), length, pfw);//写入二进制到文件 fclose(pfr);//关闭文件 fclose(pfw); } } void jie(char *oldpath, char *newpath) { FILE *pfr, *pfw; pfr = fopen(oldpath, "rb"); pfw = fopen(newpath, "wb");//写入二进制模式 if (pfr == NULL || pfw == NULL) { fclose(pfr);//关闭文件 fclose(pfw); return; } else { int length = getfilesize(oldpath); char *p = (char *)malloc(length*sizeof(char));//分配内存,读取文件 fread(p, sizeof(char), length, pfr);//读取二进制到内存 for (int i = 0; i < length; i++) { p[i] ^= 'A';//解密 } fwrite(p, sizeof(char), length, pfw);//写入二进制到文件 fclose(pfr);//关闭文件 fclose(pfw); } } void main() { char *oldpath = "C:\\Users\\wuyq\\Desktop\\calc.exe"; char *newpath = "C:\\Users\\wuyq\\Desktop\\calc.exe"; char *newjiepath = "C:\\Users\\wuyq\\Desktop\\calc.exe"; jia(oldpath, newpath); jie(newpath, newjiepath); system("pause"); }
简单加密
#define _CRT_SECURE_NO_WARNINGS//关闭安全检查 #include<stdio.h> #include<stdlib.h> // 'a' ->'b' //hello -> ifmmp //textjjia.txt //textjie.txt //按照字节的方式加密 char jiami(char ch) { return ch ^ 123; } char jiemi(char ch) { return ch ^123; } void jia(char *path, char *pathjia) { FILE *pfr, *pfw; pfr = fopen(path, "r");//读取 pfw = fopen(pathjia, "w");//写入 if (pfr == NULL || pfw == NULL) { return; } else { while (!feof(pfr))//到了文件末尾1,没有到就是0 { char ch = fgetc(pfr);//读取字符 putchar(ch); fputc(jiami(ch), pfw);//写入一个加密的字符 } fclose(pfr); fclose(pfw);//关闭文件 } } void jie(char *path, char *pathjie) { FILE *pfr, *pfw; pfr = fopen(path, "r");//读取 pfw = fopen(pathjie, "w");//写入 if (pfr == NULL || pfw == NULL) { return; } else { while (!feof(pfr))//到了文件末尾1,没有到就是0 { char ch = fgetc(pfr);//读取字符 putchar(ch); fputc(jiemi(ch), pfw);//写入一个加密的字符 } fclose(pfr); fclose(pfw);//关闭文 } } void main() { char *path = "C:\\Users\\yincheng01\\Desktop\\text.txt"; char *pathjia = "C:\\Users\\yincheng01\\Desktop\\textjia.txt"; char *pathjie = "C:\\Users\\yincheng01\\Desktop\\textjie.txt"; jia(path, pathjia); jie(pathjia, pathjie); system("pause"); } void main1() { FILE *pfr; char *path = "C:\\Users\\yincheng01\\Desktop\\text.txt"; pfr = fopen(path, "r"); if (pfr == NULL) { printf("文件打开失败"); } else { printf("\n原来的资料\n"); while (!feof(pfr))//feof没有到文件末尾返回0,到了返回1 { char ch=fgetc(pfr);//从文件读取一个字符 putchar(ch);//输出字符 } rewind(pfr);//回到文件开头 printf("\n加密后的资料\n"); while (!feof(pfr))//feof没有到文件末尾返回0,到了返回1 { char ch = fgetc(pfr);//从文件读取一个字符 putchar(ch+1);//输出字符 } fclose(pfr);//关闭文件 } system("pause"); }
按照密码加密
#define _CRT_SECURE_NO_WARNINGS//关闭安全检查 #include<stdio.h> #include<stdlib.h> #include<string.h> //加密,按照密码 // 文件加密 // 字符串加密 char * stringjiami(char *password, char *string); //字符串解密 char * stringjiemi(char *password, char *jiastring); void filejiami(char *path, char *pathjia, char *password); void filejiemi(char *pathjia, char *pathjie, char *password);
#define _CRT_SECURE_NO_WARNINGS//关闭安全检查 #include<stdio.h> #include<stdlib.h> #include<string.h> //加密,按照密码 // 文件加密 // 字符串加密 char * stringjiami(char *password, char *string); //字符串解密 char * stringjiemi(char *password, char *jiastring); void filejiami(char *path, char *pathjia, char *password); void filejiemi(char *pathjia, char *pathjie, char *password);
#include"密码加密.h"
int getfilesize(char * path)
{
FILE *pf = fopen(path, "r");
if (pf == NULL)
{
return -1;
}
else
{
fseek(pf, 0, SEEK_END);//文件末尾
int length = ftell(pf);
return length;//返回长度
}
}
char * stringjiami(char *password, char *string)
{
int passlength = strlen(password);//获取加密长度
int stringlength = strlen(string);//获取字符串长度
if (stringlength %passlength ==0)
{
int ci = stringlength /passlength;//循环次数
for (int i = 0; i < ci; i++)//循环次
{
for (int j =0 ; j < passlength; j++)//循环密码
{
string[passlength*i+j] ^= password[j];//异或加密
}
}
}
else
{
int ci = stringlength / passlength;//循环次数
for (int i = 0; i < ci; i++)//循环次
{
for (int j = 0; j < passlength; j++)//循环密码
{
string[passlength*i + j] ^= password[j];//异或加密
}
}
int lastlength = stringlength%passlength;//剩下的长度
for (int i = 0; i < lastlength; i++)
{
string[passlength*(stringlength/passlength)+i] ^= password[i];
}
}
return string;
}
//字符串解密
char * stringjiemi(char *password, char *jiastring)
{
int passlength = strlen(password);//获取加密长度
int stringlength = strlen(jiastring);//获取字符串长度
if (stringlength %passlength == 0)
{
int ci = stringlength / passlength;//循环次数
for (int i = 0; i < ci; i++)//循环次
{
for (int j = 0; j < passlength; j++)//循环密码
{
jiastring[passlength*i + j] ^= password[j];//异或加密
}
}
}
else
{
int ci = stringlength / passlength;//循环次数
for (int i = 0; i < ci; i++)//循环次
{
for (int j = 0; j < passlength; j++)//循环密码
{
jiastring[passlength*i + j] ^= password[j];//异或加密
}
}
int lastlength = stringlength%passlength;//剩下的长度
for (int i = 0; i < lastlength; i++)
{
jiastring[passlength*(stringlength / passlength) + i] ^= password[i];
}
}
return jiastring;
}
void filejiami(char *path, char *pathjia, char *password)
{
FILE *pfr,*pfw;
pfr = fopen(path, "r");
pfw = fopen(pathjia, "w");
if (pfr == NULL || pfw == NULL)
{
fclose(pfr);
fclose(pfw);
return;
}
else
{
int length = getfilesize(path);//获取长度 430
//int passlength = strlen(password);//获取密码20
char *newstr = (char*)malloc(sizeof(char)*(length+1));
for (int i = 0; i < length; i++)
{
char ch = fgetc(pfr);//获取一个字符
newstr[i] = ch;//不断存入字符
//fputc(newstr[i], pfw);//挨个写入字符
}
newstr[length] = '\0';//字符串处理为'\0'
stringjiami(password, newstr);//加密字符串
for (int i = 0; i < length ; i++)
{
fputc(newstr[i], pfw);//挨个写入字符
}
fclose(pfr);
fclose(pfw);//关闭文件
}
}
void filejiemi(char *pathjia, char *pathjie, char *password)
{
FILE *pfr, *pfw;
pfr = fopen(pathjia, "rb");
pfw = fopen(pathjie, "wb");
if (pfr == NULL || pfw == NULL)
{
fclose(pfr);
fclose(pfw);
return;
}
else
{
while (!feof(pfr))
{
char string[256] = { 0 };
fgets(string, 256, pfr);
stringjiemi(password, string);//加密啊
fputs(string, pfw);//写入jie密文件
}
fclose(pfr);
fclose(pfw);//关闭文件
}
}
#define _CRT_SECURE_NO_WARNINGS//关闭安全检查 #include<stdio.h> #include<stdlib.h> #include"密码加密.h" void main() { char string[50] = "锄禾日当午"; char *password = "123"; printf("%s\n", stringjiami(password, string)); printf("%s\n", stringjiami(password, string)); char *path = "C:\\Users\\yincheng01\\Desktop\\text.txt"; char *pathjia = "C:\\Users\\yincheng01\\Desktop\\textjia.txt"; char *pathjie = "C:\\Users\\yincheng01\\Desktop\\textjie.txt"; //printf("%d\n", getfilesize(path)); filejiami(path, pathjia, "ABCDE"); filejiami(pathjia, pathjie, "ABCDE"); system("pause"); }
动态库与静态库
静态库Lib.h
void msg(); int add(int a, int b);Lib.c
#include<stdio.h> #include<stdlib.h> #include<Windows.h> void msg() { MessageBoxA(0, "1", "2", 0); } int add(int a, int b) { return a + b; }Main.c
#include<stdio.h> #include<stdlib.h> #include"lib.h" #pragma comment(lib, "静态库与动态库.lib") //头文件只是说明,lib自己已经存在接口 void main() { printf("%d", add(10, 9)); msg(); }
动态库
Dll.c
#include<stdio.h> #include<stdlib.h> #include<Windows.h> //导出函数,可以加载的时候调用 _declspec(dllexport) void msg() { MessageBoxA(0, "1", "2", 0); } //导出函数,可以加载的时候调用 _declspec(dllexport) int add(int a, int b) { return a + b; }
Main.c
#include<stdio.h> #include<stdlib.h> #include<Windows.h> typedef void (*pmsg)();//简化函数指针 typedef int(*padd)(int a, int b);//简化函数指针 void main() { HMODULE mydll = LoadLibraryA("动态库.dll"); if (mydll == NULL) { printf("动态库加载失败"); } else { pmsg pmsg1;//定义一个函数指针 pmsg1 = (pmsg)GetProcAddress(mydll, "msg");//获取函数地址 if (pmsg1 != NULL) { pmsg1();//执行 } padd padd1;//定义函数指针 padd1 = (padd)GetProcAddress(mydll, "add"); if (padd1 != NULL) { printf("\n%d", padd1(10, 29)); } } FreeLibrary(mydll); system("pause"); }
相关文章推荐
- C/C++语言extern使用方法总结
- c语言学习二
- VC++中动态链接库的显示加载和隐式加载的区别
- C++用法的学习心得
- c语言学习一
- vc++6.0的控制台的中文为乱码
- c++ vector
- c语言 readline实现
- C++_插入排序
- C++_sizeof函数
- 第一课、认识C语言
- C++用法的学习心得
- c++拷贝构造函数的形参为什么必须是引用类型
- 详解C/C++函数指针声明 ( *( void(*)())0)();
- Effective C++读书笔记(一)
- 【源码分享】-c++界面源代码分享
- C++贪吃蛇源代码
- C++中指针的引用用法实例
- C++中struct也有构造函数与析构函数,也可以有访问类型控制以及结构体大小,类大小
- 位带操作在stm32中的C语言实现