设为首页 加入收藏

TOP

c++ 使用Tea算法进行加密解密。(二)
2015-11-21 01:03:59 来源: 作者: 【 】 浏览:9
Tags:使用 Tea 算法 进行 加密解密
? ? ? ? sline = oVecLines[i];
? ? ? ? ? ? string strTmpfileName = "";
? ? ? ? ? ? string strTmpFilePath = "";
? ? ? ? ? ? bool bCheck = IsFolder(sline, strTmpfileName, strTmpFilePath);
? ? ? ? ? ? if (!bCheck)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? string strOutFilePath = strFilePath;
? ? ? ? ? ? ? ? strOutFilePath.append("\\");
? ? ? ? ? ? ? ? strOutFilePath.append(strTmpfileName);
? ? ? ? ? ? ? ? GetCryTool()->enCryWithFileName(sline, strOutFilePath);
? ? ? ? ? ? }
? ? ? ? }
?
? ? }
? ? else
? ? {
? ? ? ? string strTmpWirtePath = strFilePath;
? ? ? ? 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);
? ? ? ? ? ? }
?
? ? ? ? ? ? size_t szPos = strFileName.find_last_of(".");
? ? ? ? ? ? if (szPos != std::string::npos)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? strTmpWirtePath.append(strFileName.substr(0, szPos));
? ? ? ? ? ? ? ? strTmpWirtePath.append("_cryfile");
? ? ? ? ? ? }
?
? ? ? ? ? ? string strCreateForderCMD = "md ";
? ? ? ? ? ? strCreateForderCMD.append(strTmpWirtePath);
? ? ? ? ? ? system(strCreateForderCMD.c_str());
?
? ? ? ? } while (0);
?
? ? ? ? strTmpWirtePath.append("\\");
? ? ? ? strTmpWirtePath.append(strFileName);
? ? ? ? GetCryTool()->enCryWithFileName(strWorkPath, strTmpWirtePath);
? ? }
?
}
?
int _tmain(int argc, _TCHAR* argv[])
{
?
? ? char buf[1000];
? ? GetModuleFileNameA(NULL, buf, 1000);
? ? string strKeyPath(buf);
? ? string strKeyfilepath = strKeyPath;
? ? size_t szPos = strKeyfilepath.find_last_of("\\");
? ? if (szPos != std::string::npos)
? ? {
? ? ? ? strKeyfilepath = strKeyfilepath.substr(0, szPos + 1);
? ? }
?
? ? string strKeyPrePath = strKeyfilepath;
//key_prepared.txt是用来存储明文密码的文件。 在加密项目下
? ? strKeyPrePath.append("key_prepared.txt");
//key_result.txt是将key加密之后的密码。同样也在加密项目下
? ? strKeyfilepath.append("key_result.txt");
? ? string strKeyPre = "";
?
? ? do?
? ? {
? ? ? ? ifstream ifs(strKeyPrePath);
? ? ? ? while (ifs && getline(ifs, strKeyPre))
? ? ? ? {
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? ifs.close();
? ? } while (0);
?
? ? if (strKeyPre.empty())
? ? {
? ? ? ? cout << "没有输入密码,请在" << strKeyPrePath << " 中写入密码" << endl;
? ? ? ? return 0;
? ? }
? ?
? ? string str;
? ? cout << "[ 当前密码为 :" << strKeyPre <<" ]"<< endl;
? ? cout << "- 修改密码请输入'key'回车" << endl;
? ? cout << "- 选择解密某个文件进行测试请输入1回车" << endl;
? ? cout << "- 选择进行加密请输入其他回车" << endl;
? ? cin >> str;
?
//判断是否要修改密码
? ? if (str == "KEY" || str == "key")
? ? {
? ? ? ? system("cls");
? ? ? ? string strNewKeyStr;
? ? ? ? cout << "请输入新密码 :" << endl;
? ? ? ? cin >> strNewKeyStr;
? ? ? ? GetCryTool()->setUpTea(strNewKeyStr, strKeyfilepath);
? ? ? ??
? ? ? ? cout << "密码已经修改.请继续操作" << endl;
? ? ? ? cout << "- 选择解密某个文件进行测试请输入1回车" << endl;
? ? ? ? cout << "- 选择进行加密请输入其他回车" << endl;
? ? ? ? cin >> str;
? ? ? ? doMethod(str);
? ? }
? ? else
? ? {
? ? ? ? GetCryTool()->setUpTea(strKeyPre, strKeyfilepath);
? ? ? ? doMethod(str);
? ? }
?
? ??
?
?
? ? system("PAUSE");
?
? ? return 0;
}
?
?
这面的代码是主函数部分。
?
然后放上加密解密的核心部分。
?
在TeaEd.h中
?
class TeaEd
{
public:
? ? /*
? ? ? ? isNetByte is unuseful param in this project.
? ? */
? ? TeaEd(const byte * key, ? ?int round = 32, bool isNetByte = false);
? ? TeaEd(const TeaEd &rhs);
? ? TeaEd & operator = (const TeaEd &rsh);
?
? ? void encrypt(const byte * in, byte * out);
? ? void decrypt(const byte * in, byte * out);
private:
? ? void encrypt(const ulong * in, ulong * out);
? ? void decrypt(const ulong * in , ulong * out);
?
?
//the method under is use fo
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇poj 1375 Intervals(解析几何 过.. 下一篇LeetCode(1) Two Sum

评论

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