设为首页 加入收藏

TOP

c++ 使用Tea算法进行加密解密。(一)
2015-11-21 01:03:59 来源: 作者: 【 】 浏览:5
Tags:使用 Tea 算法 进行 加密解密
最近在进行cocos2dx的项目,涉及到文件 加密的问题,而解密实在游戏加载的时候进行。 因此在加密功能之上还必须要求尽可能快速。所以选择了tea加密算法。
?
?
而加密过程使用window的commond 项目,进行针对文件夹或者文件加密。
?
?
?
闲话少说,直接上代码。
?
加密部分,由于对于win commond line 项目不是很熟悉, 所以做的比较粗糙。
?
// tea.cpp : 定义控制台应用程序的入口点。
//
?
#include "stdafx.h"
#include "Tea/TeaEd.h"
#include
#include
#include
#include
#include < windows.h> ?
#include
#include
using namespace std;
?
//判断是一个文件夹
inline bool IsFolder(const string & strWorkSpace, string &fileName, string & filePath)
{
? ? bool bCheck = false;
? ? fileName = strWorkSpace;
? ? filePath = "";
? ? size_t pos = strWorkSpace.find_last_of("\\");
? ? if (pos != std::string::npos)
? ? {
? ? ? ? filePath = strWorkSpace.substr(0, pos + 1);
? ? ? ? fileName = strWorkSpace.substr(pos + 1);
?
? ? ? ? pos = fileName.find_last_of(".");
? ? ? ? if (pos == std::string::npos)
? ? ? ? {
? ? ? ? ? ? bCheck = true;
? ? ? ? ? ? fileName = "";
? ? ? ? ? ? filePath = strWorkSpace;
? ? ? ? }
?
? ? }
? ? return bCheck;
}
?
int doMethod(const string & str)
{
?
//测试选项。输入输入1,则是解密一个文件,并输出到解密之后的内容
? ? if (str == "1")
? ? {
? ? ? ? cout << "将要解密的文件投入:" << endl;
? ? ? ? string strFileNameTmp;
? ? ? ? cin >> strFileNameTmp;
? ? ? ? string str1;
? ? ? ? string str2;
? ? ? ? bool bCheck = IsFolder(strFileNameTmp, str1, str2);
? ? ? ? if (bCheck)
? ? ? ? {
? ? ? ? ? ? cout << "测试解密的必须是一个文件,请按下回车继续操作" << endl;
? ? ? ? ? ? system("PAUSE");
?
? ? ? ? ? ? cout << "- 选择解密某个文件进行测试请输入 1 回车" << endl;
? ? ? ? ? ? cout << "- 选择进行加密请输入 其他字符 回车" << endl;
? ? ? ? ? ? string strInputTmp;
? ? ? ? ? ? cin >> strInputTmp;
? ? ? ? ? ? doMethod(strInputTmp);
? ? ? ? ? ? return 0;
? ? ? ? }
?
//这是一个单例加密类。负责加密和解密
? ? ? ? GetCryTool()->deCryWithFileName(strFileNameTmp, "");
?
? ? ? ? return 0;
? ? }
?
?
?
? ? cout << "1.拖入为文件" << endl;
? ? cout << "将在该文件同目录下创建一个以 [文件名 + '_cryfile'] 为规则的文件夹" << endl;
? ? cout << endl;
? ? cout << "2.拖入为文件夹" << endl;
? ? cout << "将在该文件夹同级目录下创建一个cryfolder文件夹,加密之后的文件都在这里" << endl;
? ? cout << "\n\n将文件或者文件夹拖入下方 :" << endl;
? ? string strWorkPath;
? ? cin >> strWorkPath;
?
? ? string strFileName = "";
? ? string strFilePath = "";
? ? cout << "Path : " << strWorkPath << endl;
? ? bool bCheck = IsFolder(strWorkPath, strFileName, strFilePath);
? ? if (bCheck)
? ? {
? ? ? ? do
? ? ? ? {
? ? ? ? ? ? //write path and create cry file folder
? ? ? ? ? ? size_t pos = strFilePath.find_last_of("\\");
? ? ? ? ? ? if (pos != std::string::npos)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? strFilePath = strFilePath.substr(0, pos + 1);
? ? ? ? ? ? }
?
? ? ? ? ? ? strFilePath.append("cryfolder");
?
? ? ? ? ? ? string strCreateForderCMD = "md ";
? ? ? ? ? ? strCreateForderCMD.append(strFilePath);
? ? ? ? ? ? system(strCreateForderCMD.c_str());
?
? ? ? ? } while (0);
//如果是一个文件夹, 则要判断这个文件夹下所有文件,并写入allFiles.txt文件中。然后按行读取这个文件,得到每个要加密文件的路径
? ? ? ? vector oVecLines;
? ? ? ? string sline;
? ? ? ? do
? ? ? ? {
? ? ? ? ? ? //create record file txt named allFiles
? ? ? ? ? ? //and del it
? ? ? ? ? ? string strAllFilePath = strFilePath;
? ? ? ? ? ? strAllFilePath.append("\\allFiles.txt");
?
? ? ? ? ? ? string strCmd = "cmd /c dir ";
? ? ? ? ? ? strCmd.append(strWorkPath);
? ? ? ? ? ? strCmd.append("\\*.* /a-d /b /s >");
? ? ? ? ? ? strCmd.append(strAllFilePath);
? ? ? ? ? ? cout << strCmd << endl;
? ? ? ? ? ? system(strCmd.c_str());
?
? ? ? ? ? ? ifstream ifs(strAllFilePath);
? ? ? ? ? ? while (ifs && getline(ifs, sline))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? cout << sline << endl;
? ? ? ? ? ? ? ? oVecLines.push_back(sline);
? ? ? ? ? ? }
? ? ? ? ? ? ifs.close();
?
? ? ? ? ? ? string strDelCMD = "del ";
? ? ? ? ? ? strDelCMD.append(strAllFilePath);
? ? ? ? ? ? system(strDelCMD.c_str());
?
? ? ? ? } while (0);
?
//遍历文件路径。依次加密。
? ? ? ? for (size_t i = 0; i < oVecLines.size(); i++)
? ? ? ? {
? ?
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇poj 1375 Intervals(解析几何 过.. 下一篇LeetCode(1) Two Sum

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: