// 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;
}