一 文件的简单加解密----按照字节简单加减进行加解密

#define _CRT_SECURE_NO_WARNINGS

#include

#include

//按照字节的方式进行加解密

//加密

void Encryption(char *srcPath, char *destPath)

{

FILE *src = fopen(srcPath, "r");

FILE *dest = fopen(destPath, "w");

if (!src || !dest)

{

printf("文件打开失败!\n");

system("pause");

return;

}

while (!feof(src))

{

char ch = fgetc(src);

fputc(ch + 1, dest);

}

fclose(src);

fclose(dest);

}

//解密

void Decrypthion(char *srcPath, char *destPath)

{

FILE *src = fopen(srcPath, "r");

FILE *dest = fopen(destPath, "w");

if (!src || !dest)

{

printf("文件打开失败!\n");

system("pause");

return;

}

while (!feof(src))

{

char ch = fgetc(src);

fputc(ch - 1, dest);

}

fclose(src);

fclose(dest);

}

void main()

{

char srcPath[100] = { 0 };

char destPath[100] = { 0 };

int flag = 0;

printf("请选择类型[1:加密 0:解密]\n");

scanf("%d", &flag);

switch (flag)

{

case 0:

printf("请输入您要解密的文件路径及其名称;\n");

scanf("%s", srcPath);

printf("请输出您要输出的路径及其名称:\n");

scanf("%s", destPath);

Decrypthion(srcPath, destPath);

break;

case 1:

printf("请输入您要加密的文件路径及其名称;\n");

scanf("%s", srcPath);

printf("请输出您要输出的路径及其名称:\n");

scanf("%s", destPath);

Encryption(srcPath, destPath);

break;

default:

printf("您输入的指令有误!");

break;

}

system("pause");

}

源文件:test.txt

如果有远方

那里一定种着梦想

耕种它的老人

随铁锹一起长成一株树

站在你必经的路口

如果有远方

那里一定有着你的微笑

你泛着星光的皓齿

如北斗般引领方向

如果有远方

那里一定有等盼千年的你

红色的落脚连衣裙

以醉人的姿态

等我入怀

如果有远方

那里一定有阳光沙滩

海浪之于足迹

如今之于往昔

如果有远方

那里一定住着一位诗人

他往后背的长发

还有如竹节的手指

能诗善画

如果有远方

我想我必要去一趟

拔掉电脑电源

手机扔进垃圾筐

反了门,锁掉窗

一个轻便的行囊

还有老杜用过的拄杖

如果有远方

我想我必要去一趟

加密之后的文件:jiami.txt

设狐匝辗妇湃琉蛹珐鬃仄南徐裹鬃听杜列商嚏望让蛹邱触词蛹纂锁挚折配操慨杜酶累设狐匝辗妇湃琉蛹珐匝仄配杜希学配富仄讶恒杜癃崔设膊犯变誉马妇畜设狐匝辗妇湃琉蛹珐匝渡葡权烹杜配豁尸杜缅酒颅用珊诱仡商杜靥铜渡嫌伸即设狐匝辗妇湃琉蛹珐匝阴恒蚀统护撂庄咱劁饯设掘庄咱晰硝设狐匝辗妇湃琉蛹珐丌仄蛹霞爽商厅晰霍泊杜触福己匝设昨聚杜俗坠泡爽是籍设狐匝辗妇嫌徐嫌操荧搔蛹吞毕鄂惰耪惰盏俗见稍均镰践礼傅绿钠き贴鄂当蛹棍肉蹭杜蜒庞己匝列菲阅湖杜柞稚设狐匝辗妇嫌徐嫌操荧搔蛹吞

解密之后又可以回到源文件的内容!

二 按照字节异或加解密

#define _CRT_SECURE_NO_WARNINGS

#include

#include

//按照字节的方式进行加解密

//加解密

void DeEncrypthion(char *srcPath, char *destPath)

{

FILE *src = fopen(srcPath, "r");

FILE *dest = fopen(destPath, "w");

if (!src || !dest)

{

printf("文件打开失败!\n");

system("pause");

return;

}

while (!feof(src))

{

char ch = fgetc(src);

fputc(ch^123, dest);

}

fclose(src);

fclose(dest);

}

void main()

{

char srcPath[100] = { 0 };

char destPath[100] = { 0 };

int flag = 0;

printf("请选择类型[1:加密 0:解密]\n");

scanf("%d", &flag);

switch (flag)

{

case 0:

printf("请输入您要解密的文件路径及其名称;\n");

scanf("%s", srcPath);

printf("请输出您要输出的路径及其名称:\n");

scanf("%s", destPath);

DeEncrypthion(srcPath, destPath);

break;

case 1:

printf("请输入您要加密的文件路径及其名称;\n");

scanf("%s", srcPath);

printf("请输出您要输出的路径及其名称:\n");

scanf("%s", destPath);

DeEncrypthion(srcPath, destPath);

break;

default:

printf("您输入的指令有误!");

break;

}

system("pause");

}

结果和上一程序结果一致!

3 输入密码对字符串的加解密

#define _CRT_SECURE_NO_WARNINGS

#include

#include

#include

//加解密函数

char *EnDecrypthion(char *src, char *password)

{

int srcLen = strlen(src);

int pwLen = strlen(password);

int iteger = srcLen / pwLen;

for (int i = 0; i < srcLen / pwLen; i++)

{

for (int j = 0; j < pwLen; j++)

{

src[(iteger - 1)*i + j] ^= password[j];

}

}

if (!(srcLen%pwLen))//能被整除的情况下

{

return src;

}

else

{

int remaider = srcLen%pwLen;

for (int i = 0; i < remaider; i++)

src[iteger*pwLen + i] ^= password[i];

return src;

}

}

void main()

{

char str[256] = { 0 };

char password[256] = { 0 };

printf("请输入一段需要加密的字符串:");

scanf("%s", str);

printf("请输入密码:");

scanf("%s", password);

printf("加密之后:%s\n", EnDecrypthion(str, password));

printf("解密之后:%s\n", EnDecrypthion(str, password));

system("pause");

}运行结果:

请输入一段需要加密的字符串:abcdfeg

请输入密码:4567

加密之后:UWUSRPQ

解密之后:abcdfeg

请按任意键继续. . .

4 输入密码对文件进行加解密

#define _CRT_SECURE_NO_WARNINGS

#include

#include

#include

//加解密函数

char *EnDecrypthion(char *src, char *password)

{

int srcLen = strlen(src);

int pwLen = strlen(password);

int iteger = srcLen / pwLen;//循环次数

for (int i = 0; i < iteger; i++)

{

for (int j = 0; j < pwLen; j++)

src[pwLen*i + j] ^= password[j];//注意此处的算法,这才是精髓所在

}

//处理剩下的字节

if (!(srcLen%pwLen))//能被整除的情况下

{

return src;

}

else //不能被整除的情况下

{

int remaider = srcLen%pwLen;

for (int i = 0; i < remaider; i++)

src[iteger*pwLen + i] ^= password[i];

return src;

}

}

//获取源文件的字节长度

long GetFileSize(char *Path)

{

FILE *fp = fopen(Path, "r");

if (!fp)

{

printf("获取文件长度时,打开文件失败!\n");

return -1;

}

fseek(fp, 0, SEEK_END);

long sz = ftell(fp);

fclose(fp);

return sz;

}

void FileEnDecrypthion(char *srcPath, char *destPath, char *password)

{

FILE *src = fopen(srcPath, "r");

FILE *dest = fopen(destPath, "w");

if (!src || !dest)

{

printf("打开文件失败!");

return;

}

long srcLen = GetFileSize(srcPath);//获取源文件的字节长度

char *temp = (char *)malloc((srcLen + 1)*sizeof(char));

for (int i = 0; i < srcLen; i++)//将文件中的字符全部拷贝到数组中

{

temp[i] = fgetc(src);

}

temp[srcLen] = '\0';

//下边有问题,加密函数有问题

temp = EnDecrypthion(temp, password);

for (int i = 0; i < srcLen; i++)

fputc(temp[i], dest);

//fclose(src);

fclose(dest);

}

void main()

{

char srcPath[256] = { 0 };

char destPath[256] = { 0 };

char password[256] = { 0 };

int flag = 0;

printf("请输入指令【0:加密 1:解密】:");

scanf("%d", &flag);

switch (flag)

{

case 0:

printf("请输入要加密的文件路径和文件名:\n");

scanf("%s", srcPath);

printf("请输入输出的文件路径和文件名:\n");

scanf("%s", destPath);

printf("请输入密码:");

scanf("%s", password);

FileEnDecrypthion(srcPath, destPath, password);

break;

case 1:

printf("请输入要解密的文件路径和文件名:\n");

scanf("%s", srcPath);

printf("请输入输出的文件路径和文件名:\n");

scanf("%s", destPath);

printf("请输入密码:");

scanf("%s", password);

FileEnDecrypthion(srcPath, destPath, password);

break;

default:

printf("您输入的指令有误!\n");

}

system("pause");

}

运行结果:

请输入指令【0:加密 1:解密】:0

请输入要加密的文件路径和文件名:

test.txt

请输入输出的文件路径和文件名:

dest.txt

请输入密码:123abc

请按任意键继续. . .dest.txt中的内容:

姎背鍎勡h蜍迟諜溴锭?k跇玟鴿拽颀hㄗ眾裘移续鄨穲┢8孢豆跹偣芪匂裰莨;貦搬鎱诌i貂髱柏嚉啾郸跹啣釘9詪弼饱谟圇懰徐8麊右噮儌盀疝勡瓚;貦搬鎱诌i貂髱柏嚉啾撰酎羝匂鱾h佥麡驭≌忲犖斌鷬k胺孢拽纟蚳柱貍8麊蹣忖缱辙;鲷眾剾膊才嬔ㄑ瘍8壜ⅷ鐪嗷祤崙9﹨蘩錆哺k獎埳啾墩啅9ゥ^鄨资磹弼迟瓓鴺┅i貝€亞パ菃?谯搬窊揠圇祲8鹘ㄈ執h嬋膊穱厧k踊銟柏9熏峙囋ザ种鎳kǖ娙邲駷垯輷;厙牘狋憻獨峙唭k柏壞魛觻匂惚Ρ;墛膊 ̄匂病谔圇窎儿8麊蹣忖缱辙;畨冸橙珨鄨i瓮虨潨瓮虨潨瓮虨潨瓮虨潨瓮虨潨瓮

请输入指令【0:加密 1:解密】:1

请输入要解密的文件路径和文件名:

dest.txt

请输入输出的文件路径和文件名:

src.txt

请输入密码:123abc

请按任意键继续. . .src.txt中的内容

如果有远方

那里一定种着梦想

耕种它的老人

随铁锹一起长成一株树

站在你必经的路口

如果有远方

那里一定有着你的微笑

你泛着星光的皓齿

如北斗般引领方向

如果有远方

那里一定有等盼千年的你

红色的落脚连衣裙

以醉人的姿态

等我入怀

如果有远方

那里一定有阳光沙滩

海浪之于足迹

如今之于往昔

如果有远方

那里一定住着一位诗人

他往后背的长发

还有如竹节的手指

能诗善画

如果有远方

我想我必要去一趟

拔掉电脑电源

手机扔进垃圾筐

反了门,锁掉窗

一个轻便的行囊

还有老杜用过的拄杖

如果有远方

我想我必要去一趟



5 二进制文件加解密

#define _CRT_SECURE_NO_WARNINGS

#include

#include

#include

//加解密函数

char *StrEnDecrypthion(char *src, char *password)

{

int srcLen = strlen(src);

int pwLen = strlen(password);

int iteger = srcLen / pwLen;//循环次数

for (int i = 0; i < iteger; i++)

{

for (int j = 0; j < pwLen; j++)

src[pwLen*i + j] ^= password[j];//注意此处的算法,这才是精髓所在

}

//处理剩下的字节

if (!(srcLen%pwLen))//能被整除的情况下

{

return src;

}

else //不能被整除的情况下

{

int remaider = srcLen%pwLen;

for (int i = 0; i < remaider; i++)

src[iteger*pwLen + i] ^= password[i];

return src;

}

}

long GetBinSize(char *path)

{

FILE *fp = fopen(path, "r");

if (!fp)

{

printf("读取字节时失败!\n");

fclose(fp);

return -1;

}

fseek(fp, 0, SEEK_END);

long sz = ftell(fp);

return sz;

}

void EnDecrypthion(char *srcPath, char *destPath, char *password)

{

FILE *src = fopen(srcPath, "rb");

FILE *dest = fopen(destPath, "wb");

if (!src || !dest)

{

printf("加解密时失败!\b");

fclose(src);

fclose(dest);

return;;

}

long srcLen = GetBinSize(srcPath);

char *temp = (char *)malloc(srcLen*sizeof(char));

//最精确和一致的村塾数字的方法就是使用与程序所使用的相同的位格式.

//对于标准I/O,fread()和fwrite()函数提供了这种二进制服务。

/*

size_t fread( void *buffer, size_t size, size_t count, FILE *stream );

fread returns the number of full items actually read, which may be less than count if an error occurs or if the end of the file is encountered before reaching count.

Use the feof or ferror function to distinguish a read error from an end-of-file condition. If size or count is 0, fread returns 0 and the buffer contents are unchanged.

buffer: Storage location for data

size: Item size in bytes

count:Maximum number of items to be read

stream:Pointer to FILE structure

The fread function reads up to count items of size bytes from the input stream and stores them in buffer.

The file pointer associated with stream (if there is one) is increased by the number of bytes actually read.

If the given stream is opened in text mode, carriage return–linefeed pairs are replaced with single linefeed characters.

The replacement has no effect on the file pointer or the return value. The file-pointer position is indeterminate if an error occurs.

The value of a partially read item cannot be determined.

*/

fread(temp, sizeof(char), srcLen, src);//读取二进制文件到内存

//加解密阶段

StrEnDecrypthion(temp, password);

/*

size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream );

fwrite returns the number of full items actually written, which may be less than count if an error occurs.

Also, if an error occurs, the file-position indicator cannot be determined.

buffer:Pointer to data to be written

size:Item size in bytes

count:Maximum number of items to be written

stream:Pointer to FILE structure

The fwrite function writes up to count items, of size length each, from buffer to the output stream.

The file pointer associated with stream (if there is one) is incremented by the number of bytes actually written.

If stream is opened in text mode, each carriage return is replaced with a carriage-return – linefeed pair.

The replacement has no effect on the return value.

*/

fwrite(temp, sizeof(char), srcLen, dest);//将内存中的数据读到文件

}

void main()

{

char srcPath[256] = { 0 };

char destPath[256] = { 0 };

char password[256] = { 0 };

int flag = 0;

printf("请输入指令【0:加密 1:解密】:");

scanf("%d", &flag);

switch (flag)

{

case 0:

printf("请输入要加密的文件路径和文件名:\n");

scanf("%s", srcPath);

printf("请输入输出的文件路径和文件名:\n");

scanf("%s", destPath);

printf("请输入密码:");

scanf("%s", password);

EnDecrypthion(srcPath, destPath, password);

break;

case 1:

printf("请输入要解密的文件路径和文件名:\n");

scanf("%s", srcPath);

printf("请输入输出的文件路径和文件名:\n");

scanf("%s", destPath);

printf("请输入密码:");

scanf("%s", password);

EnDecrypthion(srcPath, destPath, password);

break;

default:

printf("您输入的指令有误!\n");

}

system("pause");

}

此时所有文件都可以进行加解密,因为都是以二进制文件进行读取的。

c语言文件嚏怎么写,巩固C语言(十二)----文件加解密相关推荐

  1. IOS学习笔记之二十二(文件io)

    1.NSData和NSMutableData和NSURL NSData和NSMutableData表示oc的数据缓冲区 作用: 1).对数据读取NSData 2).输出NSData的数据 NSURL可 ...

  2. c语言程序设计笔记手写图片,C语言程序设计笔记.pdf

    陈立龙 (华中师范大学) C程序设计笔记( ) V1.06 一.C语言执行流程: 1.在C 语言中,一个C 语言源程序需要经过编译.连接和执行三个过程.其中编译是指将源程序 (*,c) 翻译成二进制形 ...

  3. c语言测试代码怎么写,初学C语言,写了一个测试手速的工具,代码发上来,存着。。...

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 RT 有幸看到的可以去试一试.... #include "stdio.h" #include "time.h" # ...

  4. llinux c 语言延时,linux下写个C语言程序,要求有0.5微秒以下的延时,要怎样写

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 #include #include #include #include #define B break void de(int timee) { unsi ...

  5. linux c语言 延迟,linux下写个C语言程序,要求有0.5微秒以下的延时,要怎样写

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 #include #include #include #include #define B break void de(int timee) { unsi ...

  6. python 学习笔记(十二) 文件和序列化

    python 文件读写和序列化学习. ## python文件读写 `1 打开并且读取文件` f = open('openfile.txt','r') print(f.read()) f.close() ...

  7. C语言程序设计现代方法(第二版)十二章课后练习题部分答案

    作为C语言初学者,以下内容为自己学习时所记,内容如有错误之处欢迎指出,非常感谢. 在此本人想致谢50no ,在此书的前面章节学习过程中曾多次参考50no 写的课后编程题答案 . 12.1 (a) 14 ...

  8. 跟我学Android之十二 文件解析与处理

    视频课:[免费]零基础学安卓Android移动开发 本章内容 第1节  File Explorer操作 第2节  SharedPreferences 第3节  普通文件操作 第4节  SD卡读写操作 ...

  9. 从零写一个编译器(十二):代码生成之生成逻辑

    项目的完整代码在 C2j-Compiler 前言 在上一篇解释完了一些基础的Java字节码指令后,就可以正式进入真正的代码生成部分了.但是这部分先说的是代码生成依靠的几个类,也就是用来生成指令的操作. ...

最新文章

  1. db2 linux 导入数据_「软件资料」-「软件使用」-Linux 导入、导出 MySQL 数据库命令...
  2. android最佳活动启动方法,026-启动活动的最佳写法
  3. java tcp 编程实例_Java实现基于TCP的通讯程序实例解析
  4. glassfish发布应用_Arquillian 1.0.0.Final正式发布! 准备使用GlassFish和WebLogic! 所有虫子死亡!...
  5. 对于一个十进制数A,将A转换为二进制数,然后按位逆序排列,再转换为十进制数(Java)
  6. Damon Edwards:IT运营是最可预测的DevOps差异化因素
  7. Pinyin4j 的使用 Pinyin4jUtils工具类
  8. eclipse启动mysql报错_Eclipse+mysql+java Eclipse中运行没有问题,但打包后运行不了,也不报错,求高手指点...
  9. 如何优雅地进行接口管理?(大厂内部分享)
  10. Vue列表搜索和排序---vue工作笔记0010
  11. 累计收益是我的收益吗?
  12. Matlab条形图bar横坐标间距设置
  13. 由ViewStateException: The client disconnected想到的
  14. 怎么安装winubuntu双系统_U盘安装ubuntu双系统及如何恢复Windows MBR教程
  15. SignalR-Client-CPP 源码编译
  16. yanqiyetan V1.0 存档
  17. 推荐一个工具:Dukto 免费快速实现局域网跨平台文件传输
  18. QT开发环境简介、安装以及搭建VS2019环境
  19. 计算机回收站设置大小,电脑怎么设置回收站容量 电脑回收站的数据文件位置在哪...
  20. python第六周拼图_python – 解决n-queen拼图

热门文章

  1. Win7 将 resync 命令发送到本地计算机 此计算机没有重新同步,因为要求的时间更改太大
  2. pandas - 交叉表与透视表
  3. unity3D 法线贴图的制作与使用
  4. 专升本第六讲 (世界的“小伙伴儿”都认识了)
  5. Spring源码解析(七)-Bean属性间的循环依赖
  6. 若你是这五大姓,说不定万里长城为你而建,霍去病打的是你的祖先
  7. NYOJ 998 解题报告
  8. 出租车GPS轨迹数据和手机数据的研究价值
  9. 原生js插件(超详细)
  10. wifiwan口速率什么意思_无线路由器WAN口状态的意思是什么?