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;

}

相关推荐
祈安_2 天前
C语言内存函数
c语言·后端
郑州光合科技余经理4 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1234 天前
matlab画图工具
开发语言·matlab
dustcell.4 天前
haproxy七层代理
java·开发语言·前端
norlan_jame4 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone4 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054964 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
czy87874754 天前
除了结构体之外,C语言中还有哪些其他方式可以模拟C++的面向对象编程特性
c语言
遥遥江上月4 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_531237174 天前
C语言-数组练习进阶
c语言·开发语言·算法