C语言加密汉字、图片

// encryp.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include <string.h>

#include <stdlib.h>

unsigned char doEncryp(const unsigned char* src, int srcLen, unsigned char* out_) {

if (!src || !srcLen || !out_)

return 0;

for (int i = 0; i < srcLen - 1; i += 2) {

out_[i] = src[i + 1];

out_[i + 1] = src[i];

}

if (0 != srcLen % 2) {

out_[srcLen - 1] = src[srcLen - 1];

}

return 1;

}

int _tmain(int argc, _TCHAR* argv[])

{

while (1) {

printf("请输入指令,0:退出,1:加密汉字,2:加密图片:");

int cmd = 0;

scanf("%d", &cmd);

if (0 == cmd)

break;

if (1 == cmd) {

printf("请输入汉字:");

char swords[4096] = { 0 };

scanf("%s", swords);

char result[4096] = { 0 };

if (doEncryp((unsigned char*)swords, strlen(swords), (unsigned char*)result)) {

printf("加密后的汉字是:%s\r\n", result);

}

}

else if (2 == cmd) {

printf("请输入图片完整路径(比如:c:\\1.jpg):");

char picPath[255] = { 0 };

scanf("%s", picPath);

FILE* fp = fopen(picPath, "rb");

if (!fp) {

printf("输入的文件路径不存在!!!\r\n");

}

else {

fseek(fp, 0, SEEK_END);

int fsize = ftell(fp);

fseek(fp, 0, SEEK_SET);

unsigned char* src = (unsigned char*)malloc(fsize);

unsigned char* dest = (unsigned char*)malloc(fsize);

fread(src, fsize, 1, fp);

fclose(fp);

if (doEncryp(src, fsize, dest)) {

char destFname[255] = { 0 };

sprintf(destFname, "%s_加密", picPath);

FILE* fp2 = fopen(destFname, "wb");

fwrite(dest, fsize, 1, fp2);

fclose(fp2);

printf("加密后的图片已保存到:%s\r\n", destFname);

}

free(src);

free(dest);

}

}

}

return 0;

}

相关推荐
yy鹈鹕灌顶38 分钟前
LeetCode 字符串类题目解析与 Java 实现指南(深度优化版)
java·开发语言·算法·leetcode
阳光_你好3 小时前
简单介绍C++中线性代数运算库Eigen
开发语言·c++·线性代数
风逸hhh7 小时前
python打卡day29@浙大疏锦行
开发语言·前端·python
浩皓素7 小时前
深入理解For循环及相关关键字原理:以Python和C语言为例
c语言·python
ᖰ・◡・ᖳ7 小时前
JavaScript:PC端特效--缓动动画
开发语言·前端·javascript·css·学习·html5
hy____1237 小时前
C++多态的详细讲解
开发语言·c++
小葡萄20257 小时前
黑马程序员C++2024版笔记 第0章 C++入门
开发语言·c++·笔记
万物此臻7 小时前
C#编写软件添加菜单栏
开发语言·c#
RongSen338 小时前
Python海龟绘图(Turtle Graphics)核心函数和关键要点
开发语言·python
小贾要学习8 小时前
【C语言】贪吃蛇小游戏
c语言·开发语言·游戏