因为项目包括我在内一共有3位同学...我们分别用Java,OpenCV, Matlab实现不同的模块...结果最后整合的时候出问题了= =向老师咨询后老师建议使用VC..整合OpenCV和Matlab方便点,然后咱就需要将Java的代码翻译成C的。。。
PS.这次是直接把Mac和次数加入视频尾部了..因为我不会实现MD5算法。。。Java里自带的类实现QAQ
一、加密部分
1、Mac.c
#include
#include
#include
//获取本地Mac
char *getMac()
{
FILE *fp = NULL;
char buf[4000] = {0};
char *index = NULL;
int size = 0;
char mac[18] = { 0 };
system("ipconfig /all > tmp.txt");
fp = fopen("tmp.txt", "r");
fseek(fp, 0, SEEK_END);
size = ftell(fp);
if (size >= sizeof(buf))
{
printf("Please extend the size of buf\n");
fclose(fp);
exit(0);
}
fseek(fp, 0, SEEK_SET);
fread(buf, 1, size, fp);
remove("tmp.txt");
fclose(fp);
index = strstr(buf, "本地连接");
//如果是英文操作系统需要另外加流程
if (index == NULL)
{
printf("Can't find 本地连接, may be you use english version\n");
}
//查找到本地连接后偏移202个字节后的17个字节是Mac地址(太过依赖于操作系统,待改进)
memcpy(mac, index + 202, 17);
return mac;
}
2、EncVideo.c
#include
/*
对视频进行加密,将本地Mac地址与授权次数添加进视频末尾
inPath:原视频文件路径
outPath:加密后文件输出路径
mac:本机Mac地址
*/
void encVideo(char *inPath, char *outPath, char *mac)
{
FILE *fpr = NULL;
FILE *fpw = NULL;
char temp[1024] = { 0 };
fpr = fopen(inPath, "rb");
fpw = fopen(outPath, "wb");
while (!feof(fpr))
{
fread(temp, 1, 1024, fpr);
fwrite(temp, 1, 1024, fpw);
}
fclose(fpr);
fclose(fpw);
fpw = fopen(outPath, "ab+");
fseek(fpw, 0, SEEK_END);
fputs(mac, fpw);
fputc('5', fpw);
fclose(fpw);
}
3、launcher.c
#include
#include "EncVideo.h"
#include "Mac.h"
int main()
{
char *mac = NULL;
char inPath[100] = { 0 };
char outPath[100] = { 0 };
printf("Please input file path you want to encrypt: ");
scanf("%s", inPath);
printf("Please input save path: ");
scanf("%s", outPath);
mac = getMac();
encVideo(inPath, outPath, mac);
return 0;
}
二、解密部分
#include
#include
#include
//获取本地Mac
char *getLocalMac()
{
FILE *fp = NULL;
char buf[4000] = { 0 };
char *index = NULL;
int size = 0;
char mac[18] = { 0 };
system("ipconfig /all > tmp.txt");
fp = fopen("tmp.txt", "r");
fseek(fp, 0, SEEK_END);
size = ftell(fp);
if (size >= sizeof(buf))
{
printf("Please extend the size of buf\n");
fclose(fp);
exit(0);
}
fseek(fp, 0, SEEK_SET);
fread(buf, 1, size, fp);
remove("tmp.txt");
fclose(fp);
index = strstr(buf, "本地连接");
//如果是英文操作系统需要另外加流程
if (index == NULL)
{
printf("Can't find 本地连接, may be you use english version\n");
}
//查找到本地连接后偏移202个字节后的17个字节是Mac地址(太过依赖于操作系统,待改进)
memcpy(mac, index + 202, 17);
return mac;
}
//获取视频尾部Mac与播放次数,前17位为Mac,最后一位为剩余播放次数
char *getVideoMac(char *path)
{
FILE *fp = NULL;
char mac[19] = { 0 };
fp = fopen(path, "r+");
fseek(fp, -18, SEEK_END);
fread(mac, 1, 18, fp);
fclose(fp);
return mac;
}
//比较Mac是否相同
int compareMac(char *localMac, char *videoMac)
{
int flag = 1;
for (int i = 0; i <= 16; i++)
{
if (localMac[i] != videoMac[i])
{
flag = 0;
break;
}
}
if (flag)
{
return 1;
}
return 0;
}
2、playCount.c
#include "Mac.h"
//获取剩余播放次数
int getVideoCount(char *mac)
{
int num = -1;
num = mac[17] - '0';
return num;
}
//判断是否满足剩余播放次数>0
int isCountSatisfiable(int num)
{
if (num > 0)
{
return 1;
}
return 0;
}
3、playVideo.c
#include
#include
#include
#include
#include "Mac.h"
#include "playCount.h"
#include "playVideo.h"
//判断是否满足播放条件
int isPlayable(char *path)
{
char *localMac = getLocalMac();
char videoMac[19] = { 0 };
strcpy(videoMac, getVideoMac(path));
int num = getVideoCount(videoMac);
if (compareMac(localMac, videoMac))
{
if (isCountSatisfiable(num))
{
changeFormat(path, num);
return 1;
}
else
{
printf("Your play count is too less\n");
remove(path);
exit(0);
}
}
else
{
printf("You don't have the access to play the video\n");
exit(0);
}
}
//还原为.avi格式且修改.k文件授权次数
void changeFormat(char *inPath, int num)
{
FILE *fpr = fopen(inPath, "rb+");
FILE *fpw = fopen("C:\\tmp\\Isc.avi", "wb+");
char buf[1024] = { 0 };
while (!feof(fpr))
{
fread(buf, 1, 1024, fpr);
fwrite(buf, 1, 1024, fpw);
}
fseek(fpr, -1, SEEK_END);
num--;
printf("you have %d times left\n", num);
char count = num + '0';
fputc(count, fpr);
fclose(fpr);
fclose(fpw);
}
//播放视频
void play()
{
//char buf[1024] =
char sysCom[200] = "start \"C:\\Program Files\\Windows Media Player\\wmplayer.exe\" ";
strcat(sysCom, "\"C:\\tmp\\Isc.avi\"");
system(sysCom);
}
4、launcher.c
#include
#include "playVideo.h"
int main()
{
char path[100];
char *outPath = NULL;
printf("Please enter .k file path: ");
scanf("%s", path);
if (isPlayable(path))
{
play();
}
return 0;
}
小结
才发现我命名.C的时候两种命名法混用了Orz 回头再改改 GUI找找资料看怎么实现。。。