gpt4 book ai didi

c++ - 在 Linux 上加密目录中的文件的问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:02:08 25 4
gpt4 key购买 nike

我已经测试了程序的加密部分,它对单个文件工作得很好,甚至像“file1、file2、file3”这样的东西,但它不适用于目录。代码对我来说看起来不错,但是在执行时会出现段错误。

它应该加密目录中的文件并将它们写入具有新扩展名(旧名称+“.wbf”)的同一目录中,并在相反的情况下删除扩展名进行解密。我只会发布处理文件的代码部分,处理单个文件的 do_crypt 函数工作得很好,我认为这不是我的问题的根源。

    // PHP explode function

vector<string> explode (string text, char separator)

{

vector<string> split;

int last_trip = 0, pos = 0;

text += separator;

for (pos = 0; pos < text.size(); pos++)

{

if (text[pos] != separator)

{

// continue with iteration

}

else

{

split.push_back(text.substr(last_trip, pos - last_trip));

last_trip = pos + 1;

}

}

return split;

};

// LINUX -- directory listing function
string LS (string dir)
{
DIR *dp;
vector<string> files;
struct dirent *dirp;
if ((dp = opendir(dir.c_str())) = NULL)
{
cout << "Error (" << errno << ") opening " << dir << endl;
//return errno;
}
while ((dirp = readdir(dp)) != NULL)
{
files.push_back(string(dirp->d_name));
}
closedir(dp);
string explosive = "";
for (int i = 0; i < files.size(); i++)
{
explosive += files[i];
if (i != (files.size() - 1)) { explosive += ','; }
}
return 0;
}

// various functions for encryption

int main (int argc, char* argv[])

{

cout << "\t! ENCRYPTR -- File encryption utility written by WBlinder, 2010. !" << endl << endl;

cout << "\t\t\t\tOPTIONS:" << endl;

cout << "\t\t\t1\tCRYPT A FILE" << endl << "\t\t\t2\tDECRYPT A FILE" << endl << endl;

cout << "choice > ";

int opt;

cin >> opt;

string sin, sout;

string dummy; getline(cin, dummy);

/*cout << "input files > ";

cout.clear(); cin.clear();

getline(cin, sin);

vector<string> fin = explode(sin, ',');

cout << "output files > ";

getline(cin, sout);

vector <string> fout = explode(sout, ',');

if (sin.size() != sout.size())

{

cout << "NO. INPUT FILES UNEQUAL NO. OUTPUT FILES" << endl;

return 1;

}*/
string dir;
cout << "dir > ";
getline (cin, dir);
vector<string> input = explode(dir, ',');
vector<string> output = input;
switch (opt)
{
case 1:
for (int i = 0; i < output.size(); i++)
{
output[i] = output[i] + ".wbf";
}
break;
case 2:
for (int i = 0; i < output.size(); i++)
{
output[i] = output[i].substr(0, (output[i].size() - 4));
}
break;
}

cout << "password > ";

getline(cin, key);

cout << "N > ";

cin >> N;

cout << "(768 => fairly secure\t3072 => secure)\nextra rounds > ";

cin >> drop;

for (int brick = 0; brick < input.size(); brick++)

{

do_crypt(opt, dir + input[brick], dir + output[brick]);

}

/*string text;

cout << "text to split: ";

getline (cin, text);

vector<string> tnt = explode(text, '.');

for (int i = 0; i < tnt.size(); i++)

{

cout << i << ": " << tnt[i] << endl;

}*/

}

最佳答案

你的LS有很多问题功能。首先,你应该让它直接返回 vector<string>而不是将数据打包在 string 中使用逗号分隔值。这将使您免于调用 explode函数,如果目录包含一个带逗号的文件名(在 Linux 上是有效名称),则不会中断。

但更大的问题(导致)您的段错误是 return 0线。由于您的函数被声明为返回 string对象,并且自 string类具有来自 const char* 的隐式构造函数, 这被编译器解释为 return string(NULL) .当用 NULL 调用时指针此构造函数引发 logic_error异常(exception)。由于您没有捕获异常,C++ 运行时调用 abort功能。此函数在设计上会导致段错误以停止执行(如果启用,则生成核心转储以允许事后调试)。

你至少应该重写你的 LS像这样的功能:

string LS (string dir)
{
DIR *dp;
vector<string> files;
struct dirent *dirp;
if ((dp = opendir(dir.c_str())) == NULL)
{
cout << "Error (" << errno << ") opening " << dir << endl;
return string();
}
while ((dirp = readdir(dp)) != NULL)
{
files.push_back(string(dirp->d_name));
}
closedir(dp);
string explosive = "";
for (int i = 0; i < files.size(); i++)
{
explosive += files[i];
if (i != (files.size() - 1)) { explosive += ','; }
}
return explosive;
}

或者更好的是,更改其签名以返回 vector<string>并像这样重写它:

vector<string> LS (string dir)
{
DIR *dp;
vector<string> files;
struct dirent *dirp;
if ((dp = opendir(dir.c_str())) != NULL)
{
while ((dirp = readdir(dp)) != NULL)
{
files.push_back(string(dirp->d_name));
}
closedir(dp);
}
else
{
cout << "Error (" << errno << ") opening " << dir << endl;
}
return files;
}

关于c++ - 在 Linux 上加密目录中的文件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4529789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com